 When you're making a game with Godot, you're going to compose it of a bunch of things called scenes scenes are a tree of nodes Which contain your game UI and objects and such It's so common that you're going to need to switch between these scenes But it's not immediately apparent when you start using the engine how to do so So let's just get right into it and talk about scene switching and Godot I've got a node 2d scene here. We're gonna call it level one. I'm just gonna add a label So that we can visually distinguish between them. I'm gonna call it level one and we'll just make it a Bit bigger here so we can see it So that's level one. We'll save this as a level one scene and run our game Now if you had a game like a platformer or some kind of game and you got to the end of the level You might want to switch the scene to level two or maybe you have a main menu where you can choose Which scenes you switch to? So let's go ahead and we'll duplicate this In the file system and we'll call it level two. We'll open level two as a separate scene So now you see level one here level two. Let's rename the root node here and let's change our label here and Now if we just run Just level two scene we have a little to let's make it so we can switch between level one and level two there's a few ways to go about this and they have different pros and cons and there's like an ultimate kind of like Fancy pants version we're gonna build at the end. So let's start with the first step We'll do the second step and then we'll do the fancy pants version. Okay, so we're back in level one Let's add a button and I don't know if you would have a button in your game. You might Might be in gameplay. It might be a button. It really just depends on what your game needs So we're gonna have this button switch to level two and regardless of how you do it You're gonna have to regardless of what your game needs. You're gonna have to do it in a script and So let's attach a script to the root node here We'll just clear this stuff out because we don't need it right now Back in our inspector click the button Let's rename it to level two button just because that'll give us a nice Naming and we're gonna click Into signals select press double-click it on level two button press connect it to our level one script And here's all we need to do is You call get tree this gets the tree of the scene Which then gives you access to a bunch of different functions As always you can control or if you're on Mac command click and view and learn more about the scene tree And the main function we're gonna call now is change scene to file and you can see it's already auto completing Some of the options of our scenes we could switch to from file. We're gonna choose level two Save it. Let me make that a bit bigger Save it run our game Switch to level two We're in level two. It was that easy. It seems like it might be a Scary thing to do, but it's really just Get tree change scene to file and you pass in a string of the scene You want to change to and then when you press the button Godot handles this and switches the scene accordingly and then we can actually When we're running our game You'll see here. There's this remote debug You may not have seen this before this remote scene. It shows what's running in our game So when we click switch to level two you see over here that changed to level two and this is just this remote scene View is just a nice way to verify what's running in the status of the status of things and you can even click and view Properties and you can even toggle them in your running game as opposed to your local scene builder That's just a little brief aside Okay, great now. Let's make it so that our level two button Exists and now we'll just quickly create a new one Level one button We'll attach a script Yeah, da da da da Connect the signal right there's other ways to go about this that would have been we could have duplicated what we have But let's just wire it up and go the basic simple route copy this over. Let me make it big Just change this to level one The dough tries to help us so now we can switch back and forth between the scenes Well, no, I didn't drag our new button. I Didn't give our button a switch to level one There we go runner game. We can switch back and forth Hmm. Why is that not working on level one button toggled button pressed. Oh, I connected the wrong The wrong signal sure did Disconnect all pressed is what we want Sorry about that Okay This is why we test things we've got a little bit more indentation than we want. Let's run that again and test it Switch switch switch switch great. It's working. Okay, so that was the first way and a totally fine way but something that isn't ideal is that The way this is referenced as a scene if we rename the scene it might not rename it here and it's a little brittle that it's a Yeah String so something we can do is we can actually make a variable and we can say export var Level two scene and we'll give it a type of pack scene and then instead of saying Get tree change scene to file it you can call change scene to packed and then we can just pass in level two scene and then What you can then do is in the inspector in our level one See now we see level two scene and has empty You can just quick load and through the user interface select The scene and now if we run it in level one and click the button it switches it to Level two and so there are two different approaches to going about this you can do it file way and code it yourself and maybe this switching Change thing to file happens dynamically and the path is determined based upon maybe like you know, like maybe you had a variable current level and set to one and you did something like plus current level plus you know Like you did something like that where it You're dynamically determining the level based upon some data. That's one way you can go about it is Using change scene to file. So it really depends on your game's needs and What you prefer for these two approaches sometimes it's nice to be able to go into the inspector and Select your screen and drive things out via the Inspector and the GUI the user interface sometimes it's nice to handle things through code depends on your game's needs, but just know that those options exist and um So that's those are the basic ways of switching scenes and that'll get you pretty far at the start, but The Godot Docs, which I'll put a link in the description about outline a kind of more advanced Approach to scene switching that I want to show so I'm gonna back up this code for the purposes of Sharing it in the github gist In the description and now we're gonna go and we're gonna approach this in a slightly different way and this is totally just based on the Godot Docs and You know if they've got it good and nice then we're gonna just do what they do So they recommend adding an autoload constant. They called it global. I'm gonna call it Scene switcher. So click add Whoops, what do we want scene switcher? There we go, so leave the path empty because we don't have the scene name it scene switcher This will create an autoload constant Singleton as a global variable that we can then access throughout our code. So Now in scene switcher dot gd We'll get rid of this stuff. Well, we actually we want ready and and What the Godot Docs what we're trying to basically get is scene switcher dot Go, you know switch They call it something different. I prefer this naming and then we'll take a path, you know This is what we're trying to get to to call on our code and we'll follow along with what their docs recommend to get that set up so Store the current scene in a variable and it sets to defaults to no then we'll say the Root is equal to get tree Dot root so that's the root node in our scene and Then we'll set our variable for current scene is equal to the root get child Root dot get child count Minus one so that just sets it to the first scene at The root of our renegade and we can even do something like print debug Current scene so that we can see that that's working. So let's just go ahead and run this run our game and see what that does So you can see our game booted and then our print debug output level one And that's our node 2d and that's our scene level one, which is our current running scene So that's great. We feel like that's pretty accurate and then now what we'll do is we'll code this Function that we want to add right. We'll call funk switch scene and We'll call it. I want to call it resource path because that's ultimately what we're going to Handle passing to it and With the good old ox recommended I wanted to explain this further is they recommend using called deferred which says call something at the end of the current, you know game loop so that Whatever is running can finish processing that way we're not interrupting things and here are the dots calls the method during idle time and This is a when you call Q3 on a resource in good out That's doing something very similar and then what we do is we code up deferred switch scene and We also take a res path and it's pretty simple from there We call current scene not free that releases it and then what we'll do is we'll take our new scene and We'll load it and we'll just say Load res path We'll take and set our current scene variable From our scene switcher and we'll say s dot instantiate instantiate So let me instantiate an Instance of our loaded scene we set at the current scene And then we get tree Root and we add child Current scene because we've updated it And we can say get tree current scene is equal to current scene So now we can make use of our new function in our code here. So here we'll just say scene switcher dot switch scene and We'll pass it in our resource and we'll zoom out and Make it So we're in level twos code and we can just drag in this to get the resource path and then in level two sorry Yeah, I was in level one So we got to fix that Make this level one in the level two code kind of confusing with this level one level two stuff Now you can see it's switching, but it's waiting to do it until it's idle and not in the middle of the current running Game loop and so this is the preferred way to go about it according to Godot's docs and it makes sense logically and we have a nice API where you can just call scene switcher dot switch scene and You can even say switch to you know, you can call it whatever you want it to so you would just take this fancy pants scene switcher or Do those simple versions if you're just working pretty quickly, but yeah drop in the scene switcher and just make use of it and You can go ahead and change your game Scenes as needed now there Is something you might need for bigger games? Which is like a loading bar or progress bar and that's beyond the scope of this video But something I want to cover in the future Hope this helped with how to switch scenes and Godot showed three different ways to go about it and I would I would recommend going this last route and just taking the code from The github just in the description of the video and adding your auto load constant right just go to project settings auto load just set the path to the script and You'd be good to use it throughout your entire game. All right. Thanks