Best Practices for State Machines? (Opinion)

T

ThatTwoGuy

Guest
First time posting to the Game Maker forums. I would like to ask a question more about general practices than a specific issue: when making a platformer, how general or broad should a state machine be? Should it be as specific as every action the player can take (walking, jumping, falling, etc.) being its own state, or should it be as broad as only changing if the player character, for example, goes underwater or collects a power up of some kind. I realize this is likely going to vary depending on the project, but I would like to hear the general opinions of the programmers here on the forum anyway.
 

Yal

🐧 *penguin noises*
GMC Elder
I personally use a "ordinary" state that handles walking, running AND jumping all at once - transitions between states tend to make the game clunky to code and often make the game feel clunky and rigid as well, depending on how smooth the transitions are. So I only use separate states when the state radically changes the object's behavior - for instance, melee attacks might temporarily take away your movement ability entirely, and override the standard animation system. Being sent flying by a springboard or a powerful attack will temporarily make you unable to control the character and might change your physics for the duration of the state, and so on. Those conditions deserve their own state, because you'd need completely unique code for them.


Oh - and I recommend having "behavior package" functions / scripts for behaviors you'd use in a lot of states, like "gravity", "move based on current speeds unless there's a wall in the way", and "read controls" - duplicating the code itself means you'll have to fix every bug multiple times, too, so use scripts instead of repeating yourself 🦈
 
T

ThatTwoGuy

Guest
Hmmm... that sounds like it's probably a good idea. I've been using the more specific model I mentioned above for a little while now, and while it's made implementing things like a hovering ability a cinch, it has also made handling things like falling, jumping, and other abilities rather... well, clunky, like you said. Behavior functions also sound like a good idea, especially considering that you can even take away behaviors depending on the state.
 
Top