For variables

We store the path of a scene into a variable, like:

const pickup_heart_scene := preload("PickupHeart.tscn")

However, what is the purpose of creating another variable to represent the same thing? Like:

     var heart_pickup := pickup_heart_scene.instance()


Can't we use the variable pickup_heart_scene? Instead of the variable listed above:



  • Nathan Lovato replied

    Can't we use the variable pickup_heart_scene?

    No, we can't. If you try, you'll immediately get an error.

    The code preload("PickupHeart.tscn") loads what Godot calls a "packed scene file" in the computer's memory. This is not a node or a scene instance, and it doesn't provide the same functions and properties.

    It is only a template to make concrete instances of the pickup scene. It doesn't have the add_child() function that we need to add a child label (or any other node).

    So this is not the same thing as the packed scene:

    var heart_pickup := pickup_heart_scene.instance()

    It is one concrete heart pickup, one instance (reproduction) of the template described by the packed scene.

    Among other things, the instance gives you access to the add_child() function, which allows us to add nodes as children of this one instance (and not any other instance of the heart pickup). You can also change its position, rotation, scale (same, of this instance only, not any other instance).

    Does this make sense?  I hope it helps to see there's a difference between a packed scene, which is just a template you can create instances from, and concrete instances, which you can move and do things to.

    1 love