Reaching a child of an instance in a var

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

Say there’s a function that instances a scene into the main scene.

func shoot(args):
    var projectile = preload(path).instance()
    projectile.position = (formula for calculating position)
    projectile.velocity = (formula for calculating velocity)
    $background.add_child(projectile)

however, i also need to reach a child of the projectile node, Area2D, to connect its body entered signal to the main scene. Is there a way to do it before adding the child, or do i have to do it after using $background.get_node()?

:bust_in_silhouette: Reply From: cascas

After you instantiated the projectile you can do it whenever you want really.
Like this for example:

func shoot(args):

    var projectile = preload(path).instance()
    
    #connect the signal
    projectile.get_node("path to Area2D").connect("singal", object, "function name")

    projectile.position = (formula for calculating position)

    projectile.velocity = (formula for calculating velocity)

    $background.addchild(projectile)

Thanks! I’m really stupid that i didn’t think of using get_node() on the var.

mymokol | 2021-01-23 14:42