about FSM

I am new game programmer,i have been read about FSM in game design,but i dont really understand why we need it in game essentially???

long answer short, you don’t need them. but sometimes they are very useful, because they make programming life a lot easier.
it sorta… is a way to get out of coding-hell. just keep them somewhere in the back of your mind till you really need them.

FSMs can be thought as Manager+Proxy classes that always do keep a reference to one object or state. Possible states, the effects of each state, possible transitions and transition effects can be all handled in one abstract class.

One example can be found in the manual. There you have an actor that can do specific things like swim, walk and the like. Each state implies specific behavior.

Example 2:
That object varies over time, but you always have one or none objects. You could, for example, have a sky manager class that you tell to load a particular sky model. once you want to change that sky, you tell the manager to do so and it replaces the old one. You save yourself unneeded references to all the particular choices and instead only keep a manager reference plus you abstract the whole process of setting things up.

Example 3:
A button has different visualisations for ‘disabled’, ‘normal’, ‘mouse over’, ‘mouse over + button pressed (click)’ and ‘active (as in “you are where you wanted to be”)’. Making a menu with lots of buttons and other elements, you don’t want to specify these states and all the effects over and over. You implement a button as a FSM and connect it to a mouse listener or some other event handler. What stays is a self-contained object that you create and not care about anymore.

Example 4:
In my project I use a manager class for switching scenarios, which are kind of chapters in the game. You can only be in one scenario at a time. The transition is a loading screen and preloading of models. If this FSM goes to the state off, either I delegate that to the top level FSM that handles the whole game or I simply quit the game.

This might help: