Jump to content

  • Log In with Google      Sign In   
  • Create Account


What is a (Finite) State Machine? Why are they useful for game dev?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 auryx   Members   -  Reputation: 122

Like
3Likes
Like

Posted 04 November 2012 - 08:46 AM

Hi all,

In this and other forums, I've seen lots of references to how using FSMs can be really helpful when designing and coding a game. Unfortunately, I've yet to find a really good article/forum post/tutorial that explains the concept in terms of game development.

I've tried reading up on State Machines on Wikipedia, etc, and using the forum search here, but I'm still slightly confused. Can anyone point me to some really good intros for a beginner?

Thanks in advance.

auryx

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9386

Like
5Likes
Like

Posted 04 November 2012 - 10:06 AM

Think of your typical AAA game. When you start the game you're usually presented with a splash screen featuring the developer, then after a period of time or pressing a button, you get another splash screen of the company they licensed IP from, then a splash screen for the publisher, then a splash screen for some of the libraries they used, and eventually you get to a loading screen. After the loading screen you usually see a main menu of some sort. From there you can load a game, start a new game, change your options, exit the program and maybe a few other menu choices like multiplayer. From the loading menu or from the main menu you can go to the game proper. And in the game proper you can usually pause the game, exit the game or go back to the main menu. One way to organize these different game states is as a finite state machine.

Another application is for enemy AI. Consider a sentry NPC. It can start off in a patrol state, where it just walks back and forth between two spots. If it hears a noise it can go into an investigation state, where it goes to where the noise came from. If it doesn't find anything, it can go back to the patrol state. If it spots the player it can go into an attack state where it tries to kill the player. If it loses sight of the player it can go into the investigate state to go look where it last saw the player.

#3 lride   Members   -  Reputation: 633

Like
2Likes
Like

Posted 04 November 2012 - 10:51 AM

EDIT:
There are some advantages in implementing FSMs as objects


Without objects your game might look like this
[source lang="cpp"]while(GameOn){if(state==menu)menu.doStuff();if(state==setting)setting.doStuff();else if(state==scene1){if(playerDead) playerdead.dostuff(); else scene1.doStuff();}else if(state==scene2)scene2.dostuff();//and so on}[/source]Everytime you need to add a scene, you have to add your scene to this if-else list. It can get complicated if a state has a substate.
but with objects, your game loop will look like this
[source lang="java"]while(gameOn){currentState.doStuff}[/source]
You can change what currentState refers to when needed.
For example, when the game starts currentState can refer to MenuState. But when you press "play" button on the Menu screen, you can change your currentState refer to PlayState.

Edited by lride, 04 November 2012 - 11:06 AM.

An invisible text.

#4 SiCrane   Moderators   -  Reputation: 9386

Like
3Likes
Like

Posted 04 November 2012 - 11:00 AM

Without a finte state machine, your game loop might look like this

That's still a finite state machine. What you're talking about is the advantage of implementing a finite state machine with objects, not the advantage of having a finite state machine.

#5 lride   Members   -  Reputation: 633

Like
1Likes
Like

Posted 04 November 2012 - 11:04 AM


Without a finte state machine, your game loop might look like this

That's still a finite state machine. What you're talking about is the advantage of implementing a finite state machine with objects, not the advantage of having a finite state machine.

Thanks for catching my error. I didn't consider the definition of FSM
An invisible text.

#6 auryx   Members   -  Reputation: 122

Like
0Likes
Like

Posted 04 November 2012 - 01:48 PM

Thanks SiCrane and Iride for the replies. So, to use your examples SiCrane, what is the benefit of actually doing it that way? Is it just a "different" way to approach the architecture of the game? Or does thinking about the game in that way provide real benefits in terms of efficiency or clear coding?

auryx

#7 apatriarca   Crossbones+   -  Reputation: 1601

Like
2Likes
Like

Posted 04 November 2012 - 06:18 PM

A finite state machine is a quite general and abstract model for something whose behaviour can be described in terms of "states". There exists several different implementations and versions (they can be deterministic or not, they can be event based or not..). The use of FSMs is not motivated by efficiency or clear coding. These things depends on the specific implementation. FSMs are used because they can often model complex behaviours using a small and easy to understand description.

#8 auryx   Members   -  Reputation: 122

Like
0Likes
Like

Posted 05 November 2012 - 08:25 AM

Thanks Apatriarca, that's an excellent explanation. So it is essentially a modelling technique, I can understand that.

auryx.

#9 KaiserJohan   Members   -  Reputation: 1020

Like
0Likes
Like

Posted 05 November 2012 - 10:34 AM

Can anyone show a pseudo-code example depicting a FSM-driven game loop and a non-FSM-driven game loop? I'm not 100% how the latter would look

#10 EpicWally   Members   -  Reputation: 282

Like
2Likes
Like

Posted 05 November 2012 - 02:46 PM

Another user linked me to this a while ago. I think this will help solidify the concept, and show you a state machine in action in a simple game.

Edited by EpicWally, 05 November 2012 - 02:47 PM.


#11 leeor_net   Members   -  Reputation: 306

Like
1Likes
Like

Posted 05 November 2012 - 08:29 PM

Thanks SiCrane and Iride for the replies. So, to use your examples SiCrane, what is the benefit of actually doing it that way? Is it just a "different" way to approach the architecture of the game? Or does thinking about the game in that way provide real benefits in terms of efficiency or clear coding?

auryx


To answer your question directly, efficiency of execution is not the point or intent of a FSM. FSM's are used to break down game logic into easily managable units for the human programmers. E.g., it's useful for making your code easier to read, easier to understand and easier to maintain. It has the added benefit of making bugs easier to find (part of the maintenance I mentioned).

EDIT: I see apatriarca already beat me to this description... ;)

Edited by leeor_net, 05 November 2012 - 08:30 PM.

-The Legend of Mazzeroth-Project Lead, Lead Software Architect & Engineer, General Tasks and Professional Chef.

 

http://www.lairworks.com


#12 auryx   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 November 2012 - 10:18 AM

Thanks for the reply anyway, leeor_net - it's still useful to get different people's perspectives on this!

#13 AustinK   Members   -  Reputation: 107

Like
1Likes
Like

Posted 06 November 2012 - 12:03 PM

I want to take a shot at this. One, to try and help, and two, to be corrected if I'm wrong since I still consider myself a novice.

When I think of FSMs, I think of things like the previous posters have said, but I wanted to try and give a different visual to help you (hopefully not confuse you). For example, this is one path a user could take in a FSM:

Splash Screen -> Main Menu -> MultiPlayer Menu -> Find Room -> Game Begins -> Pause (Player Quits) -> Multiplayer Menu (Player backs out) -> Menu Menu -> and so on.

Now, Iride said something in his first post about a state having a subset. So, I will give you an example of some states I believe could have subsets, and those states would manage their own subsets.

Subset 1: Main Menu (Which, remember, is being managed by some other FSM)
Main -> Options (Player goes back) -> Main -> Help (Player goes back) -> Main -> Multiplayer is clicked, which effects the manager taking care of Main Menu (My above FSM in bold-green).

MultiPlayer Menu -> Find Room -> Game Begins -> Pause

Maybe the pause menu can have its own FSM?

So, that is my answer. I tend to think of FSMs as managers, and I usually give them that type of name (StateManager, MenuManager, etc). I hope I was able to help.

If anyone thinks I don't know what I'm talking about, please correct me as it's always fun to learn!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS