Signals proper way -- can't find an answer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JoeSeki

This question is about the proper way to design my scenes and how they communicate.

I have a scene that uses an OperationCard scene in 9 places on the main scene. 3 different behaviors are instantiated and each unique on what sprite it contains. As you can imagine I might want to change the position of the Cards. As an object, OperationCard will send a signal that it has been selected, it also shares the ID the card has been assigned by the main scene so that I can track who is sending the signal.

Problem: When connecting to the signal of the OperationCard scenes. Do I need to connect to all 9 signals? Can I pick one OperationCard and connect to it, and expect that if any OperationCard sends the signal, I will receive it with the ID? Or do I need to make 9 connections to receive the signal with ID? If 9 signals, why did I send the ID?

If I’m doing it wrong, what is the right way to be making an OperationCard such that when it is selected I can begin the swap process or cancel it if a new selection is not on a possible swap position.

:bust_in_silhouette: Reply From: PickledPenguin

You probably already know this stuff already, but just in case:

To send information (like an ID) with a signal, you have to declare the signal in the OperationCard script like this:

signal signal_name(*value you want to send*)

So in your case:

signal signal_name(ID)

where “ID” is the ID variable you want to send over.

Then when you want to send the signal, use:

emit_signal("signal_name", ID)

In the script that you want to recieve the signal, (in your case, the main script) put this line of code in the _ready() function:

OperationCard.connect("signal_name", self, "on_signal_function")

Somewhere in your script you can then declare a function with the name you put as the last parameter.

func on_signal_function(recieved_ID):
    *do stuff*

And now you can transmit information across with signals.
As I said, you probably knew that already.
However, to answer your question, you don’t have to connect to all the OperationCard scenes in your main scene. When you connect to a signal, you are connecting to all of the instances of that scene in the main scene. So when any one of the 9 OperationCard scenes in the main scene is selected and emits its signal, the main script should pick it up, along with its unique ID.
Then, once the main script knows a card was selected and it knows its unique ID, it can do stuff like swap the card.

I hope this helps you!

I have created my scene by adding the object to it, not via code as an instance.

So i actually do have to connect to each scene entity to get the signal from it. If I only connect to one of them as below, I only get one signal printed out because only one is being received. it appears the singal is unique.

#connect to signals for selected
#connect emit_signal(“OC_pressed”, id, bSelected)
$NewOC1.connect(“OC_pressed”, self, “onCardSelected”)
#$NewOC2.connect(“OC_pressed”, self, “onCardSelected”)
#$NewOC3.connect(“OC_pressed”, self, “onCardSelected”)
#$NewOC4.connect(“OC_pressed”, self, “onCardSelected”)
#$NewOC5.connect(“OC_pressed”, self, “onCardSelected”)
#$OnDeckOC1.connect(“OC_pressed”, self, “onCardSelected”)
#$OnDeckOC2.connect(“OC_pressed”, self, “onCardSelected”)
#$OnDeckOC3.connect(“OC_pressed”, self, “onCardSelected”)

func onCardSelected(id, bSelected):
print ("Card ", id, bSelected)

JoeSeki | 2021-01-13 21:26