Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the exit tree virtual method. So the exit tree virtual method is the last virtual method in our life cycle script. As a matter of fact all cold logic inside the exit tree method gets called when the node is removed from this scene. Trick as you can see here we use the function keyword if you can see followed by exit tree.
And as you can notice it takes in no arguments and it does not return back of value. Now we can do more complex things inside the exit tree for example when we want to notify other code or nodes that are specific node is leaving the scene tree and we can also store our node that is leaving the scene tree in an array. Again these are more complex topics. However as a beginner you should be looking at deleting the node rather than for example storing it an array.
So again the easiest thing to do as a beginner is to delete the node from memory after it has left the scene true. So why exactly are we deleting or nodes from memory. Well just because a node leaves the same tree does not mean that the node or object removes itself from memory. You must manually manage memory your self and the best way to do that is through the exit tree. Or a troll method. So Godot offers us two ways of deleting our nodes from memory. We have the queue free method that we get when we inherit from the node class and we have the free method that we inherit from the object class we talked about this in the script fundamental tutorial series but let's quickly go over these two as a refresher.
So the queue free method basically cues our node for deletion at the end of the current friend. One thing to note is that with the queue free method when deleted or children nodes will be deleted. This method makes sure that it is ok to delete the node and safer to use than using the free method that we get from the object class. It's because of this safety measure that queue Free has that the queue free method is the preferred method when deleting nodes from memory. Now the free method that we get from the object class deletes the object from memory any pre-existing reference to the free object will become invalid.
This may cause issues if you don't know what you're doing and so it is best not to use the free method. Now when exactly do we use the queue free method and as a beginner you should default code inside the exit tree method with the queue free method. As a matter of fact you should be doing that for all nodes and the simple reason for this is memory management. Memory management is a complex topic. However it's best to start by deleting everything that leaves the scene. Tree. It's best to make sure that we do not have memory leaks.
And if you don't delete your nodes from memory every time they leave the scene tree and you keep adding nodes your memory will grow. And that's basically what we call a memory leak. And it does cause issues. So just keep that in mind as a matter of fact it's very simple to delete your node from memory. So you have your function you have your exit tree virtual method and all you have to do is call the queue free method. I added the self keyword just to show you through code that we are referring to our current node and that we would like our current node to call its queue free method.
And that's basically it. And this example is assuming that we haven't attached anything else to our node. Now on top of that this example doesn't do anything until we add and remove nodes. And so we will be taking a look at adding or removing nodes in the next episode. Thank you so much for joining me. I hope you learned a lot. If you have any questions or comments please feel free to leave them in the comments section down below. Thank you for subscribing and thank you for clicking the like button. I look forward to seeing you in the next episode.
Have an amazing day.