How to send a value to an instantiated scene script with custom signals

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

I followed a tutorial some time ago about a dash where you create a trail effect by instantiating a PackedScene containing a Sprite2D node. This has it’s own script and in my player script, I get the input direction of the dash. But to change the frame, I need to send a frame value from my to this script called So this is usually done with signals afaik but I haven’t done a whole lot with custom signals and I think this is where my problem lies.

In my script I have:

@export var dashobject : PackedScene
signal send_frame(frame : int) 

func _ready():

func _physics_process(delta):
    if dash_dir == Vector2(1, -1):

func _on_send_frame(frame : int):
    return frame(don't know if this is necessary)

I expect I can do this in reverse by adding and connecting the signal in the dashobject rather than the player but there’s something about the code that wrong that I just can’t see. Also I don’t really understand how to use the value received from the signal in the dashobject script. If there’s another method to achieve the same result, I’d be happy to hear it. Thanks in advance!

:bust_in_silhouette: Reply From: zhyrin

The object that gives a value to the other objects has the signal defined.
All listener objects have a function that takes the same parameters the signal emits, and they are connected to it.

Note: you would not connect to a PackedScene, a packed scene describes how to create a scene. You would connect to the instantiated scene. The instantiated scene (root) has to have a custom script that will handle the signal

# your
@export var dashobject_scene: PackedScene # I assume the root has the script ""
signal send_frame(frame: int)

func create_dash_object() -> void:
    var dashobject := dashobject_scene.instantiate()
    add_child(dashobject) # you might want to place it in the scene tree differently

func _physics_process(delta: float) -> void:

# your
func on_send_frame(frame: int) -> void:
    print("I received frame ", frame)