I recently started using Godot and still learning. I joined a game jam for more learning experience and i have some errors with my code that is attached to a button that is supposed to load a separate scene in 12 specific parts. Some of the code uses what i think should be Callable or connect but it wont work with either.
I tried changing the spot of error from "connect" to "Callable" but it only adds a runtime error.Ill show the errors i get with connect and Callable. The bold line is the line where the error occurs.
connect:
Line 23:Invalid argument for "connect()" function: argument 2 should be "Callable" but is "res://scenes/spawn_button_1.gd".
Line 23:Cannot pass a value of type "String" as "int".
Line 23:Invalid argument for "connect()" function: argument 3 should be "int" but is "String".
Callable(this is the main error, I think this is supposed to be the word but the following error blocks me):
Invalid call. Nonexistent function 'Callable' in base 'Button(spawn_button_1.gd)'.
Here is my code:
extends Button
#var rares = {"common" : 15,
#"rare" : 5,
#"worthless" : 2}
@export var scene_to_spawn : PackedScene
@onready var spawn_button = $"."
var spawn_positions = [
Vector2(-65, -65), Vector2(120, -65), Vector2(65, -28),
Vector2(-25, -65), Vector2(160, -65), Vector2(15, -28),
Vector2(15, -65), Vector2(160, -28), Vector2(-25, -28),
Vector2(65, -65), Vector2(120, -28), Vector2(-65, -28)
]
var occupied_positions = []
func _ready() -> void:
**spawn_button.Callable("pressed", "_on_Button_pressed")**
func _on_pressed() -> void:
your text`var available_positions = get_available_positions()
if available_positions.size() > 0:
var random_position = available_positions[randi() % available_positions.size()]
var new_scene = scene_to_spawn.instantiate()
new_scene.position = random_position
add_child(new_scene)
occupied_positions.append(random_position)
else:
print("no spots!")
func get_available_positions() -> Array:
var available_positions = []
for position in spawn_positions:
if !occupied_positions.has(position):
available_positions.append(position)
return available_positions
If there is any more info needed just ask and I'll see if I have it.
You can do it with less hardcoded strings.
func _ready():
self.pressed.conect(_on_pressed)
See the _on_pressed
is without parthesis. That means, the function will NOT be called, but it behaves like variaoble typed as Callable
.