GameMaker: State Machine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello game developers and welcome back I hope you're sitting comfortably because today we're going to be covering something very important we're going to be looking at the concept of finite state machines and how you can use them in game maker to do lots of awesome exciting things now finite state machine is a very complex sounding term it's actually not really that complicated of a thing although it is quite difficult to kind of explain and it's actually really not all that complex of a concept really all it is is just using the idea of an object being in a given state and Qi being able to change its state in order to control how your game works it's really in terms of how we're going to be using it in game maker all you really have to think of it as it's a way of organizing your code in a way that makes making much more complex games much more manageable and possible to do so let's take a look at an example of say a platformer game that doesn't use state machines here I've set up a platformer game just using the same sort of code that we've used in like my previous tutorials if you follow it along with any of my platforming stuff how it works we have a player object who's established just a couple of little variables and then has this step event okay so who has it gets a the player gets all the inputs at the start of a frame we perform some calculations based on those inputs on where we want to move and then we check to see if there's going to be a collision in the place that we want to move and we move towards that place if there isn't we move as close as possible to the wall and stop if the if we're about to collide with something okay simple like 30 lines of code and it's manageable while our game is that simple but now say that we want to add ladders into the game okay so I'll set up these ladders around here now normally what we would do in that situation is change this code to something more like this code okay so what I've done in this code is I've created a new variable called ladder that checks whether or not we're on ladder and if we're not on a ladder then we do our regular movement and if we are on a ladder then we do a specific type of movement like going just going up and down and checking to see whether or not we press a key to detach from the ladder and so on so forth and if we're not on the ladder we check to see if we're touching a ladder and if we are touching ladder and pressing up then we attach to the ladder now this is an OK way of doing things while you've got a very small number of kind of mechanics going on in your game you play if I just wanted a platform with just ladders and then this would be fine and there's not really any problem in doing this however if I wanted to start doing more and more complex things say I had ropes that I wanted to swing off of or like NPCs to talk to or different kind of states you might see where I'm going with this that my player needs to be in at any given time it gets more and more complex and more and more unmanageable to write code just using like the odd variable to check whether or not you're doing something because then you have to wrap everything you do in different if statements already here I have to wrap all of my movement code my normal movement code into this if statement to check to see whether on what we're on a ladder and if we're not on a ladder then do this and all of the rest of the code of still gets run regardless and so if anything and letting my collision needed to change based on whether or not I was on a ladder I would have to wrap that an if statement as well and so on so forth and it gets really really complex and manageable as you start to wrap more and more things if not on a ladder if not talking to NPC if this if that if the other you know what I mean the problem is we're only looking at our players logic as being a single step event ok this single event the single big chunk of code that's executed every single frame of the game so whenever we want to do anything we're always checking against every other possibility impossible kind of state the player might be in or we want to do instead of having just this one step event that contains all of the possibilities for this character and has to juggle between all these different if statements just based on what our character is doing at any given time what we want to do is divide that into a different state that our player can be in at any given time and then when each frame comes around instead of running one huge chain of logic we run one specific chain of logic depending on what state our character is currently in you can have states for everything so you can have states for talking to an NPC for being in the air as opposed to being on the ground for swimming for climbing for anything you can really imagine and what this does is it means that we own we get to choose specifically what code is run we're without having to divide everything into lots of different if statements and then when all we need to do is define the conditions for changing between those states inside the states themselves if that explanation made no sense whatsoever let's take a look at what this exact game our little dude running around climbing on ladders would look like if it used a state machine so why I have here is a copy of the exact same game just has a little dude can run around can climb on time on ladders only this time we're using a state machine and instead of having a single step event I've divided this all the code that would go into this step event into a number of different scripts but how do I decide which script a call well when I initialize the game in the first place in room 0 just stuck it in the creation code I create an enumerator for States now if you've never used an enumerator before what an enumerator is is essentially just another way of creating a constant variable a variable that will never change but giving it a more English readable name that's really all that they're there for so I've created one called States and what that means is I can refer to States dot normal that will always equal I think 0 or I think 1 it doesn't really matter just a number that's different from States dot ladder as I've defined here which would be I think 2 in this circumstance doesn't really matter the number is not relevant the fact is it's just a different number that's given a different name by doing that that just allows me to refer to instead of just having to set state equal 1 or state equal 2 I can say state equal States normal or state equal states ladder just so it's more readable to me and I don't have to remember what number is what state so after creating that enumerator I've gone into the player object and in the create event I set my state to be state equals states normal now in the step event for player where we would normally run all of our our huge chain of logic that would just be like Oh first of all getting our inputs then checking all of our movement and checking on whether or not we're on a ladder and so on to both then doing collision while you body bar what I do is I run a single state that a single switch statement based on our current state now our state is set to states normal so by default it'll run this first case which is case States not normal it'll run this script script player underscore normal break and if our state is ladder it'll run this script instead s.coups s ER and the score player underscore ladder so what actually exists in those states into the scripts even sir if I go into SCR underscore Player underscore normal this is what happens I've even divided the inputs into separate scripts as well and the collision section which means that we can decide in these states whether or not we actually want to call collision or whether or not we even need to get the players input in the first place without having to just copy and paste huge chunks of code around we can just call the script or not so we're going to SCR underscore get inputs you can see it's just that thing that was at the top of our step then before get input key left equal keyboard check left and so on so forth all just called at the start of SCR underscore player underscore normal so this is like a standard step event this is basically everything that would happen normally in our step event without any of the ladder stuff okay does all of our normal movement stuff but then importantly oh and also what does all of our collision stuff at the end just like how it normally would but importantly in the middle here it does this thing where it says or if I'm colliding with a ladder and I'm pressing up or down then set my HSB and vs b to 0 set my speeds to be 0 and then set state the equal states dot ladder okay and then it will do all its collision and everything like that but then the next time around the the step event happens it's going to come back into here run this run this switch again and then this time it's going to run SCR on the score player on to score ladder meaning it's going to skip all of the stuff from the previous step event it's going to ignore all of it and it's just going to go straight to s you're on just go play on the score ladder now this is just a different version of our step event so basically I'm just sending the player to a different step event instead of just having one we've just divided it into multiples that can happen depending on what state were in and in this one I'm going to gang our inputs again but this time I'm ignoring all the movement calculations stuff from before and I don't have to wrap it in an if statement or anything like that because we already know that we're on a ladder because we're in the ladder state so I do the movement that's only moving us up and down ignore any ability to move left and right you can't move left or right on a ladder right and then just said or if key jump or if I'm not touching the line anymore because I climbed off the top cell speeds to be 0 again and set state back to state dot normal so I have a way to transition between both states very easily and then when I'm back in states normal it runs our normal step event again and of course at the bottom I run our collision code which I've just bundled into a script rather than just have in copied and paste it into each one since I know it's going to be the same in most states okay now this results in exactly the same kind of game that we would have if we coded it how we used to how I've coded it and want my platform our tutorials but by using a state machine suddenly we have a ton more power and a ton more freedom in terms of how we're organizing our code to be able to do all different kinds of complex states and much more easily and much more manageable without running into loads of problems like having a triple check everything like oh if this and if that if the other because all of that stuff is handled by the fact that when we're in the latter event we only have to worry about everything the player needs to know when they're on a ladder which is that they can move up and down right they don't need to do it they don't need to need to register the left and right arrow keys because that they don't don't mean anything you know I don't have to like wrap the section the controls horizontal movement oh don't do this if you're on a ladder because we don't need to were already in that stepper gun okay now this isn't just relevant to platformer games this is relevant to basically any kind of game you can create okay you can have States view different enemies okay this is why I'm covering this tutorial so I'm going to platform or AI and doing enemy AI stuff okay is that you can create enemies that are like have an alert state or an aggro state or like our Idol or a walking around and stuff like that basically anything you can imagine that any object you can imagine that has complex behavior based on link Oh being in a different state or being in a different conditions you can apply this technique and it's a really really solid way of organizing your code and basically will empower you to be able to make much more complex games without your code just becoming a totally unmanageable mess they have it I hope that made any kind of sense that's how you state machines in game maker and I hope I really really hope you were able to understand it make sense fix it's really really useful thing to know I hope you enjoyed that leave any comments or suggestions below and I'll catch you guys next time thanks for watching see you guys you
Info
Channel: Shaun Spalding
Views: 105,869
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, State machines, Finite State Machines, Finite-state Machine
Id: -0bckaBj__M
Channel Id: undefined
Length: 12min 25sec (745 seconds)
Published: Sat May 30 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.