# Multi States Machines or Booleans?

Attention Topic was automatically imported from the old Question2Answer platform.

hello guys, I came from Construct 2 so I’m a begginer programer, on construct I always used some bools to say what the character was doing like, is running, is attaking, with that, I could make an attack animation if the player was running and another if not, but state machines looks have just one state at the time, so I wondering if I should use booleans or more than one state machine, what is the best way to make something like “attacking and running at the same time” ?

I recommend a state machine with additional states.
For example.

``````enum Player { STANDING, WALKING, RUNNING, JUMPING, RUNNING_AND_JUMPING, etc }
``````

Also, if you give your enum the correct values, you don’t need the AND states.
Example of What I am talking about but in C#

``````#Power of base 2
enum Player {

STANDING = 1,
WALKING = 2,
RUNNING = 4,
JUMPING = 8

}
``````

thanks for reply! sorry I don’t get that second enum, about the first one, to make a “running and jumping” I need to copy the code of both states, has a better way to do this?

ManoD | 2022-03-13 00:42

Basically, an enum is just an integer and is represented in the computer a series of bits that can be either zero or one.

If you set each of the states to a power of two, you can use each bit in your interger to represent one state.

Now we can exploit that property to represent multiple states without having to explicitly make the states.

Example:

``````enum Player {
STANDING = 1,
WALKING = 2,
RUNNING = 4,
JUMPING = 8
}

var state = Player.STANDING #This means the variable equals 1 ->  0b0001
var state = state | Player.JUMPING #This means the variable equal 9 or 8 + 1 or a standing jump  -> 0b1001
var state = Player.RUNNING | Player.JUMPING #This means 12 or a running jump  -> 0b1100
``````

Now here is how you write your code to make this work.

``````#These need to be else if because it doesn't make sense to run , walk and stand at the same time
if(state & Player.STANDING != 0):
#Do STANDING Stuff
elif(state & Player.WALKING != 0):
#Do WALKING Stuff
elif(state & Player.RUNNING != 0):
#Do RUNNING Stuff

#Jumping can happen at any time
if(state & Player.JUMPING != 0):
#Do JUMPING Stuff
``````

To clear any state just use: state = state ^ Player.{INSTERT STATE HERE}

By following the above you wont have to have a bunch of variables and don’t have to repeat any code.

Hope this helped.