Sign in to follow this  
riruilo

Question about character state managment in a 2D platform game.

Recommended Posts

Hi friends! I'd like to know your opinion... I'm programming my character state machine and have some questions. See how in this game, the main characters manages his sword when jumps, waits, walks... SNES ActRaiser - Fillmore http://www.youtube.com/watch?v=YLUlbnc11_w (1:00) How can I do that? walking or jumping and at the same time using a sword? My character have some states, let's say: enum STATE { FALLING=0, JUMPING_UP, JUMPING_DOWN, WALKING_ON_FLOOR, WAITING_ON_FLOOR, PUSHING, NUMBER_OF_STATES }; Should I create some states like: WALKING_ON_FLOOR_USING_SWORD WALKING_ON_FLOOR WAITING_ON_FLOOR_USING_SWORD WAITING_ON_FLOOR? Using that, should I have some problems with foot animation? Should I use nested state machines? How? Any advice/ideas are welcome. Thanks a lot for your time.

Share this post


Link to post
Share on other sites
You can do something like this : -

Make a function (func1) that will be called everytime you call updategame function.

void func1(entity temp)
{
switch(states)
{
case state1:
//code for state 1
case state2: //code for state 2

.
.
.
.
.
etc
}
}

You can make this type of function for each type of entity to manage its state.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chetanhl
You can do something like this : -

Make a function (func1) that will be called everytime you call updategame function.

void func1(entity temp)
{
switch(states)
{
case state1:
//code for state 1
case state2: //code for state 2

.
.
.
.
.
etc
}
}

You can make this type of function for each type of entity to manage its state.


That's what I already have, my problem (and question) is how can I combine or use together WALKING and HITTING (or not HITTING)
Perhaps using flags as BigFreak says... I don't know, but in this case, I have the same problem with animations, I mean, a strange frame skip. Do you understand me?

How do real 2D game solve this problem? Maybe a couple of state machines, one for legs and another one for main body.. no idea.

Any suggestion is welcome, thanks.

Share this post


Link to post
Share on other sites
Dear friend.

I understand your problem , as I'm facing the same issue wright now.

Well, today My approach to deal with this kind of "mixed state machine" is to write
separated state machines that are executed in parallel: I guess your "using sword" state is more complex than a simple state. It can have several sub-states that help controlling the animation for example. So I would do your character update function someway like this:

UpdateCharacter()
{
ExecuteWalkingOnTerrain()
ExecuteUsingSword()
ExecuteAnithingElse()

}

where each Execute... function is a separate state machine.

I think that approach works very well if you consider using state variables of one state machine to control another. For example:

ExecuteUsingSword()
{
if( Walking_state == jumping)
UsingSword_state = Stop
else
UsingSword_state = atack
}

It is only a naive view of the solution. I think it may be necessary to write more generic state machines that control more specific ones. For example, you can have a ExecuteUsingObject function that calls the specific state machine depending on the object the character have on its hand.

As I said, I'm still solving the question in my game. So please send a mail to me if you find another solution.

arturapps@gmail.com

Share this post


Link to post
Share on other sites
I can't speak for all games, just the ones I've been a part of.

Most commonly, I've seen character state broken up into multiple state machines or state machines and sets of flags. This is mostly for internal state management and is organized to keep internal state management as simple as possible, but the information is used to determine what animation to play. The animations frequently still end up being strictly enumerated so there's an animation for every unique state combination. You could break the character into multiple sprites that are animated independently based on separate state machines, but you risk making the character look stiff and unnatural, like in ActRaiser.

Whether you allow animation hitches or movement that isn't matched to the animation is entirely a design question: Do your animations take precedence over player control or not? It's not a binary function, and just about every game chooses a different mix between code-driven movement and animation-driven movement.

Share this post


Link to post
Share on other sites
have a state machine for your general character state (on terrain, walking, jumping, falling, dazed), one for your weapon system and sword wielding. If you take quake3 as an example, the feet animation state is separated from the torso animation, the head is another component, and you could make the arms another one as well.

Dont do specific states like WALING_USING_SWORD and JUMPING_USING_SWORD, it will just snowball into a lot more states if for example, you can throw ninja stars (and who doesn't want to throw ninja stars), grapple, crossbow.

If you are, for example, falling, and you want to block the sword state to a fixed state (where the player cannot hack and slash), then it's easier to control.

Share this post


Link to post
Share on other sites
It makes sense. A pair of state charts running in parallel, one for legs, and the other one for the rest of the body. That should useful for animation control, but I wonder if I have to create another state chart for general logic control or I may use just one of the body (body or legs)

I will try to implement 3 in parallel, thanks for reply. Any idea is always welcome.

Thanks mates ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this