All Questions

Community

V
Rogers Vizcaino

Init vs Ready

Hi, what's the difference between Init and Ready functions?

  • Daniel Queiroz Porto replied
    Solution

    _init() is called when the object is created, so whenever you do for example

    Node2D.new() 
    

    the function _init() will be run.

    While _ready() is called after the node enter's the tree and all it's dependencies have been loaded. In other words, when it's "ready" to be used by the script or another part of the game. So let's say, you created a new Sprite node and you will add it to the scene through code:

    var sprite = Sprite.new() #_init() will be run here
    add_child(sprite) #_ready() will run after this
    

    I think in the case where you have a scene loaded and you create an instance of it, it will run _init if your scene script has it:

    some_loaded_scene.instance() #will trigger _init in your scene
    

    I think that's the gist of it, or at least how I've understood it from using so far.

  • V
    Rogers Vizcaino replied

    Thank you so much Daniel! Where are you from? The name sounds like colombian maybe? Or spanish?

  • Daniel Queiroz Porto replied

    I'm from Brazil!

  • M
    Christopher Marsh replied

    I know someone from Brazil that codes as well, he is learning english at the moment. Thanks as well for desribing the init, as the instructor also did a fabulous job for those of visual learners