 So you've been coding for a little while, but you're still running into a few little pesky bugs in your games and projects Or maybe just looking for a better way to structure and organize your code. We'll check out this simple state machine example that uses scratch I'll talk about what a state machine is through a worked example And then we'll build one together a really simple one all this coming your way Hello world surfing scratcher here teacher server programmer and on this channel I make how to coding videos for curious learners and educators if I need digging this content Be sure to hit that subscribe button down the bottom corner to stay in the loop if you want more information on anything That's mentioned in this video be sure to check out the show notes down below in the description So this is a high-level simple state machine a nice gentle introduction It's not your full-blown mathematical abstraction in programming that you'll see out there on the web And that's for two reasons one. I'm just not that dude And secondly, I like to keep things simple so that you can go down that rabbit hole. All right Let's go check it out All right, let's start talking about state machines first up We better define what one is so a state machine is just a condition that something is in at a specific time to help us with our Understanding this video. Let's think about something as some traffic lights This simple set of traffic lights can have three particular conditions That's stop the amber light that signals that we're about to stop and the green light to signal where go So that's all well and good at all, but how's that relevant to scratch? Well, we can take out something and we can think of it as a game now when we learn of a scratch game We're usually in an idle state and we click the green flag to get it going Then we've usually hit the state where we're playing the game And usually if we succeed or if we fail that triggers another event in the game That's when it's game over Usually to restart your game again, you'll press the green flag and the cycle just continues So you're probably wondering alright, how's this relevant the cool thing about the state machine is that we're not limited to three different states We can add in our own traffic light system We can add in as many lights as we want and when our games get a little bit more complex This type of approach is really helpful to organize at all You could use the state machine to control the states of the overall game So you might want like a poor screen or you could use the state machine to even control the states of characters So if you want a character is idle and then it goes into this sort of jump state here. All right That's enough drawing. Let's head over to scratch and I'll walk you through a game where I've used the state machine And then we'll go build one ourselves So we're going to use this whack a rule game that I built a little while ago There's a link in the description if you want to go check that out and I've got this little variable here on the stage And it is a state variable and here I've just set some text for different states Watch what happens as we play through this game and click the green flag and We're in a pregame state if you check down here in the code pane You'll see that I've got state here, which is the one that is on the screen I've got all these different variable states that I have created as we cycle through the tutorial and we can click on This box to go to a new level and we now into start game And then it quickly transitioned to this idle state here So idle state is this screen where nothing is happening and we've got some feedback here from this chick to do So we can click the chick to guess a number and see how I've just changed a ask for number So we can enter in a number there and then there's a new state validate number and it's just returned back to the idle state there We can also click a sign and that has changed the state to guess rule So notice how all these different events and user interactions are affecting the state of the game So remember when we're in that I'll state we could click these signs So now we're in this state. I can't click at those signs anymore. I can but nothing actually happens All right, now I've run out of lives and we've just hit the game over state So that's how we can use the state machine to code different states in the game. Let's now go build a simple state machine Okay, so I just created a new scratch project and I just used the traffic light emoji and I created three different states green Yellow and red for this project. I'm gonna be working in the code area of the stage First thing I'm gonna do is create my state variable. It's gonna right click on this my variable rename it to state So what we're gonna do in this project is create three different states a stop idle and go state That's to signify the red yellow and green lights of the traffic light and to create a function that sets the state We're gonna create an event that tells the listening sprites that status change And we're going to create events that set the state to specific conditions. Cool First thing we want to do is create our three state variables So click make a variable and I like to prefix my states by having a state underscore I'll have a state underscore stop have a state underscore idle We'll have a state underscore go. We've got our three states over here in the code pane We can just uncheck all those and let's check on state so we can see what it is Next thing we want to do is when the green flag is clicked We want to set the text variables of these states. Alright, so at the moment, they're all equal to nothing So we need to make them equal to a text value to differentiate them So let's go ahead and change the state underscore go to just go Would you look at that and let's now set idle to idle and we'll set stop to stop I do it this way So I only ever have to check the actual text value and set it once now I can just go ahead and check if the state variable is equal to either of these three states It just eliminates the chance that I'll misspell something now. We're ready to create a function that sets the state So jump over to your my blocks. Let's create a block and we'll call it change state To you put a call on that you put a space and we're going to add an input And this will be the state that we'll change to press okay, and now we're ready to set some instructions We're heading back over to the variables category and grabbing out a set state block. We're not setting it to zero We're going to set it to the state that we want to change to alright Let's test this out So I'm just going to drag out the change state to stack block and for the input I'm just going to type the word go I'm going to click it And you'll see up here in the top left hand corner of the stage our state is now set to go But this presents a problem because I've just inputted the text value myself I could set this to anything so I could set that to yet and it's going to change the state So the reason we set these states here before we create these variables is to eliminate the the chance of this happening So I can just go ahead and grab that state variable just there We're going to make sure that I press the green flag because that's going to set all these states here And now I can change the state to go and that works just as you'd expect if I drag in our idle state Our state is set to idle. We'll do the same thing with stop and boom just like you'd expect What I like to do now just to complete this change state to function is heading over to events and just creating a event to Tell every listener in the game that our state has changed So we need to broadcast the message and the message that we want to broadcast is anything that you want But it's pretty useful to be descriptive and I'll just say state changed Cool, so let's head back over to my blocks. So we can just drag out a change state to We'll set the game state to stop when the green flag is clicked now ready to hook up our traffic light to the state system So I'm going to jump into the traffic lights bright head over to the events Now we want to drag out when I receive event I've just connected this if control block with this building comparison We can use this when the status change to check if the state is equal to go Duplicate that if the state is equal to idle or if the state is equal to stop The move down is head over to the looks category and just drag out a switch costume stack block And I've just linked all the states with the costumes that they should show just back over here in the code area Of the stage we just created an event that tells listening sprites that the state has changed the last thing We need to do is create an event that sets the state to specific conditions Fortunately or unfortunately in scratch. We don't have access to this change state to function throughout the whole project So the workaround that we can use our events, of course We're going to create three new events and the names of these events are going to reflect the names of the states So we'll say set state to go then we can drag out our custom stack block and set the state to go to reflect What the event actually does and I've just gone ahead and done that for the other two states for idle and stop The reason we do it this way and not just set the state directly on the stack block is so that we can notify Every sprite that is listening if we have tens or hundreds of sprites in here We want them to know about it another useful advantage of doing this is we don't need to use heaps of forever loops Because if we use lots of forever loops, that's going to impact the performance of our game or program Okay, so just got a little demo and I've got ahead and modified our states a little bit inside the traffic light sprite I've added some sounds to each state So I've got a zoop sound for both the idle and the stop state And I've also got a little engine sound here in the go state if I were making a fully legit game I'd probably break these out into their own custom blocks But this will be fine just for now and I've just got a little sequence of instructions here as a demo So when the green flag is clicked, we'll wait a second set the state to stop Then set the state to idle and then set the state to go as a little countdown. Here we go So This has been a pretty novel example of the state machine in action in a future video I'll look at some games on scratch that I have included that you guys have and I will apply the state pattern to it Lastly, I'll make this project available down in the description So go check it out It's time for a scratchy question And I want to know do you have a project that's out there that you'd like to see implemented using the state machine If you do drop it down in the comment section below and I'll go check it out Also, if you think I've missed something then tell us about it in the comment section down below I'd love to hear about it Hey, thanks for checking out this simple state machine example that uses scratch Be sure to smash that like button if you found some value and have a scout of some of my other content on your screen right now Hey, if you're an educator and you want more then sign up to the surfing scratch You're mailing this link below in the description That's where I'll send news and resources straight to your inbox But until then I'm off to go find a wave. I'll catch you in the next one