0
\$\begingroup\$

What do you guys recommend to use? A State Machine with function pointers or classes?

Or, in which case one would be better than the other one?

At work we use the state machine with function pointers only and I've never tried to use one with classes before.

\$\endgroup\$
  • 1
    \$\begingroup\$ Bjarne Stroustrup would probably tell you something to the effect, "you are designing your system using C mentality ... that's not an effective use of the language." :P I actually agree in this situation, usually I find he pushes C++ language features a little bit excessively, but there is no reason not to embrace OOP and generic programming here when your language is C++. Templates and classes will improve code reuse and maintainability tremendously if you take the time to work with them. \$\endgroup\$ – Andon M. Coleman Mar 13 '15 at 11:11
1
\$\begingroup\$

The main difference is that with classes you can store information that only applies to that state in the class itself, whereas with function pointers you need to store it "somewhere else", hopefully in a closure or possibly in the main object using an associative array/map/dictionary.

In a programming language with closure support the difference is much smaller and in the end your choice might come down to which looks more natural in your language of choice or which is more efficient during execution.

For instance in C++, Java and C# I would use classes, it is the natural fit. You should tag your question with which programming language you are talking about if you want more specific information.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ would be a state machine with classes good for a wasd movement system?, because i would have to check for all key inputs in every class right? \$\endgroup\$ – jeromintus Mar 17 '15 at 22:43
  • \$\begingroup\$ @Sleicreider One option would be to use a controller class (as in model-view-controller) that checks for key presses and forwards this to the game classes, through a listener interface if necessary. I'm not quite sure why you would need to listen in "every class" even if you don't use a controller class though, only the player class would need to check the keyboard surely? \$\endgroup\$ – Daniel Carlsson Mar 18 '15 at 21:48
0
\$\begingroup\$

Both are good approaches. If you are working on the logic of a simple NPC character, and have 2 states, Idle and OutOfScene. With a State Machine class that update a state enum, know the state of the character is too simple. And in the "Global Loop" of the character logic, your only task is checks the machine states. But if the character is a little more complex, for example, when we are close of them begins a animation. Now our logic have a new state called "speaking". So, It's very convenient listen when happens character change of state. So, If you store a function pointer to a function called "doSpeakAnimation", when the character change to the state "speaking", just call to "doSpeakAnimation" and your code will be more elegant. My conclusion is that. The right approach depends of your necessity.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.