Finite State Machines Tutorial?

techmaster90

  • Posts: 26
Hello,
  In my struggles to learn how to make video games I came across the topic of Finite State Machines at this link here http://community.stencyl.com/index.php?topic=54586.0   But I can't find a tutorial, explanation, or set of instructions anywhere on how it works. Can some one please make a detailed tutorial on how to make a Finite State Machine for Stencyl and how it all works? This would be super helpful to beginners like myself to learn code logic and how to use stencyl.

Also if there is one already made can someone please direct me to the link?

merrak

  • *
  • Posts: 2739
Hello,
  In my struggles to learn how to make video games I came across the topic of Finite State Machines at this link here http://community.stencyl.com/index.php?topic=54586.0   But I can't find a tutorial, explanation, or set of instructions anywhere on how it works. Can some one please make a detailed tutorial on how to make a Finite State Machine for Stencyl and how it all works? This would be super helpful to beginners like myself to learn code logic and how to use stencyl.

Also if there is one already made can someone please direct me to the link?

There is an abundance of tutorials out there, but here is one directly related to game development: https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867 It's pretty extensive, but covers what you'd need to know.

A finite state machine is a concept, so this isn't something someone can "make" per se. You implement a FSM to solve a problem.

I use a FSM to handle player input and character actions. A simple example goes like this:

Suppose you want to program Kirby to react to the down arrow key being held. He should crouch when he's on the ground, and fly downward if he's in the air. A simple way to program this is:

Code: [Select]
if player is pressing 'down':
    if kirby is in the air:
        fly down
    else if kirby is on the ground:
        crouch

This is fine for a simple character with a small number of moves. However, as the controls get more complex: more animations, more possible inputs, and more reactions to environments, your code could have a lot of if/else if statements.

A FSM solution to this problem would look more like this:

Code: [Select]
if player is pressing 'down':
    trigger event 'player_pressed_down'

You can then change the text attribute 'player_pressed_down' to the event that should be executed when the player is pressing the down button. Change it to one event when Kirby is in the air. When he lands, change it to the other event. You can automate this further by naming your events after the animation name in conjunction with the control:

Code: [Select]
if player is pressing 'down':
    trigger event 'animation for kirby & _down'

If you have an animation called 'flying', then the event 'flying_down' will be triggered. Populate this event with the code you want to execute. This is a great use for custom events in behaviors.

When you have a lot of animations, this will give you much cleaner code that is easier to keep updated as your game evolves.

gazugafan

  • Posts: 4
Did you download my example, try it out, and look through the code? If not, download the .stencyl file, import it, and then create a new game based on it...
http://community.stencyl.com/index.php?action=dlattach;topic=54586.0;attach=56467

I think my post did a decent job explaining how to use it. For exactly how it works, check out the code in the State Machine behavior. The details are maybe a little complex, but the basic idea is that the State Machine behavior contains a stack of states (actually, one stack of states for each state machine), and each state corresponds to a behavior. The State Machine behavior is then responsible for ensuring that ONLY the behavior corresponding to the active state (the one at the top of the stack) is enabled, and all of the other behaviors corresponding to inactive states are disabled. It also triggers enter and exit events whenever a state change occurs. I think it's pretty elegant!