Hello and welcome to another episode in the Godot basics tutorial series in this episode we will be taking a look at changing scenes now. Why exactly do you need to change scenes. Well in most games you will generally deal with having more than one scene. Let's take a quick look at a basic setup you would have for a game. Generally when you start a game you're gonna have a splash screen which leads into a main menu for main menu. You'll have choices you can either have a game or you can have credits perhaps in about section most of the time when you're player or user finishes or beats the game.
It generally leads to the credits scene as well. And so typically in a game you have this kind of format where you have splash screen main menu and then game and on top of that you may have other things you want to show your users as well. Now
Now how exactly do we go from changing from one portion of our game into to another. Well
Well each game engine will provide you with a way of handling that. However Godot offers you three ways the first way and the way I'm going to show you in this episode is deleting existing scenes through those built and swapping functionality. Now
Now Godot also offers two other ways hiding a scene and removing a scene while also keeping a reference to that scene through variables.
I will not go over that in this episode because we went over them in previous episodes for example in a previous episode we went over hiding a scene. We know that canvas Adam provides the method called a height which hides everything in the scene from the visibility or rather from the viewport that the user is shown. We can also remove a scene while also keeping a reference to the scene through variables. This is called Object polling and basically you're letting a top level node handle its children by adding and removing them and keeping references to them in memory.
And we also went over this in a previous episode as well. One thing to keep in mind is that when you use Godot's built in scene swapping functionality you're going to delete every node on the scene. So that means that we don't have to worry about our scene having orphan nodes because Godot will handle deleting everything in the same tree for us Godot offers two way of changing scenes and it will be through the scene treat the first way is through the change scene method and the second way is through the change scene to method.
Again both of these will delete the current scene immediately when called. Now we talked about the scene tree global class quite a while back in these series. So as a refresher the scene tree manages the hierarchy of nodes in a scene to get the current scene. Tree
Tree we use the global get tree method let's talk about the change scene method to use the change scene method.
We need to use the get tree method to get tree method provides us with the change scene method and it takes in a string value that happens to be the path to your scene. So towards the bottom we can see an example. We have a variable named scene to. It happens to be a string data type and we assign it a string value. With the representation of getting to our scene. File. Which happens to be named. Scene 2. Now once we have this value this string
File Path of value. On to the variable scene to we will then call get tree method followed by the change scene method and we will pass in the scene to variable which again is a string value which represents the file path to our scene file. And this is one way of changing scenes. The second way to change scene is through the change scene to method. The difference between this method and the previous method called Change scene method is that the previous method used a string value whereas the change scene to method takes in a packed scene data type.
So towards the bottom you can see we have a variable named scene to again but this time instead of passing it a string value we use the preload method and we pass it in a string method with the representation of the firepower. We can also use the load method as well. No matter what what we really want to do is we want to get a packed scene data type and using the preload and Load method on a string file path will in fact get us the pack scene data type. Now once we've assigned a practicing data type to our variable named scene two we're gonna go ahead and use the get tree method followed by the change scene to method and we're going to pass in an argument that is of the data type packed scene and actually it's very simple to change scenes and go.
Now benefits of using Godot those built in change scene functionality is that we don't have to worry about memory management when deleting scenes because Godot under the hood will handle deleting children nodes in the scene tree for us. One last thing is that the change scene and the change scene two methods will both return an error enum value. I'm going to go ahead and leave a link down below in the description so you can see the full list of inning values for errors. However all you really need to know or two values the first in human value is code okay.
And it returns the integer values 0. So if your change scene or change scene to method returns 0 then that means that changing scene was a success. However if any of these methods returns a value that is greater than zero then an error or issue has occurred. And from that integer value you can take a look again at the description link down below to see what type of error happened and the type of error will be based on the integer value that the change seeing or change seemed to method.
Return to back. I'm going to upload to get hub a project file with the two different methods. We went over in this episode the change scene method in the change scene to method. So please feel free to download that see what's happening and play around with the code.
Well that's all I have for you in this episode.
Thank you so much for joining me. Thank you for clicking the Like button and thank you for clicking the Subscribe button in the description section down below. I have a treble page with other YouTube channels that deal with Godot so please feel free to take a look at that and I look forward to seeing you in the next episode. Have an amazing day.