Connecting two nodes in two different scenes with a signal

Godot Version

4.2.1

Question

Here’s a rough overview of the trees in my project

(Enemy01 Scene)

  • Enemy01
    • EnemyClassNodes
    • …some other nodes

(EnemyClassNodes Scene)

  • EnemyClassNodes
    • StateTree
      • IdleState
    • …some other nodes

For context, Enemy01 is a specific enemy and EnemyClassNodes is a bunch of nodes that each enemy should have, including nodes that manage hitboxes, hurtboxes, player detection etc, so each enemy should have them. This is why I have an extraa scene here.

I want to connect a signal from IdleState to a callable in Enemy01. I can’t do this in the editor since the IdleState node is in a separate scene compared to Enemy01. I guess I need to do it via code. The problem is that the new version of the .connect function only seems to be able to connect a signal from IdleState to a callable from IdleState. I’ve tried the following.

(code for Enemy01):

$EnemyClassNodes/State/Root/Idle.connect(“state_phyiscs_processing”, _on_idle_physics)
print($EnemyClassNodes/State/Root/Idle.is_connected(“state_phyiscs_processing”, _on_idle_physics))

I get “false” printed a lot.

Does this code connect the signal with the _on_idle_physics function of IdleState or that of Enemy01? Is there any way I can make this work without having to move both nodes into the same scene or doing some other weird workaround?

Note that I cannot add any code to IdleState directly since that node has its own external script. Let me know if anything needs clarification, thanks.

1.you can make use of EventBus autoload https://gdscript.com/solutions/signals-godot/#:~:text=the%20script%20file.-,Global%20Event%20Bus,-In%20GoDot%20we

simply do it like this:
EventBus.gd (AutoLoad)

class_name EventBus 
extends Node

signal send_state(state:bool)

Enemy01 node’s script
at _ready() add
EventBus.send_state.connect(target method)

to emit the signal from idleState node’s script:
EventBus.emit_signal("send_state",true)

2.in idleState node’s script
add
signal send_state(state:bool)

to emit the signal:
emit_signal("send_state",true)

Enemy01 node’s script
at _ready() add
$"EnemyClassNodes/StateTree/IdleState".send_state.connect( target method )

Note: code no.2 might not work as expected, because the idleState node might havent be ready when Enemy01 node’s ready

1 Like