Can you connect a signal to a parrallel scene?

Godot Version

4.2.2

Question

I have an area 2D on an enemy scene, and I want to send a signal to the player whenever it is triggered. my current approach is to use the area2D’s signal to call a function stored in an autoload, though I now can’t get that feedback down to the player. I’m probably using the wrong tool for the job, but I’m not sure what else I can do. the project is based on the brackeys “make your first game” tutorial, though i’ve made some changes to it, so not everything is the same. please help!

Maybe then use an signal from the autoload.

I can’t connect two parallel scenes, so since the autolod isn’t present in the player scene, I can’t link a signal. That’s why I used a function to get to the autoload. Ideally I can hop straight from the enemy to the player without the need for the autoload.

Why you cannot connect signal? how you tried? you can get reference of player and call player node method.

I think there’s something wrong with how you are emitting and connecting autoload signals.

Create an autoload script signals.gd like this:

extends Node

signal my_signal()

Go to ProjectSettings and add your autoload script to autoloads. Let’s name it Signals

Emit signal in your enemy script in like this

Signals.my_signal.emit()

Connect signal in your player script like this

func _ready():
  Signals.my_signal.connect(my_function)

func my_function():
  print("Hello!")

Is there any way to do this in the editor, or do you have to do this in script?

Edit: I’m already declaring the signal in script, but I wasn’t connecting it, I was trying to use the dialogue box for that. If there is a way to do it using the dialogue box, would it work for multiple scenes?

You can only connect autoload signals in script afaik.

Ok, thanks!

1 Like

how do you connect the signal in the code? my code passes an argument through the signal, but the connect function also wants an argument. I’ve looked at the documentation, but I don’t understand what a callable is, beyond a type of variable

functions are callables, you can pass them into the connect function

So, if I wanted the function to be named attack, I would pass in that name as the argument?

Yes, but don’t add the () after.

I think you can use .bind function to add arguments.

when I’m trying it, it works like a regular function, including the brackets. not sure when you wouldn’t include the brackets, just putting this here for anyone looking at this post in the future.

No, the brackets mean your calling the function. This is one of those godot rules that is somewhat unclear, but if you add brackets, it’s not a reference to the function.

Ah, nevermind, I just realised I misunderstood what part you were talking about, I meant that I added brackets after the func keyword, not the signal name :sweat_smile:

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