How do you make a duplicate that can process signals separate from the original copy?

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

I’m working on a 2D game and trying to make a body that can be interacted with. I have managed to connect signals for body_entered and I’m able to store and print the name of the body it entered. Then when pressing the q button it emits a signal carrying that name with the bodies listening for it.

The problem I’m having is when the signal is received only the script attached to the original copy of the body that was duplicated is processing the signal. I can see this by printing the name of the body when processing the signal. How do I make all of the duplicates receive the signal?

I’m trying to compare the names so it only updates the variable on that specific copy of the body.

I’ve tried detaching and reattaching the script after duplicating the body, with no luck. I might try restructuring my code to use a signal bus, but I don’t think that will fix my problem.

Any advise about this is appreciated!

Sorry i cannot understand your problem. Try paste some code and fix your question (You are 2d game?)

Moreus | 2023-04-08 07:16

Sorry, I got focused on getting the info typed and missed some grammar. I reworded the question and a bit of the body.

The relevant code snipet on the player is just:

if Input.is_action_pressed(“treat”):
emit_signal(“heal”, touching)

“touching” holds the name of the body the player is touching.

The code on the duplicated body is just a function connected to the “heal” signal called:

func _on_Player_heal(name_healed)

I’m seeing prints from inside of the “_on_Player_heal” function being output, so I know the input is setup correctly, the signal is emitting and is connected to the script. However, the prints output the name of the body that the script is attached to and they only print the current name of the original body instead of having multiple prints with each duplicate’s name as well.

dmpad | 2023-04-08 15:14

How are you duplicating the object? Are you using [yourobject].duplicate()? Are you instancing a new object using load(“something”).instance()?

In all cases, I think you need to connect the signal handler on the new object just as you did on the original object. If you have connected signals on the original object, but not on the new object, then it makes sense that only the original object’s signals are being handled.

stormreaver | 2023-04-08 15:33

I’m currently just right clicking the body in the scene viewer and selecting “duplicate” to create a copy of the body with the script and children attached. I connected the signal from the player script to the body’s script using Godot’s gui. Since both the original and the duplicate use the same script I’m not sure how I would connect it that way again.

The closest way, I can think of, to try your suggestion with how it is connected would be to have the script re-initialize the signal connection on start. Do you know if there is a way to do this?

dmpad | 2023-04-08 15:52

I was assuming you were creating your copies via GDScript. Duplicating via the editor should also duplicate all the signals (I do that very thing in one of my current projects).

Perhaps you could post a minimal project that demonstrates the problem? Abstract theory is not one of my strengths.

stormreaver | 2023-04-08 16:22