Hello and welcome to another episode in the Godot Basics tutorial series. In this episode we'll be taking a look at auto loads. Before we get into that. Let's talk about singletons now. Singleton has a design pattern where you only have a single instance of an object. Godot does not support singletons or rather you are unable to program your own Singleton through code. However Godot does offer a workaround. Now get those work round to the Singleton pattern is through auto loads.
Now auto nodes allows us to create a single instantiated script unto the scene tree. Now depending on what you inherit through the extended keyword in your script your auto load will in fact have access to the lifecycle scripts because again it will be created and active on this scene. True why in the world would you need to have only a single instantiated object. Well for one thing you may want a single source of truth for your data. You mentioned a scenario where you need to share information amongst different scripts and game objects.
If there are multiple sources of truth for your data how do you know which one it holds the most updated information and well that's what auto loads tries to solve by having only a single instantiated object in the same tree. You will only have a single source of truth so in the case with an auto loaded script you will feel more confident. The fact that you are retrieving the most updated information or piece of data and on top of that auto loads allows us to keep our data living across multiple scenes when we change scenes and creating an auto loaded script is quite simple and you can do this in four easy steps.
First you have to create your script. Then you have to go to project settings into the auto load tab then add the path to your script and lastly enable the Singleton option and while you will have a single instantiated object on the same tree no matter which scene is loaded and let's go ahead and take a look at that as you can see we're inside project settings in the auto load tab. Right here we have our path to our G.D. script file. We have a node name which in this case a singleton example and here is our path Singleton example that cheating.
And lastly our Singleton is in fact enabled and that's the most important thing to remember we need to make sure that that box is checked marked. If you omit the check mark in the box for the enable option you will in fact have to do an additional step in order to retrieve data from your singleton or in this case auto loaded script on the same tree. Now when the game runs you'll notice that you have your G.D. file. In this case Singleton example and right here we have the route viewport and when our game is running you'll notice that our Singleton example auto loaded script is in fact instantiated onto the scene tree.
Now let's go ahead and see how we create our own auto loaded script. Now if we go to project and project settings we must go to the auto load tab and as you can see here we already have a singleton. So let's go ahead and create another instantiated script object or auto loaded object. Now first we must define our path to a G.D. script file. In this case we can type it in here or click the folder button and over here we just pick our G.D. file. In this case I'm going to pick scene one script right here in the node name we can choose our name.
Notice that the name is auto generated based on the name of your file. We'll go ahead and press the ad button and you'll see that are seen one script has been created and now every time we run our game we will have both. Singleton example and seen one script auto loaded on to our scene true.
Now notice that both of them have the enable checkmark option set to true. We don't want to touch that. We also have a folder button but all that really does is it just opens up the script so we can edit it on our Godot app. Now on top of the folder button that just takes you to the script file so you can edit the code. We also have an up button and a down button and that changes our order of our auto loaded scripts. Now this is important to note because whatever order you see here is how they will be ordered or rather auto loaded onto the same tree when your game runs.
So let's take a look as an example now. So someone's script is first in Singleton example a second. So when we run our game
you're gonna notice when you click the remote tab that seen one script is first and Singleton example a second and then you'll notice that no two is third. Basically everything or rather all nodes you see in your scene. Tab will be loaded after your auto loaded scripts are loaded onto the scene true. Now let's go ahead and change the order and instead of seeing one script being first we're going to set Singleton example first. So you'll notice here that when we run our game Singleton example is first in scene one script is second followed by everything else that was in our scene.
Tab now that we took a good look at our project settings. Let me go ahead and delete the scene one script from the auto load tab and to do that you just click the trash can icon to delete it. So for the rest of the example I'm going to use the singleton example script file to show you are two loaded scripts. Now on top of that I have three scripts the Singleton example has life points and this is the information we're going to grab. So on top of being able to retrieve variables or excuse me class properties we are also able to use functions.
Now our method called life will just add 10 points and on top of that because we extends from node 2D We are also able to run our script life cycles in this case we can run the ready virtual method. Now it's quite simple it's just a simple example I just want to show you that when we change from scene one to scene to that when we edit our life points that the data in fact is consistent among scene changes let's go ahead and take a look at the scene to script file. So extend from node 2D.
We only have one virtual method in this case it's the ready virtual method and all it does is it prints to the screen scene too so we know that we have in fact change from scene 1 2 Scene 2 and we print the left points property provided to us by the Singleton example script. Now notice here that we have an instance object called Singleton example but in our entire script we do not instantiate it. We do not load it as a matter of fact because it was also loaded in the projects settings with the Singleton check mark enabled.
We are able to globally access the script file through the name that we gave it in the auto load tab and again the name we gave it with Singleton example and so we can just go ahead and call it followed by the dot notation followed by either the property name or the method name. And that's how simple it is to use auto loaded scripts. Now on scene one we have the same thing. We print to the console scene 1 so we know that we are on scene 1 we print the Singleton example life points property.
We then go ahead and change its value from 10 to 11. Then we call the Singleton example script instantiated objects life method which adds 10 to the life points so ten plus eleven is twenty one. We go ahead and we print that to the screen or excuse me the console and then we call the scene. Trees change scene method and we change to scene too. Now when I go ahead and run this game you'll notice in the output in our console log that we do in fact call the Singletons ready function or excuse me.
Ready virtual method. We aren't seen one and we do print to the console the life points property value which starts at 10 we change it to eleven we add ten because we call the life method the new value is twenty one. After that we change our scene and you'll notice here that we call scene 2 and we print up the value of the singleton example script file life points property and because we changed it in scene one to the value of twenty one notice and scene two when we call that property value life points it will print out twenty one and so you can see that singletons are a great way of making sure that data is consistent when we change scenes well that's all I have for you in this episode thank you so much for joining me.
Thank you for clicking Like button and thank you for clicking the Subscribe button if you have any questions or comments please feel free to leave them in the comments section down below. I'm gonna go ahead and upload what I showed you in this episode to GitHub so please feel free to download the example Project in the description down below. I look forward to seeing you in the next episode. Have an amazing day