Could someone explain it to me like I'm 5 on why I would use onready over var? Maybe with a nice little metaphor. Tutorial I was following kind of glossed over this.

I read the documentation (yay me) but still didn't quite understand it. I get that it's being loaded immediately at the start of the script or something but I would assume that's how it always works though.

And while I'm asking for clarification, what is the benefit of assigning a class name to my script? Thank you

    IndustryStandard

    In case it helps, this is the full player script

    I just don't fully understand what we were doing or I forgot, it's been a bit over a week and I've had a rough couple days.

    IndustryStandard Could someone explain it to me like I'm 5 on why I would use onready over var? Maybe with a nice little metaphor.

    @onready var foo = 42 is basically just syntactic sugar for:

    var foo
    
    func _ready():
        foo = 42

    It's just shorter and more readable.

    Now why would you want to do something like this? Simply because you cannot always assign something while the script is loading and you have to wait until it's ready.

    You can assign simple, constant values at script load time like var foo = 42 without a problem. But you cannot write something like this: var camera_3d = $Camera3D (note the missing @onready) At script load time the node "Camera3D" doesn't exist yet! Therefore you have to wait until it exists, which is when your script is ready. Because at that point in time all its children have been initialized and are ready as well.

    So to solve this problem you would write something like this:

    var camera_3d
    
    func _ready():
        camera_3d = $Camera3D

    Or in short: @onready var camera_3d = $Camera3D

    To give an analogy think of it like this:

    • You want to play a game that isn't installed yet.
    • You open Steam and your see the name of the game in your library. This is the equivalent to script load time where you "see" the name of the variable itself but there is no value behind it. Just like there is no game (or game files) behind the name in your games list because it's not installed yet.
    • You install the game. Once it's downloaded it is ready.
    • And now Steam is ready to play your game because now there is an installed game behind that name in your games library.

      Toxe

      So, is there a tradeoff to doing this then? I imagine it'd be a performance issue or something, like you can only have so many games downloaded at once.

        IndustryStandard @ready just determines when the initialization happens

        var enemy = $enemy

        ^ Happens when node is instantiated.

        @onready var enemy = $enemy

        ^ Happens when node is added to the scene tree

        Note the important difference between node creation and node addition to the scene tree. Nodes are created and initialized before they are added to the scene tree. Using @onready postpones the initialization of the variable until the node is added to the scene tree.

        This is particularly useful when you want to initialize a variable by something that's in the scene tree, like a reference to a child via a node path. Doing it with @onready will assure that the child is actually there.

        Neatorino fellas, I think it'll all start making more sense overtime when I'm writing my own stuff more than following someone elses code.

        Ok, I guess I have a noob question to go with this, does the on-init or on-ready eat up extra memory?or in computer time its all so fast its negligible ?

        • Toxe replied to this.

          udiosd Unless you constantly create a lot of nodes then you don't need to worry about it. Or: when in doubt always profile and measure. Also there isn't really a choice because if you need to initialize objects after they have been created then, well, you have to do it one way or the other and there is no way around it.