I have to scenes which are completely unrelated to each other, so they’re completely seperate. Now I need scene A to send a custom signal to scene B but every time I try it doesn’t work. Is there any way to do this?
Scene A:
signal take_damage(amount: int)
func _ready() -> void:
take_damage.emit(10)
Scene B:
@onready var ally = $"./Ally"
func _ready() -> void:
ally.take_damage.connect(func (): print("working"))
#i have absolutely no idea what to do
The scene must exist at the same time to be connected, so how can you reach from one scene to another? Your signals name is take_damage, it seems like this object would have collision in that case, maybe your scene A and scene B collide at some point and can keep a reference from that point on.
Make sure to give more specific information when asking questions, why do you want to connect these two “unrelated” scenes? How are they really related?
Then it’s not about connecting the signals wrong, it’s about getting a proper reference to your Ally node.
Can you show a screenshot of your scene tree?
For the node that listens to the take_damage() signal, you can simply put @export var ally: Node and then put the node that emits the signal there with the editor. This way, you can choose which node’s signal you are listening to without making a change to the code.
For example:
## Set it to the node that emits the signal by using the editor's inspector panel.
@export var node: Node
func _ready() -> void:
## Check if the node has the signal you're looking for to avoid crashes.
if node and node.has_signal("signal_name"):
node.signal_name.connect(_on_signal_emitted)
func _on_signal_emitted() -> void:
print("Signal successfully received!!")
It is ready before your Ally is as @wchc said earlier.
Normally I would use a ‘signal bus’ for such things, however I think you problem will only be exacerbated by this. That node reference going up the tree and back down is really bad. I know it works, and is fine if your just chucking out some code in a very rough prototype. But that habit is going to cause you all sorts of problems in the future.
What you should do is in your root node ready function, call initialisation functions to pass references down the tree. So in your ‘game’ (root node) ready function do something like:
func _ready() -> void:
SceneB.initialise(SceneA)
By the time the root node is ready, all the others will be too, so you can pass the reference. Then in SceneB do something like:
var ally
func initialise(ally_ref_node) -> void:
ally = ally_ref_node
ally.take_damage.connect(_on_ally_take_damage)
Now your signals will connect.
I am guessing that @gertkeno was asking what you are trying to do because it is much better to not have to inject dependencies like this, and there may well be a better way to solve your actual problem, making your whole game structure more modular and independent. Dependency injection is perfectly fine, when used appropriately, just often it is used to overcome other deeper structural problems.