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.

While in development of a character, I feel like I'm digging myself deeper into a hole every time I add more functionality to him, creating more bugs and it seems like my code is tripping over itself all over the place.

What are the best practices when managing character states for a character that has a large selection of abilities and actions that they can perform, without their abilities interrupting each other and creating a mess overall?

share|improve this question
    
I'd start by revising the design; if you're having trouble keeping track of everything with insider knowledge imagine your poor players trying to figure it out =) In my case, when a system starts to feel too complex I take a step back work it out on paper before continuing - not only can I show paper to other people, it forces me to think ahead and justify each piece. Or you could just talk to the rubber duck c2.com/cgi/wiki?RubberDucking –  Patrick Hughes Jun 17 '12 at 19:20
    
I would definitely like to revise the design, I just don't know exactly what part of it is broken. The system is simple in design, but the manner I have it implemented seems to be poorly written because my character cancels actions for other actions, which isn't behavior that I find very desirable. –  TheBroodian Jun 17 '12 at 19:26
1  
I'd suggest fleshing your question out a bit more. What sort of states do you mean? What kind of problems do you encounter? Each game is different so there aren't really any best practices, just potential solutions for potential problems. –  Kylotan Jun 17 '12 at 19:55
    
Well, I would imagine this could apply to any character that has multiple abilities, actions, or states that you would like them to be able to execute at any time, however, only with one in use at a time. What is the best practice to keep these abilities separate from overlapping or creating unwanted bugs? –  TheBroodian Jun 17 '12 at 20:29
    
Like I said, there is no best practice, because every game is different. I'll post a vague answer below but what you're asking is very general. –  Kylotan Jun 17 '12 at 21:08

1 Answer 1

up vote 8 down vote accepted

If a character can only perform one action at a time, then you can use the State pattern to represent that action.

Basically this means that your character contains an object which handles the current state of the character, or specifically in this case, the current action. Typically you derive all your states from a standard base class or interface, which in a game might only have 1 method on it, an Update() function.

Add the action object when the action begins, call Update() once per frame or as often as you need to update the state, and remove the object when the action is complete.

share|improve this answer
    
Even with multiple actions he could use multiple state machines, like one for movement, another for attacking etc. –  Kikaimaru Jun 17 '12 at 21:28
    
I actually think this is closer to what I'm looking for, especially being as my character is 2D, he's not going to be able to move while attacking at the same time (at least that's the behavior I am aiming for) I will have a look at this, thank you for your input! –  TheBroodian Jun 17 '12 at 22:10
    
+1 for states. One of the best ways to keep track of objects (in my opinion) in a game environment. –  Jamie Taylor Jun 18 '12 at 16:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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