Godot Version
4.2
Question
How to connect a signal form a autoload Scene to a different Script ?
I want to connect a Signal from my Gobal Scene to my different Level_scenes
i get an Error (Invalid Argument, Argument 2 should be callable)
Both of my scripts are below
(Autoload)Gobal Script:
signal respawn_level_signal
func respawn():
Engine.time_scale = 1.0
player_score -= score_reset_counter
if has_level_checkpoint == true:
if checkpoint_reached == true:
pass
get_tree().reload_current_scene()
respawn_level_signal.emit()
print("Checkpoint: " + str(checkpoint_reached))
player_health = 1
Level Script:
func _ready():
Gobal.connect("respawn_level_signal", self, testtt()) #ERROR: Invalid Arugument
Gobal.current_level = "level_zero"
func testtt():
print("CONNECTED")
From Level script, get a ref to your autoloaded node, should be in the tree of your scene. then do someting like:
myRefToAutoload.MySignalDelegate += testtttt;
at least this is how you do it in C# should not be too different.
In your level script:
func _ready() -> void:
Gobal.respawn_level_signal.connect(testtt) # no parenthesis
Gobal.current_level = "level_zero"
func testtt():
print("CONNECTED")
In 4.x you should use the signal without quotes, and when connecting you want to give the function itself. With parenthesis this calls the function, it evaluates and it tries to connect to whatever the function returns (in your case nothing). Without parenthesis the function does not evaluate and returns itself, which the connection can store and call later.
Probably a good idea to save reload_current_scene
for last on this list.
1 Like
I get an error, if i do it your way
Can you tell me what this means?
func _ready():
Gobal.respawn_level_signal.connect(testtt())
func testtt():
print("CONNECTED")
Found it by myself.
function without()
func _ready():
Gobal.respawn_level_signal.connect(_test)
func _test():
print("CONNECTED")