Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

closed as primarily opinion-based by Krom Stern, bummzack, Anko, Seth Battin, concept3d Mar 18 at 8:33

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
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. –  Andon M. Coleman Mar 13 at 11:11

2 Answers 2

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.

share|improve this answer
    
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? –  Sleicreider Mar 17 at 22:43
    
@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? –  Daniel Carlsson Mar 18 at 21:48

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.

share|improve this answer

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