Search code examples
lifecyclegodotgdscript

I am missing a lifecycle initialization method after _ready()


Disclaimer: new in Godot here. Some experience with Unity

I am doing the initialization of my scripts into the _ready() method.

But I am missing a method that is automatically called on each script after all the _ready() methods have finished for all the Nodes in the scene. I understand that this is when all the tree has been loaded.

The reason is that in the _ready() method I initialize the actual script's internal state. But if I need to make some initialization, adjustments, and setup, into the scripts referenced by this one I need to be sure I do them after the _ready() method has finished on all that objects.

I see that this is the lifecycle of scripts in Godot:

This is the same in Unity:

As I am understanding the Godot's _ready() is equivalent to the Awake() in Unity.

I am missing in Godot what would be the equivalent of Start() in Unity. Which is called after all the objects have been initialized.

Does this method exist? Is there a workaround?


Solution

  • You can do your initialization in a function called do_setup and then call this function deferred in the next frame by doing: call_deferred("do_setup") If you do this in _ready the next frame will be the first frame of the game.

    Like so:

    func _ready():
        call_deferred("do_setup")
        
    func do_setup():
        # Do initialization here
    

    Docs: https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred