Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the virtual method and the enter tree virtual method. Let's begin. First let's take a look at virtual method now a virtual method allows subclasses of the base class type that has the virtual method to over ride the method for the purposes of the Godot application all a virtual method means is that its methods will be called if the node is active in the scene tree.
So this is a snippet of the node API. And as you notice under the methods of our node class we do in fact have for example the topic of this episode and torturing and right next to it it says virtual. And so when you look at the API is in Godot and you happen to see a method with the virtual tag or the word virtual what that means is that Godot will handle virtual method calls if they are overridden
And. If. The script happens to be attached to a node. Object that is active. In the. Scene tree and just to refresh your memory. This is our same tree when we are running our game application. As you can see the Node 2 D is attached to the root viewport which is handled by the same tree. Let's take a look at how to use the enter tree method. So first you use the function keyword f you and see followed by the words enter tree in snake case format followed by empty parentheses basically are enter tree does not take in a parameter and it is of type void.
Or rather it returns a void value which means that we do not have to return anything moving on. So the enter tree is a virtual method. As a matter of fact the node class and all its subclasses provides the virtual method called enter tree. And again virtual method means that we allow these subclasses of the base class type to override these methods. And so the node class and all its subclasses allow us to use the enter tree virtual method.
Now this is probably the most important thing you should take from this episode and that is that the enter tree method is always cold. Notice how I have that in yellow always code when scene. You can also think of it as a game object enters the scene tree and the enter tree method notifications are in a top to bottom order. So let's take a look at the top to bottom order. So as you can see here we have our root viewport AK are seeing tree and at the very top is our root node in our scene which is parent and it has two children.
Child one and Child 2 and Child 1 has another child. And in relation to the parent node this would be the grandchild. And so if we were to have a print statement attached into the enter tree method of each node listed here what we will print out to console was the following. So first we will enter the virtual method of the parent class. We will call it enter tree method and we will print to console and in this case it says parent has entered the scene true.
And then we go down because it's a top to bottom order. So after the parent we go to Child 1 and we enter the virtual method enter tree and we printed screen. And as you can see here. CHILD 1 has entered the same tree and we just keep going down the list so we will print from the grandchild and then after we will print child to last. So keep this in mind when you're running the virtual method and to treat as a matter of fact I'm going to upload the code onto get up so please feel free to download that and play around with the project.
Get a feeling and an understanding of how the virtual method entry works. Now the big question is when exactly do you use the enter tree method.
And it's a very specific case. However if you need property values to reset and or some type of action to happen immediately every time the node is active on the same tree then you found a good candidate for using the enter tree virtual method. Now keep in mind that the enter tree method is good for scenes that will be active and inactive multiple times throughout its lifetime. And of course to note on that your node will never be released from memory.
So we'll take a look at memory handling in later episodes over just keep in mind that when a scene or game object in Godot is active and switched to inactive. Just because you're seen as inactive does not mean it is deleted from memory.
Memory has to be handled manually by you the programmer. Let's go ahead and take a look at that. So we have our sentry at the top. We have a root viewport.
Everything above this line cannot be managed directly by you. The programmer over everything beneath it which is our scenes. We are able to manipulate and in this case we have a node that's active. However we can also make it inactive and so before are seen as loaded onto the same tree it is in an inactive state. When you press the play button you come from an inactive state to an active state and it is every time that you enter an active state the enter tree method is called. So keep that in mind let's do a quick example.
If you are inactive and then you get into active you call the enter tree method once and then lets say you make it inactive again and it's inactive for 10 seconds and then you decide to make it active again the enter tree is called again and in the nodes lifetime the enter tree method has been called twice. Now if you don't know if you should be using the enter tree method this most likely means you have no need of using it again. The enter tree is very specialized and by specialised I mean it is called Every time it becomes active on the scene.
In most cases your scenes or game objects will never be in an active and then inactive state unless you purposely want to do that. In which case the enter tree method is a perfect candidate. However most of the time you want to prioritize the ready virtual method over the enter tree virtual method and you especially want to prioritize the ready method when you want to set initial property values for your game object in this case. Think of the player health when your game starts you want to set the player health to an initial value.
Perhaps it's the integer value one hundred well you would do that in the ready function. You will not do that in the enter tree function unless for some reason and this is really up to you. But if you do want your player to be in an active and inactive state and every time it's an inactive state you would like to reset its health value to 100.
And yes you would in fact prioritize the enter tree method over the ready method. Thank you so much for joining me again. I'm going to upload the script or the enter tree method onto get up so please feel free to download that if you have any questions or comments. Please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day.