SignalBus Emit & Connect Help

Godot Version

4.3 `

Question

Howdy,

I was reading the documentation on Signals and came across a comment describing the use of a SignalBus. Right now I am trying to to see how it works and seem to be struggling to actually connect the signal. Here are the relevant bits of code:

signal_bus.gd

extends Node

signal player_action

player.gd

func _ready():
	SignalBus.player_action.emit()

enemy.gd

func _ready():
	SignalBus.player_action.connect(_on_player_action)
		
	
func _on_player_action():
	print("signal received")

I am fairly new to programming and am trying to rely on the documentation as much as possible before asking for help but this one has me stumped. I tried putting the argument for the signal connection in parentheses but I believe that was for an older version of Godot. I do believe that my issue lies in how I am trying to connect to the signal but can’t figure out the why. Any help is greatly appreciated, thanks!

The problem is probably order of execution, the player’s _ready() is called before the enemy’s _ready(), so the signal gets emitted before the enemy connected to it. Try putting the .emit() call into func _process(delta) instead, then you should see a result in the output.

2 Likes

Thank you! This worked. Completely forgot about execution order, hopefully won’t forget again!