Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

stilltjack

FSM in games

This topic is 5765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howdy all, I''ve been doing the beginner game programming thing for five or six months now and I''ve noticed that all the examples in the typical game programming books model all of their programs as simple finitie state machines. This has worked for most of my smaller titlescreen\gameloop\endscreen type games. However, I have recently been writing a larger final fantasy clone and this approach has left my code rather messy and confusing to read. Still, I cannot conceive of any other realistic model for a game. Can somebody give me some general ideas here. thanks, dave

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Well FSM is pretty useful most of the time. The only other alternative I can think of off hand would be a message passing setup. Look around the net for tutorials on how to setup a message passing subsystem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Roof Top Pew Wee
What exactly is a finite state machine?

Just not familiar with the vocabulary.

--Vic--


A finite state machine is one of those type of abstract terms that defines a way of managing things based on object''s states.

An example would be:


  
enum gameState { Running, Paused, Finished }

gameState game.currentGameState = Running;

while( game.currentGameState != Finished )
{
if( game.currentGameState == Running )
{
// Update your game here.


// Check to see if the user paused the game.

if(keyDown == ''p'' || keyDown == ''P'')
game.currentGameState = Paused;

// Quit out of our loop we are done.

if(keyDown == WM_ESCAPE)
game.currentGameState = Finished;

}

if( game.currentGameState == Paused )
{

// Don''t do any game specific stuff until the user

// presses p to unpause the game.

if(keyDown == ''p'' || keyDown == ''P'')
game.currentGameState = Running;

}
}



That is a basic FSM. FSM''s are used used for sprite states, dead. alive, shooting ect. This makes it easier to manage.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Question: can I type html in this box and have it show up correctly?
Answer: <img src="wink.gif" width=15 height=15 align=middle>
I''ll just have to see...

Share this post


Link to post
Share on other sites
There''s lots of ways of implementing FSMs. You could have a series of ''if'' statements (like the above post), or a ''switch'' statement. But these can get large, cumbersome and difficult to update (especially if you''re in a team).

In C, you could use function pointers. Write a separate function for each state, ensuring each function has the same prototype. Then, in your game object, have a variable that holds the current function pointer (state). Finally, in the object''s update routine, call the function through the pointer. Changing state is accomplished by changing the function pointer.

In C++, replace function pointers with class pointers, and define a class for each state, where each class is derived from the same base class.

Care must be taken when changing from one state to another and ideally shouldn''t happen whilst in the state code (especially in C++ - you don''t want to delete the object that is being executed!). The best method is to return a value from the state handler that indicates if and what state change is required.

In C++ you''d probably want to overload the new/delete operators for the base class and implement a free list of some sort.

Functionality common to all states should be implemented at the object level and not the state level. In C++ you could derive your objects from an FSM base object that handles all your FSM updates. Also, implementing a state stack would be a good idea as well.

This is just a rough guide to get you started, the FSM design pattern is widely used and can be applied to many elements of game development (front end menus, game status, AI, etc..).

The important thing is to design a framework that lets you tailor the system to specific needs, then you can use the code, with minor modifications, on all your future projects. Modularity and reusability are the keywords here.

Skizz

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!