So first off, I'm not sure if it matters but I am using SFML and C++. I've been wanting to write a game and I've read a lot on FSMs and what they do/make things easier for managing games, etc. What I gathered so far is that a state machine can be set up for each entity as well as the overall game so we are transitioning between states. What I am having trouble with is actually starting off and making a basic implementation of this. So I'd appreciate it if someone can point me in the right direction.

  1. How do I implement multiple state machines to handle different aspects of the game? For example for characters, for menu/loading/splash/credits etc. If I understood it correctly I would need something like an entity class, and then derive/inherit from it in order to make characters/mobs with various AIs? Is that the right way? Can I do it in a simpler fashion?

  2. How do I handle transitions if I don't want to use a switch statement? It seems like a naive approach if a more object oriented method exists. What is going to do the switching, do I have to remember the previous state or only know next state?

Thank you in advance!

  • \$\begingroup\$ This is broader than it seem. However, not something that can't be handled. Start by noticing that state machines are an abstraction. We may consider a fragment of imperative and sequential code a state machine (each state is executing an statement). Of course, you need an abstract state machine. Using switch would work for that. More OO? Have a look at state pattern and strategy pattern, of course. But, let me present to you an abstraction a level above state machines: behavior trees. Start here: aigamedev.com/insider/presentation/behavior-trees \$\endgroup\$ – Theraot May 4 '18 at 15:26
  • \$\begingroup\$ A link to something relevant for state pattern. stackoverflow.com/questions/14676709/c-code-for-state-machine \$\endgroup\$ – Rivasa May 4 '18 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.