Jump to content


[QUESTION] Hierarchical State Machines (HOW TO?)


  • This topic is locked This topic is locked
1 reply to this topic

#1 xDGameStudios


    GMC Member

  • GMC Member
  • 71 posts

Posted 08 March 2016 - 10:21 AM

State machines are very useful for creating games, implementing advanced game logic and player actions, enemy AI, menu commands....

And there are verious ways to implement them (already stated on other posts), my "problem" or question is what about Hierarchical State Machines.

A machine where a given state is a state machine itself... this could be very useful for example the main state machine is has the states:

Playing, Menu, Pause.

When Playing state is active:
you have the player state machine running and the enemy state machine running

When Menu state is active:
you have the menu state machine running with various states (MAIN MENU, ITEM MENU, EQUIPMENT MENU, SETTINGS MENU... as you can see I'm talking about RPG here xD) (...the other state machines are not running).

When Pause state is active:
you have all other state machines are stopped.

I know you can do this "hacking" and with workarounds.... but what about a clean Hierarchical State Machines? Any ideas how to implement one? I would like to here from the advanced coders here, maybe some brainstorming! :)
Thank you

EDIT: Adding to the theme!! For example each state having an input... and the other input check would be put on hold... if you a in a menu state-machine a the arrows move the cursors and space selects (for the ITEM menu) ... page up and page down would switch characters but only in the PARTY menu sate.. if you are in playing state.. arrows move player, space jumps and shift attacks, if in pause.. space removes from pause. input could be added to states, and have their hierarchy too.

Edited by xDGameStudios, 08 March 2016 - 09:03 PM.

  • 0

#2 RujiK


    GMC Member

  • GMC Member
  • 841 posts
  • Version:GM:Studio

Posted 09 March 2016 - 04:52 PM

For your example (Playing, pause, menu) HFSM's would be a poor choice. HFSM's would be greatly complicating something that should be very simple.


Instead of an HSM, you literally only need a single variable and a switch.

//1 = playing
//2 = menu
//3 = paused

switch (global.game_status) {
     case (1): {stuff; break;}
     case (2): {another switch here for the menu; break;}
     case (3): {stuff; break;}
//This is not a "hack" or workaround, this is clean simple code.
//Complex != better

HFSM's shine when you are not sure what the best choice of action would be (Should I stealth, flee, or attack?) but for these examples, they are all 100% choices. Either the game is paused, or it isn't. There is no "Hmm.. Should the menu be open now??" Your dealing with absolutes.


Now that's not to say its IMPOSSIBLE to implement an HFSM for what you suggested, but it's absolutely overkill. Even a normal finite state machine would be overkill for a menu in my opinion.


(I've used behavior trees and FSM's (Both for AI) but not HFSM's so some one with more experience may disagree.)

  • 0

Also tagged with one or more of these keywords: gm:studio