# Maintaining/Updating Large Amount of Player States

I am developing a fighting game to practice my skills in Java and I would like to know what would be the best approach for figuring out how to easily expand the amount of states a player can have without fiddling with a large amount of code. With a small amount of states such as, inAir, isHurt, isRunning, I can hard code all the conditions I need. Things like 'jumping' can only occur when '!inAir', etc. However, for my project there are a large amount of states I do not know how to organize. Here is a small diagram I redrew:

My approach is to make an enumeration called State and do a switch case between them. I am not sure if this is a good way of doing this and I am trying to learn a better method if there are any.

• a read of this may help, hopefully a more detailed answer comes along I am too interested. Aug 23, 2016 at 11:11
• What are the red arrows? Can't you turn when walking? Aug 23, 2016 at 13:14
• And how do you currently handle input and transition between states? Aug 23, 2016 at 15:56

I'd organise it similarly to what you described.

Pseudo-code:

enum totalStates = {STATE_1, STATE_2, ..., STATE_N};
totalStates state = totalStates.STATE_1;

void Update() {

switch (state) {

case totalStates.STATE_1:
// update STATE_1
break;

// ... other states ...

}

}

bool initState(totalStates newState) {

switch (newState) {

case totalStates.STATE_1:
// check if the object's data allow the object to move to STATE_1
if (canState1) {
state = totalStates.STATE_1;
return true;
}
return false;

// ... other states ...
}


So every time you want your object to move to a new state, call initState(totalStates.STATE_2) (or any other state) and if it returns true the object has moved to that state, if it returns false, the object refuses to move to that state. For example if a character is in the air, and user presses the jump button again, the program would ask the object to go to jumping state, the object would see it is already in the air, and refuse.

If you are worried about being scalable and organisation because you have too many states, you can separate state logic to use different functions, like updateState1() or updateState2() etc etc.

Alternatively, you could try to restructure your code so there are a few different State categories that are set independently, like Stance, Movement, IsDazed, etc. Also, you could make a State/SubState system where the SubState provides more detail on top of the general State.