Connect signals after instantiate a child

Godot Version

4.3

Question

hi,

i have a parent node, which spawns a player child on a grid on ready. the player sends a signal and grid grabs it. but it seems like my grid never listen to the signal.

setup:

parent (spawns child player)

  • grid (needs signal)
    ā€“ player (spawned, sends signal)

parent:

var player_scene = preload("res://Scenes/Player.tscn").instantiate()

func _ready():
grid.add_child(player_scene )

grid:

func _ready():
player.connect("my_signal", other_function)

player:

signal my_signal

some_function():
emit_signal("my_signal")

i know, grid tries to connect the signal before the player is loaded.

how can i fix that?

Try connect the signal in the parent node instead (you need to remember godot call the _ready from the childs to parent):

var player_scene = preload("res://Scenes/Player.tscn").instantiate()

func _ready():
	grid.add_child(player_scene)
	player_scene.my_signal.connect(grid.other_function)

Or just use groups instead:

# Grid script
func _ready():
	add_to_group("grid")
# Player script
some_function():
	get_tree().call_group("grid", "other_function")
2 Likes

hi matheusmdx,

the group did the trick! :slight_smile:
thanks for your time.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.