Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at adding and removing nodes from the scene true. Let's start with the
Pseudocode of adding and removing nodes from the scene.
So to add a node unto the scene true or we have to use as the add underscored child method and it takes in a single parameter which is a node object.
The same is true for removing a node from the same tree as well to remove a node from the same tree we use the remove underscored child method followed by a single parameter which is just a node object that is attached to the scene.
Treat a very important thing to keep in mind is that removing an object or a node from the same tree does not remove the node or object from memory.
As we briefly talked about in the last episode and a little more in detail in the G.D. script fundamental tutorial series I explained that you need to manually remove objects and nodes from the same tree and to remove an object.
You use the free method to remove a node we use the Q underscore free method or at least it is preferred for a node to use the Q free method. Some basic terminology. When a node is added to the scene tree it is called an active node. Now when we remove a node from the same tree it is referred to as an orphaned node. As long as it's staying in memory so keep that in mind.
A node is an orphan node when removed from the same tree but not deleted from memory.
In this episode I'm going to do something a little different I'm going to I'm going to walk through code with you and take a look at the debugger so we can see memory grow based on certain code. In the example I'm gonna be using a timer node I'm gonna add a timer node onto the same tree the timer node will do absolutely nothing. One thing to keep in mind is that the Timer class inherits from the node class and Node class inherits from the Object class. So we are free to use the Q free method along with the free method depending on circumstances.
However in the example I'll just be using the Q free method. Let's get right to it in this example we will be going through four different methods in the first method. We will be looking at just adding objects or in this case our time or node onto the same tree in the second function. We'll be looking at adding a timer node and removing a timer node and seeing through the debugger how our memory looks in the third example we'll be looking at the same thing adding and removing the timer node from the same tree.
But on top of that we will be running the Q free method directly. And lastly we will be looking at the exact same thing of adding and removing a timer node Except we'll be attaching a script onto it and seeing how our node behaves with a script with only the exit tree virtual method being used.
And our code will be run inside the process for a trial method so we can see our code being continually added and continually removed as fast as possible so we can see the memory grow.
I'm gonna go ahead and open the debugger.
I'm gonna click for the first example.
We're gonna look at nodes and we're gonna look at memory. So in the first example we'll just be adding our timer node onto the scene tree on every possible frame.
Because we're running in the process method we're trying to call this code as fast as possible. So let's begin.
First let's look at the remote as you can see here are local was just a route. But now that our code is running. Look at all these Tamara nodes we're adding onto the scene. Tree.
Now this is a rudimentary example but I just wanted to show you that. Well for one thing. Adding a child will have performance issues. And so if we go to monitors look at our node
It keeps growing. We're at 2000 we were at. Basically. 1 node. Or for example a total of 2 nodes if you count the root viewport over now we're at 2600 nodes on our scene tree. And on top of that our memory has grown. But as you can see here it just keeps growing. And that's what happens when we just. Add a child. We're not removing anything in this example. So let me go ahead and. Put a comment here. So we can see what we're working with and the base stats.
So if we take a look at our remote again you can see that we just have two nodes. A root viewport. And our No 2 D which we call root. And if we go to monitor. And if we go down. We only have two nodes. Now there is one thing I want to show you real quick we're gonna run this code again real fast. I just want to point out something in that is. That. As our code
Keeps adding timer's onto the scene treat. Notice how are nodes increases but aren't. Orphan nodes is at zero. That's because we haven't removed anything from this scene. True.
So let's go ahead and take a look at that. So I'm gonna add the second code in the process method and if we take a look here. You'll notice that the first two lines are the exact same we're. Creating a variable. To hold our timer node. Object. And then on the second line of code we're adding the timer node onto the. Scene tree. Just like our first example. However in this example we're doing or rather we're adding one more thing we're going to remove. Our timer node. From this scene. Tree
Let's go ahead and take a look at that
If we go to remote. Notice how we only have two nodes. A root. Viewport. And are. No two D. And you may be thinking well perfect. I'm probably keeping my memory low and that is not true. I'm gonna show you that now. So as you can see here are nodes is still it to. However remember when you remove a node from the same tree it becomes an. Orphaned. Node. So. Notice here. Are orphan nodes is increasing.
And that's not a good thing. It's a matter of fact if we come back to memory you can see our memory is still
Growing. And this is a problem that many beginners face and. I will admit. I. Also face this problem because in. Other. Frameworks or. Other languages the memory management is done for you however Godot is giving you full control over how to remove something or rather in Godot you have to manually manage memory yourself. So just keep that in mind. Just because you are removing. Something. From the scene tree. Does not mean
That you are deleting it from memory. So let's go ahead and take a look at the third function and the third function. We're going to create a. Timer node. We're going to instantiate a timer node
Then we're going to add our timer node onto the scene tree we will then delete it. It's exactly the same three lines of code as our second function.
However the only difference is we're going to explicitly tell our timer node object to remove itself from memory when it is able to with the Q free method.
So I'm gonna go ahead. Gonna
Change the code in the process save that and we're going to run the code. Let's take a look at the remote. And notice how. We only have two nodes on the. Scene. Tree.
And if we go to the bugger monitor notice how are orphan nodes is zero. And that's because we are explicitly removing or Tamara node object from memory.
And if you notice here and nodes are only to the root viewport and the route node 2 day
and if we go to memory notice our memory isn't changing. Now you may be thinking what exactly is going
On with our code and we're doing the same process as our second function. However because we're removing it from memory you can see your memory is not getting affected.
And I'm going to show you in slow motion what that looks like. So I'm gonna go ahead and insert and you can only use the free method in this example you can't do it in the fourth example I'm about to show you later. But I just want to show you something. So. We're gonna go ahead and. Do something called filler. Right because. I'm going to actually show you through a line break worry. Break Point. What exactly is going on behind the scenes with the Q frame. However it's easier to show you if we use the free method
So
We're gonna add a breakpoint here and we're gonna go line by line and we're gonna take a look at how it looks like in memory. So as you can see here the most important thing is our timer node. Saw a timer node. Is No it hasn't been instantiated yet however we're calling the line that instantiate and when it does
Notice how we have an object idea. Basically we are. Existing in memory at this point in time ever if we keep going down. And we remove the node from the same tree. Notice. How we're still here we still exist in memory yet. We just finished. The remove child call. It's not until this line of code here when we. Explicitly free from memory. I do not recommend using the free method but in this example. Will be easier to show you what's happening in code but don't do it. Now when we run the free method
We're gonna. Actually change this to no. And that's basically what we're doing. We went from having some object I.D.. Basically a pointer in memory. And we just deleted that. And now that. Memory is gone. And it is removed not only from the same tree but removed from memory. And that's what Q. free method does. If you tried to do this with Q Free you're not going to. See it because basically Godot will handle that for you. And that's what I wanted to show you. By the way I'm gonna upload this to get up so please feel free to play around with it.
Now Lesley. We're going to test the fourth function
Which is adding or removing a timer with a script. Now in the last episode we dealt with the exit tree. Virtual method. So if we take a look at our. Exit treat timer This is the code we're
Going to attach to our Tamara. Class when it's instantiated. It's a very simple. Function.
All it is is the exit tree which gets called when we are removing our node from the same tree. But this is the most important part. It is what I was trying to teach you in the last episode and that is we are going to remove our self from memory. So again keep in mind it's the cue free method. If you use the free method here
You're going to basically cause an error.
However what's happening underneath is not important at this time just know that when you're using a node we don't use the free virtual method that the object class passes to us. Instead we use the Q free method so I'm gonna change that back to Q Free. So again our timer script
Only has one function and we're removing ourselves from memory. Now
Now let's go ahead and break down the fourth example. Now we haven't changed anything really. We're still creating an object in this case the timer node. We're still. Adding it
And we're removing it from the scene tree. However we are not explicitly. Going to call the Q free like we did in the third example. Instead what we're going to do is we're going to attach a script to it. And we're going to let the script. Deal with memory management. Because in the. Script we're calling the Q free method inside exit tree. And exit tree gets called when we remove a node from this scene true. And so. In order to attach a script. I'm going to create an episode. That deals with that but for now just know that these two lines of code will attach
Our script. Unto
Our newly created node. So let's go ahead and run it
So. You can see here. We have no orphan nodes so that's good. And on top of that we only have to node, which is also good. And on top of that. Our memory is not. Growing. And so in a sense that is a win for us. Because one we don't have to explicitly in our example. We do not have to explicitly. Call the Q free. Like we did in the third example instead we let the script handle that. With the Q free over here and that makes it easier for us as programmers because when we're dealing with a class weed we don't really want to worry if we're calling the Q Free instead we let
The script
Deal with that. And this is the important reason why we have the exit tree and the Q free method.
So as a beginner you should be aiming to use this block of code in order to help you because of the problems we face with memory management. Now that's basically it for this episode. Thank
Thank you so much for joining me. Thank you for. Subscribing and thank you for clicking the like button if you have any questions or comments please feel free to leave them in the comments. Section down below. Now don't worry about. Adding scripts to node classes and seeing tree. I'm gonna go over that in a. Future episode. Also I'm going to. Upload this code onto get hub. So look in the description down below. Go ahead and download this file and play around. Try to understand for yourself how memory grows and how we can deal with
Memory growth. And then the next episode we'll be taking a look at memory. Management theory
At least the basics. Again thank you for joining me in this episode. I look forward to seeing you in the next. Have An amazing day.