Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at script life cycles how most game engines need to run scripts on two game objects in games. Our game objects such as life have a time to be born to flourish and then to perish. Let's go ahead and take a look at that. So no matter which game engine you decide to work with it will have some sort of script lifecycle.
Now the basics are that every game object has a point in time where it is created. It has a point in time where it exists and then it has a point in time when it is destroyed. And that's basically it. Some game objects you would like to spawn for example and so the process of spawning and in those specifically the process of adding a scene onto the root viewport in a sense would be the creation of your game object where in that specific case the creation of your scene and nodes and then when your enemy is on the screen you would like it to do something.
So for example maybe you wanted to move towards the player position and so you would call some type of method inside a function that gets called repeatedly such as the physics virtual method and then when your player or hero defeats that enemy you would like to remove it from the scene. And that's one example of the process of in this case our enemy. Game Object or rather that is the lifecycle of our enemy Game Object. In Godot we have functions that help us with our script and life cycles or rather our game object life cycles.
Let's go ahead and take a look at that. So first a scene is loaded from disk or created by a script. After the root node of the newly instantiated scene is added as a child of the root viewport or to any child of it next. Every node of the newly added scene will receive the enter tree notification and the order of the nodes that get notified. Start from the top. So the root Node and it works its way towards the bottom in that order.
After the ready notification is called When a node and all its children are inside the active scene. And so the ready notification goes from a bottom to top order because the root node before it gets its ready notification must wait for its children to first get their ready notification. Now while your game object is active in the scene callbacks such as the input virtual method the process virtual method and the physics process virtual method are called if your game object is using them.
Finally when a scene is removed all nodes receive the exit scene notification in bottom to top order. So the route known in your scene has to wait for all its children to run its exit scene. Virtual methods before the root node can run its own exit scene. Function now which classes allow for script life cycles and basically any class that inherits from the node class. So if you remember in the previous episode called Node and resources we took a look at the different types of nodes we could create at the very top.
The first choice was node and this makes sense now. The node class provides the virtual functions we can replace and use to write code during certain steps in the game objects life cycle. Now I'm gonna make an episode on each individual life cycle that we have available and actually I listed them in order of what gets called first. So first we call the enter tree virtual method and then after we call the virtual ready function and then we call virtual input if there is player input.
After this is important first we call physics processing and then we call process. Finally when we are ready to delete our scene or node we call the virtual exit tree method. And this is the order that are virtual methods that deal with our lifecycle stages get called in. So I'm gonna go ahead and upload to get up a project. Very basic. It will have all the virtual methods listed here. So it will have all six and I will have some special function that will delete our root node from scene so we can get the exit tree.
And from there it will just print out to screen what was called and you will see the order and it will be exactly that. It will be enter tree then ready then input then physics processing then process then exit tree for input. You actually have to press a button or move your mouse after you hit the play button so the input method gets called. Because if you don't exit tree gets called and then input will not receive anything because there is no game object to receive input and so my advice to you is if you do download the github project and you do want to see it for yourself.
After you press play just go ahead and move your mouse back and forth really fast so the input gets called and you're gonna see that it is in this exact order. That's a matter of fact I went ahead and ran my own code so I could take a picture and this is what you should see if you do in fact download the get hub file. So as you can see here entry is called first. Then our ready method is called then our inputs called if you're moving your mouse or pressing some type of keyboard at least with the code I have after an inputs received physics process is called then process is called.
And lastly when your node is exiting the tree the exit tree virtual method is called less and when exit tree is called your scripts will not work well that's all I have for you in this episode. Thank you so much for joining me. If you have any questions or comments please feel free to post them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day.