Hello and welcome to another episode in the Godot to basics tutorial series. In this episode we will be taking a look at the ready virtual method the ready virtual method can be used by any class that inherits from the node class since the ready method is a virtual method. The ready virtual method gets called in our game loop when a node is added to the scene tree. If a node has children that children node will have their ready function triggered first. And this works its way up to the parent node one thing to keep in mind is that the root node of your scene will have its ready virtual method called lest the most important takeaway to get from this episode is that the ready virtual method is called only once.
It will not be called ever again. This means that removing a node from the same tree and then adding it back onto the scene tree will not call the ready virtual method as it's already been called when it was first initialized onto the scene tree to use the ready virtual method. It's fairly straightforward. First you use the function keyword f you and see followed by an underscore followed by the word ready and then followed by empty parentheses the ready virtual method does not take in a parameter nor does it return back of value.
Let's go ahead and take a look at how the ready virtual method calls all of the nodes that are in the scene. So in this example we have a fairly straightforward scene. We have one root node and for the root node we have two children. We have child 1 and Child 2. CHILD 1 also has two children grandchild one and a node called another grandchild one and grandchild one also has a child and we call it great grandchild. One most important thing to note is that everything in the first child has the number one appended at the end. Now
Now the second child only has one child called grandchild 2 and everything on the bottom has the number 2. So keep that in mind let's go ahead and take a look at an example. So as you can see here I have the exact same scene from the less slide and each node in this case each node to day has a script and all it does is it prints to the screen the name of the node followed by ready method is code so we can see the order that the ready virtual method is called in now the simplest way to understand what's going to get called as a beginner is the following.
First you look at the root node and you ask two questions first is does the root node have children. And if the answer is yes yes. Have all the children had their ready virtual method code. And in this case the answer is No. So we move down and we say the same two questions. Does Child 1 have children. The answer is yes. And then you ask has the children of Child 1 had their virtual ready method called. And the answer is No. And you move down and you basically repeat the question again we ask does the grandchild one have children. The answer is yes and then you ask have the children had their virtual ready method called.
And in this case the answer is No. So you move down and you ask the same question Do you have any children. Since the answer is no we go ahead and we call the virtual ready method. And in this case it just prints and it says great grandchild one ready method is called we move up again and we ask the same two questions. Does this node have any children. The answer is yes. However with the second question we ask have all the nodes of the children had their ready virtual method code. And since the answer is yes we go ahead and we call the ready virtual method. In this case it's another print statement.
So grandchild one ready method is called and then we basically keep going in that order. And so we call another grandchild one. It prints it's ready or rather it prints to the console another grandchild one ready method is called and then we go on to child one and we call child's one virtual ready method. We move down and we ask the same questions we keep going down and we call grandchild to be called child 2. And then last but not least root method. So in a sense even though we're going top to bottom and I mean chronologically we look at our first child and then we look at their first child and then we look at their first child and then we just call in that top to bottom order.
It's sort of working in a sense horizontally because even though we're not going straight down or we're not going straight up we are checking to see that while going from top to bottom order as a matter of fact it makes more sense if you play around with the file. So I'm gonna go ahead and upload this to get up so please feel free to download that and move the children around so you can see for yourself how it behaves because it's harder to explain but when you see it happening and you see the print statements going to console you will in a sense understand better the order of call for the ready virtual method however moving on at the end of our ready function already virtual method at the end I have deleted child one so all of this will disappear and we will look at that now.
So I went ahead and deleted the children of child 1 and you noticed that in our same tree it's no longer there in a sense or child one and all of its children are now in active but they have not been deleted from memory so keep that in mind just because a node is inactive and removed from the scene treat. Does not mean that the node has been removed from memory. So now our root viewport only has our root. Followed by Child 2 and grandchild 2. And now we're going to add back child 1 and we're gonna see if the ready virtual method gets called again.
So in the code that you can download from get a child one is now added back onto the scene tree. Notice how it is beneath child 2. We'll take a look at this at later episodes. However as we added Child 1 onto the scene through or console hasn't changed even though the code is still there even though the ready virtual method has a print statement. Notice how the print statement has not been caught and that's because since the ready virtual method has already been called it will not be called again. Now if you do download the file from GitHub I did add one more line of code to move our child.
Just in case you're curious about how to move children after they've been added to the scene. And again even though we've moved our child from index 1 to index 0 already virtual methods still has not been called. We'll look more into moving or child or children nodes in a later episode. So when exactly do we use the virtual method ready. Well we use the ready virtual method when we want to initialize default property values. Once when our object is created and added to a scene treat. This would include for example our default player health point items.
The list goes on. However in most cases we only need to set our default values once and never again. And for that reason the ready virtual method is the perfect candidate. Now if you do need to set or reset property values or call method every time something has been inactivated and activated you'll look into enter tree of course for deactivated. You'll be looking into exit tree virtual method but we'll look into that in a later episode. And in this case I'm talking about the exit tree virtual method the enter tree virtual method has already been talked about in the last episode.
So if you haven't checked that out please feel free to go ahead and check that out. As a beginner you most likely won't have a need to use the enter tree virtual method so you should be looking into using the ready virtual method at least 90 percent of the time and then the 10 percent of times that you find yourself needing an extra use case go ahead and look at the enter tree virtual method. 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 leave them in the comments section down below. One last thing.
Don't forget to download the github file and play around with the node orders on the scene tree so you can get a feel and look at what gets called when. Again thank you so much.
Have an amazing day.