Professional Documents
Culture Documents
file:///C:/arquivosdeprogramas/cifras/IRR_HTML_LINHA_DO_COD...
DevMaster.net
Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us
Introduction
File Not
Found
Applications of FSM
File Not
Found
What is a FSM?
If you ever seen a flowchart before, you can think of a FSM as the same thing. It has a set
of states that follow a certain path. A state has transitions to other states, which is caused
by events or actions within a state. Here is a real world example.
http://www.pdf4free.com
30/3/2007 21:33
file:///C:/arquivosdeprogramas/cifras/IRR_HTML_LINHA_DO_COD...
You are in the kitchen (state) and you felt a sudden urge to drink water. You then decide
to walk over to the cupboard and grab a glass. This is an event (walk to cupboard) and you
are now in a new state with new options available to you. You then open the cupboard
(action) and grab a glass (action). With the glass, you walk over to the sink (event) and
turn on the tap (action). Water into poured into the glass (event) and you then drink it
(action).
So in a nutshell, an event leads you to a state in which you perform an action or several
actions (though you do not always have to perform an action). So when you walk to the
cupboard, that leads you to the state "At Cupboard" where the typical action is "Open
Cupboard". To better demonstrate a FSM, a robot example will be used throughout this
tutorial.
Example - Robotics
With all things in life, careful planning is a must, especially with a FSM. The first step to
developing a FSM is to list all the known entities in your problem. Let us start with the
robot.
Figure 1.1: Bender, our robot
Here is Bender, as you may recognize him from Futurama. Bender would like to turn on and
off, walk, run, raise and lower its arms, turn its head, and talk. There are many other
features left to be desired, like ducking, jumping, repairing itself, reacting to environments,
give it emotions, etc As you can see, the list goes on and this is quite normal in a FSM.
Keep in mind however, as the term finite implies you will need to limit the scope of the
problem to a finite set of entities. For the purpose of this tutorial, a subset of functions is
chosen. Given these entities, we should construct a table listing all the possible events and
states associated with them.
Event
State
turnOn
Activated
turnOff
Deactivated (Idle)
stop
Stopped
walk
Walking
run
Running
raiseLeftArm
LeftArmRaised
lowerLeftArm
LeftArmLowered
lowerLeftArm
LeftArmLowered
raiseRightArm
RightArmRaised
lowerRightArm
RightArmLowered
turnHead
HeadTurned(direction)
speak
Talking(text)
http://www.pdf4free.com
30/3/2007 21:33
file:///C:/arquivosdeprogramas/cifras/IRR_HTML_LINHA_DO_COD...
Actions
Comment
turnOn
bootUp
Bender is turning on
bootUp
Activity
walk
goWalk
run
goRun
talk
say(text)
turnHead
raiseArm
raisingArm
Caused By
Will Effect
2-10
3-8
http://www.pdf4free.com
30/3/2007 21:33
file:///C:/arquivosdeprogramas/cifras/IRR_HTML_LINHA_DO_COD...
lowerArm
laweringArm
stop
powerDown
3-8
shutDown
1-9
10 turnOff
Example - Code
Knowing how to set yourself up for a FSM is one thing, going about programming it is
another. There are many ways to program a FSM, so the method I present here is one that
I just prefer. I tried to comment the code as best as possible so hopefully my coding style
won't be a strain on your eyes =)
How The Code Works:
There are two major classes.
1) State:
The state class is responsible for setting transitions, specifying actions, and
pointing to the right function that represents the action. In this code, a state
supports the 3 preliminary specifications: OnEntry, Do, and OnExit, and
supports an unlimited amount of OnEvent specifications. These specifications
are used to define your actions. This should help you practice with designing
much larger FSMs.
2) FSM:
The code does not make use of any conventional programming style. There are no switch
statements and there are no IF/THEN expressions. Everything in the FSM is controlled by
sending the correct events down the pipeline. After that, the FSM will choose the correct
course based on how you configured your states.
Also, I have not placed any fancy reactions in the code (such as guard conditions). I simply
want to demonstrate how state transitions occur and provide a clean and elegant interface
to handling FSMs. Feel free to add new features and reactions to them.
Click here to download the code (.zip)
Code was developed in Visual Studio .NET 2003
Conclusion
Finite State Machines are widely used in video games to supplement AI given its easy
structure and maintenance. When given the problem that requires finite number of
solutions, FSMs are definitely an easy method to approach. Just remember the format to
help you design and develop FSMs.
1) Review the problem. Write down all the entities involved
2) Design a state diagram from the work you did in 1)
3) Review the state diagram. Make sure it meets all the requirements and does not fail
under simple transitions.
4) Develop a state table to clarify the state diagram and correlate to the code.
5) Design the code.
http://www.pdf4free.com
30/3/2007 21:33
file:///C:/arquivosdeprogramas/cifras/IRR_HTML_LINHA_DO_COD...
Glossary
Here is a list of terminology used throughout the tutorial. If any of these seem unfamiliar to
you, it would be helpful to know what they mean before continuing on with the tutorial.
They are as follows:
1) FSM
2) State
3) Event
4) Action
5)
Transition
A collection of states and transitions that outline a path of actions that may
occur.
A state is a position in time. For example, when you are at the bus stop, you
are currently in a waiting state.
An event is something that happens in time. For example, the bus has
arrived.
A task performed given a certain event that occurred. For example, you enter
the bus.
A link between 2 states. May be unidirectional or bidirectional.
http://www.pdf4free.com
30/3/2007 21:33