Trouble understand connecting via code

Godot Version

4.2

Question

I think I’m over complicating it but I have two scenes. One scene already exists in the world and needs to connect to a scene that is randomly spawned.

The randomly spawned object has a signal called “dialogue” when dialogue is emitted, the initial object that was already spawned has a “SetDialogue” function that needs to be called.

Whats the best way to connect the dialogue signal from the randomly spawned object to the SetDialogue method of the pre-spanwed object?

Thanks

Whatever script you spawn the random object in, connect it there after you spawn it. If that script doesn’t have a reference to both objects you could emit a signal to a parent object that can reference both nodes to connect the signal

1 Like

Does it need to be from the script spawning the object? I have it on the spawned objects script in _ready() but cant seem to get it to connect

shatteredreality is right, the important part is having the reference to the right object.

The only thing I might add is that you can connect a signal from either object, as long as you have a reference.

As an example, you could do this:

SCRIPT OF RANDOMLY SPAWNED OBJECT

signal dialogue

func _ready():
   existing_node = [[[make sure you have a reference to your existing node that has the SetDialogue function]]]
   dialogue.connect(existing_node.SetDialogue)

EDIT:
just saw your reply. It would help if you could provide us your codue / node structure so we can help better.

Turns out I was connecting wrong like this:

dialogue.connect(dialogueRef.SetDialogue())

instead of this

dialogue.connect(dialogueRef.SetDialogue)

Thank you

2 Likes

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