 This video was brought to you by my patrons. Thank you so much for your support. Hello there. Continue this series on design patterns. In this video we will talk about the Singleton pattern. A pattern that tries to solve two problems, so it has some critiques about it because it breaks the single responsibility principle. And it also tries to provide a global access to an instance, but let's try to understand it. So I will change to the book here. So the Singleton pattern is a creation of design pattern that lets you ensure that a class has only one instance while providing a global access point. So you can see that it already has two responsibilities. It provides only one instance and provides a global access point to it. But the problem that it tries to solve are very crucial because you have these problems on your own, mostly of the cases. So let's say you have a utility class that provides some utility functions or something like that and you don't need two instances of this class. So you just need one. And also you can have like a database and you don't need two instances of a database. You can have only one, but this database has to be global as well because some classes will use this database. So yeah, it is a very tricky pattern, although it is very useful, very, very useful. So let's try to understand it. The problem that it tries to solve are two, as I said, ensure that a class has only one instance and provide a global access point to that instance. So let's try to see how this can be applied in Godot Engine. So here I am in Godot and I prepared this simple score node, score system, score class. And basically what it does is that it takes a current score and it compares if this current score is greater than the high score. If so, it will set this high score to the new score. I should actually say this. Okay, so this is basically what it does. It controls the scoring of the game. But if you think about it, there are so many things that can increase the score of the player that can make the score increase or set a high score. There are so many things that can increase this score that it will be not so reasonable to all of them create new instances of a scoring class and then saving the score or making something like this. Instead, we can create a global instance of this. And this instance, actually, the main problem here is that what if we create two instances of this score class and we don't sync the current score or the high score. So on some instance, it will be 11 and some other instance, it will be 100. So you have to keep this in one single place. And this is what this class is proposing to do. So it's very clear that since we will have many classes sending information, sending data to a single class, and we need it to be single, we can apply the singleton pattern here. In Godot Engine, there is a very simple way to apply the singleton pattern. There is going to the project tab here, project settings, and we have here auto load. So we have an interface for creating singletons and you can see that this is the actual singleton here. So we have the singleton already explicitly communicated to you that this will be a singleton node. So let's create, let's load this script here. So singleton pattern score. We can set its name. So I will use score here. And we can, this is what this is the thing we have here, the enable singleton thing that we can enable or disable it. And we also have the ability to open this script. So we can quickly go to the script that we are using as a singleton. And when we set this, what Godot will do is that it will create the sing tree and we have the root node of the tree load. And as a singling of this root node, we will have the auto loads already loaded for us. So let's say I will create a very simple scene here just so we can test this. So I will create a new score scene and this scene will have a label. That will be the score. So score and we have something like score here. And I will just make it on the top white and make it centered. And on this score label, what I will do is that when, let me save this, save score interface. And what I will do here is on the score label, I will create a script. And when it's ready, it will let me try to take rid of the singleton so you can see what it does. What I want to make here is that when it is ready, it will connect this signal. So score changed to the label. So we can change the text to synchronize what is being displayed to what is behind the system. And I want to connect this to the label. But how I will connect this if I don't have a reference to this node? So how will the label have a reference to the only instance of the score that we want to have in the whole game? Here is where the global team comes in handy. So when we try to make this, let's try to go here in the script. Well, what I want to do is that I want to go to the score and connect its signal. It's a score changed signal to this node on score score changed method. So let's create this method here. And we'll get a new score. And I will set the text to be score. And let's say this modulated to have the new score. So this is the function. And you can see that Godot is telling us that the score variable is not declared in the scope. So in this scope, in the class scope, we don't have any variable, any reference for a score team. So we don't have a label. We don't have anything. So we are trying to make something that doesn't have a reference to what we are trying to connect. But if we go here in project, project settings, auto load, and we load this score, naming it with score. Now we have a single tone. And then it should work. So if we test this, we already have this score here. So let me just change this to something more visual like this. So we already have this, right? So when the score is updated, we will have this connected. You see that we didn't get an error. And probably this should work already. So yeah, we don't have any errors now. And now let's try to make something that will update this score. So I will just create a button here, button. And just to test this, we will have increase score. And when this happens, so score button, let's create this. And it will quite make the same thing, but we will make the reverse operation. So instead of connecting the score node to this node, we will connect the button up signal to the score single tone. So on the ready call back here, we will connect the button up to score set score. I should use strings here. And we will pick the score, the score plus one. I don't think we can bind this way. So instead, I will create another function here that when we press the button, it will communicate the score auto load to update the score using its current score. So what I do here is that I will connect this node button up to itself on button, on button up. In this function, this call back, what we'll do is that we'll go to the score and we'll go to the current score plus one. This is what we'll do here. So if we test this, we can see that the score update and we have this. And you know that these are referring to the same class. So if we go into the sin tree, so remote sin tree here, see that we have this root node. So normally, if I take rid of this auto load here, you see that this won't appear here. So we have this root node that is the scene that we are testing. And we have this score. And if we check the score here, we have this current score set it to eight, which is what we are testing here. So if I update this, we have this updated there. And just so we know simply what this auto load does is that I'll take rid of this. I will comment everything here just so we can test this properly. Let me take rid of this as well. Actually, not coming this just this. And so if we test it, we don't have any error. All right. So what we'll do here is that we'll take rid of this singleton class of the singleton node. And now in the remote scene, we don't have it loaded as a sibling of the current scene. So we have the root, we have the current scene, and we don't have any other node here. But if we set it to be an auto load, let's go here, score, edit. Now we will have a reference to this node as a sibling of the current scene. So we have this and this on the same hierarchy. But this is not the only use case for singletons at least in Godot. Because here in Godot, you can make a whole scene an auto load. In this case, I made a script, an auto load. So if I go here in auto load, what we are loading is a script. So I did a script, right? But we can make a whole hierarchy of scenes an auto load as well. One of the use cases for that is let's say a music, a background music. So let's create another scene here, which will be a background music. So audio stream player. And what I will do here is that this will be the background music. Let me load a music here. So here, music, let me take this music that I made here. And I will open the project there. I will open directly here. Open in file manager. I will throw this music there. And here I will make it play this music. If I test this, yes, save, singleton background music. If I make it auto play. Alright, but if I play the game here, we don't have this music, right? And even better, since it's an auto load, it will keep... Let's remember how is the scene structure here. Remote. We have this score, which is an auto load. And if we switch the scenes, these auto loads will be kept in memory. So they won't fade away. They will be right there. So the music will keep playing if we switch the scenes. Let's try to make the following. I will add... Let me stop this. I will add a simple button here. The button, okay. So we will try to switch to the command pattern scene. So switch to command scene, which is this one right here. Remember, I will make a simple script here. It can be a building script. And when this is pressed, so on bottom up, okay. We will ask the scene tree to switch to the command scene there. So get scene, get tree.change scene. And let's pick this copy path, okay. Save it. And let's see what we are trying to achieve here. I will put this background music into this scene here. So I'll throw this here. So we have it playing on the scene, right? But if I try to change it... To change the scene, we will lose this music. See? We don't have it playing anymore. So we don't want this to happen into our game. But if I make this a singleton... Yeah, delete this node. Properties. I will load the scene. And notice that we can load the whole scene. So singleton background music. All right. You see that we don't have it stopping anymore. So I will play the scene. Oh, I forgot to make this play project. Oh, I forgot to add the actual node. So save this. Let's try this score. So you saw that the music doesn't stop playing. And Godo provides this on any sort. So let's say that I don't want this score scene to be an independent scene. I want this to be an overlay on all other scenes. I can make this whole scene to be an autoload as well. So I can go here. Project settings. Load. And I will load this score interface. Edit. And if I play the command pattern... There is the score. Which I can increase the score. And I can switch to this scene. So this is the singleton pattern in Godo engine. And that's it. I hope you enjoyed the content. Make a very good use of the singleton pattern. It can be very powerful, but it can be misleading sometimes. So use it carefully. And this is it. Thank you so much for watching. Keep developing and into the next time.