 This video was brought to you by my patrons. Thank you so much for your support You know, I'm not a big fan of the state But regarding the state design pattern, I didn't realize its value until recently when I did some research and I tried to process all the information on the problem that it tries to solve and I Notice that I was missing a lot by avoiding and I understood the problem that it tried to solve How it approaches this this problem and how it provides a solution for that problem and I realized that man I was losing a lot by avoiding it So let me guide you through that the creative process that I passed through to understand this pattern And also how did I implemented it on kitchen tails in good old engine? So let's get started. So to understand the value of the state pattern We first need to understand how object oriented design and how object oriented programming works So let's go back to the basics. What is an object? What is this type of data that we work and we call an object? An object is a set of Data and process so we have the variables and the constants and also the methods and functions the set of Data that we have the set of values that we have in each variable or constant is what we call a state So when an object has a Let's say it has a Boolean variable and this variable is set to true So the state of this object is the whole set of the values of its variables If it has a one a single Boolean Verbal it can only be on two states, right? So it can be only on a state where this value is on or true or when this value is off or false when we have true Verbal's like say we have two Booleans we start to have a Exponential problem because now we have four states the combination of each of the states of the of these both variables and this can grow and this can actually snowball a lot when we start to have variables that the actual value can be Anything like strings for instance or colors. We have we have a lot of colors Let me research how many colors we have with the RGB system RGB system a total Color that I'm not good with math. I must say that Okay, so with a 32-bit system We have about four billion colors that we can have With a single Verbal so with a single color variable. We can have a huge huge amount of colors and each of these colors directly Theoretically Theoretically can build up a huge amount of states each of these states When we change a value, so let's say we have an object that is built up of only constants So all its properties all its data is constant This object can have actually only a single state It can be only into a single state even though these values allow for a lot of more states But the value of the state this amount of states that these object can be since it's using only constants It's only one. This is because when you have an object When you change these values and when you allow for the changing of these values What happened is that you have a mutation? This is what we call when an object changes from one state to another So let's say we have an object with a variable a Boolean variable with its value set as true If in the middle of the program it set its value to false this that just happened It's called a mutation. So that's a what is the value? What is the thing that the state pattern tries to provide? What is this solution and the problem that it tries to provide when you are designing a program? You'll try to simulate what are the possible states that an object can be and also what are the set of states that the whole Program the whole application can be so you try to simulate Okay, this can happen. So if this happened and this is in this state this can happen as well So it's try you try to simulate all the possible outcome, right? So the state machine that they state pattern actually tries to Scope the possible states in which an object can be so let's say you have a Character and this character can be walking dashing or idle there There's only three states that these objects can be right so you can say that when it's dashing It has a given velocity. This is only this is the only thing that changes between the dashing They're walking in the idle. So let's work only with this speed or this velocity variable when it's dashing The variable can be the highest amount possible within the scope of your game when it is walking It can be a middle ground and when it is idle it can be zero So you scoped down your project into only three possible states and this works as Kind of like if these states were supposed to be constant So when you have a mutation you don't have a mutation within the the velocity variable You have something which we call a state machine and this state machine will tell which of these state is the Actual the actual the current actually sorry the current value So which is the current state and this state machine will try to handle which is the current state Meaning that there can only be one state active at the time knowing that we know all the possible outcomes from a given object So if it can only be within these three states Another state is not possible Unless you have a mutation within these three states So if you change the velocity of a Dash state for instance You have a mutation and you lose control over this design that you created over this architecture that you created So this is how I understood the state pattern if you try to analyze the all of these Patterns that we have with object oriented design you start to see kind of like a trend Which is something that I was supposed to know already, but I didn't let's talk about for instance The strategy pattern. What is the strategy pattern? The strategy pattern tries to encapsulate Conditionals in an object. So instead of having a mutation instead of branching your logic instead of having an if and else Statement instead you change how the object will behave by changing What is the current strategy that it was supposed to to use further in the in the running of this application for a command You have encapsulation of a method So you pick an object a method and you transform this of this method into an object and For the state pattern you pick up the possible states So the combination that the set of values that the variables of this object can be and you turn this into a new object So this is my creative process This was what I pass it through to understand what the state pattern was supposed to provide to us So let's see how did I implemented that in kitchen table? So I'll just It will be just an overview. It won't be a tutorial. Okay. Okay, so let's start from top to bottom So we can understand the whole architecture of this design here We have the state machine which is what will handle what is the current state active? So if we open that all that it does is that it will transits transit between this object this state Turning one of them active and the other Unactive so this is basically all that it does but Since I'm also working with some commands. We'll see that a bit later I also added something to this state machine Which is that it will delegate Commands to this state. So instead of that trying to make a command execute right away We will instead ask this state machine to tell a state to execute a command. Okay, so It passes through two delegations. So the state machine delegation the state delegation and then the command This is because when we if I open here the state machine We can see the the state so we can be on these states and if I open the state Actually, let me open the sin tree here. You can see that it has a Set of possible commands that can be executed within this state So if I open the state we can see that when it is turned active It will also turn all its children which are commands active as well And when it do so it will allow this Commands to be executed and then when it receives a command it will try to search If this command exists within its hierarchy, so if a command was delegated to a state and This state doesn't have this command within its hierarchy. This command won't be executed. So this will prevent for instance Let's say I want a character to Prevent a character actually from dashing when it's jumping I can just remove the dash command from the jumping state and you can see that How many layers of design pattern I am in So we have the finite state machine. So the state patterns. We have the command patterns And also we are using the observer pattern here as well. So to mutate the States of the state machine. These commands will send a signal telling to this state machine What is the state that it was supposed to transition to? We have here the false state So if the false state is finished So if you open here, it finishes when the actor is on the floor So we can see that here when the character is on the floor This false state this specific false state, which is the false state of the single jump false state The full command Within the single jump false state will tell the state machine to change a state to idle This is how the state machine transitions between the states a command will tell it to transition This allow me so to tell what is the Specific state that a specific command within a specific other state will transition to And this allow me to force it very clearly What will be the possible transition? So if a if this object reaches a given state, I will know probably hopefully How did this object reach at this specific state just by analyzing What are the the commands from the previous state and so on and so forth until we backtrack What causes this state to be active in this specific time of the of the application of the game in this case? Now I can tell game because we are messing with games now For instance in the wall state, it can stop walking. It can jump It can perform another walk which will change The direction so it will go back to this walk. Actually, it won't even transition through any other state. It will just execute this command with the updated direction We also have the fall so if the character is walking and it reaches an edge a cliff and starts falling So it will go to the single jump fall state which will allow it to perform another jump And with that I was able to implement my character movement Reimplement actually it was working, but I saw that I will lose track of the Logic so I decided to implement the state pattern and I had to refactor everything But now everything is working again So you can see that the character can do everything it could before But now I have a clear site of what is happening. How did it reaches a given state? How What are the available things that these objects can do in this specific state and I can start to design other characters such like A enemy a boss etc because now I can just pass a state or an action that this object should perform This enemy for instance this boss can perform and I know what are the states that it will transition to So it will be very very nice to try and test this and stress this out to get content for kitchen tables So that's it That was my design process to understand the state pattern to understand what I could achieve with that And how I implemented it in kitchen tails using good at engine If you want to get this exact implementation I will put the link to the project in the links below in the description and also in the pilot comment And also I put a link to the specific pr where I implement this design pattern So you can understand all the the process that I pass through all the steps that I pass through To implement that in kitchen tails. Also, don't forget to follow kitchen tails By following me on each IO the links will be in the description as well. So that's it. Thank you so much for watching Keep developing and until next time