Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the scene tree. What probably confuses a lot of people beginning to program and could do is the idea of these scene treat.
And that's most likely because the name scene tree is very close to the named scene.
And now though they are similar and they interact with each other similarly they are quite different from one another. And in this episode we'll be taking a look at that. Now in the last episode we learned that a scene is a collection of notes your game world is a scene your different enemies can also be a scene. Your hero could be a scene your tiles could be a scene.
Basically anything good do that you want to do could be grouped inside a scene. However the scene tree is not a scene although a scene tree interacts with a scene. A scene tree is not. A scene. A scene tree
Or rather the scene. Tree is a class. As a matter of fact it's one of the most important class. Outside of the West class. Now the same tree in good though is the default. Came loop that is provided to the
Operating System class. Now this is a cool feature about good dope that you probably may or may not. Already know and that is that good deal allows you to supply it with a game loop. That means you could create your own custom game loop that you could give to the good dough to run. However in many many cases you most likely will not or do not need to create your own game loop and therefore good dough provides you with a default game loop and that default game loop is provided to you by the same tree class.
Now these same tree class is completely useless by itself it's a matter of fact you can run the game with the same treat class alone. However your game's not going to do anything without a scene. The scene treat class will not do anything.
Now to recap a scene needs a root note and that root no needs to inherit from the node class at minimum. In order for you the programmer to have a functioning game basically if you want something that's not just a blank screen you need to provide a scene.
And the most important thing is that the same tree is basically a singleton.
There is only one scene. Tree instanced object that you are able to use the methods for.
So let's go ahead and take a look at this scene tree. Let's take a look at how our game operates at a global scale.
So here is our scene tree. It's at the top. Now what's above the scene. Tree is the operating system class. Or scene. Tree actually has a root and this root is called the viewport. We won't have to worry about viewport just yet. However just note that the viewport. Is what. Allows us to draw to the screen basically the viewport is. Our
Window into our game world. Now. Everything above this line cannot be accessed. We cannot remove the same treat class nor can we remove our root class again which is the viewport class the main viewport class.
Now below this line are basically our nodes that we can add and remove.
So you can see here a node is in active and we can make it active. However I want you to think of this a little differently. I
I want you to think of this as a scene.
We can add scenes and we can remove scenes from our root. Again the root not to be confused with the root node but rather the root
Viewport also referred to as the main viewport which again handles what we see on the screen. Now
Now let's go ahead and take a look at a example. So in our scene we just have one root node and as a matter of fact we can see we have a scene and we have a Node 2 D. This would be called our root node because as you notice it has no children. So our scene has a root node and when we play our game. And we've chosen. That seemed to be our. Main scene. That we get to see when we press the play button. When you click remote you'll notice that we have this object up here called Root and root.
Happens to have something attached to it.
And yes it's no 2D it happens to be our scenes root node so our scenes root node is attached to root. And this is what you would see when you run your game and as a matter of fact if you have children's. And other scenes
It's all going to go underneath root. Now let's delete our scene and we won't go over deleting scenes in this. Episode ever. Let's just take a look. At what. Our scene tree looks like. So when we run a game
We have code that basically deletes this scene from the scene tree and because we delete the scene from the scene tree. Notice how
Our root is still here but nothing's attached to it. That means that our scene tree is free to add. Another scene. Onto our scene tree. So when we start the game. Our game is going to look like this. However once we get to the code that deletes our current scene and as a matter of fact this game only has one scene. What it does is it removes itself from the root. And now our root is free to have other scenes added to it. And this is the concept that I wanted to show you
Is that we have a scene tree. We have a root viewport. And underneath that we are able to attach and detach scenes and nodes and as long as you can get that concept you'll find that it will be easier in your programming journey to get into changing scenes or having more than one scene. Basically you have the freedom and flexibility to do a lot with scenes once you get into the habit. Or at least the basic understanding that you can have multiple scenes
Inside your scene. Trick. That your game won't crash or do anything funny if you remove your scenes properly. So on the previous slide we saw that when we deleted our scene we were deleting everything from the root viewport and from previous slides we saw that the scene tree was on top of the root viewport. So let's go ahead and take a brief and quick look at the root viewport you root viewport is always at the top of your scenes on top of that your nodes will always add itself to your root viewport as a child. Also
Also one thing to keep in mind is that the root viewport is basically your main viewport. It's basically your window to the game world now because of that anything that is a child of the viewport
Is drawn inside of the root viewport by default. Now if we take a moment to think about that that means that if the root viewport is not the node at the top of your scenes nothing will be seen on the player screen. That's why the root viewport is at the top. And. Everything else is a child of the root viewport.
And again we'll be taking a look at view ports in a future episode but again just know that the root viewport is our window into the game world. Without it we will not see anything in the game. And lastly the root viewport cannot be created manually by the programmer. The root viewport is created automatically by the scene treat class. Now. Again I just want to reiterate because of how the same tree works with our scenes. We are able to have games that are
Quite flexible. We have quite a lot of freedom and how to present games and how to manage scenes.
And as a matter of fact, That's basically it. So thank you for joining me and 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. I look forward to seeing you in the next episode. Have an amazing day