0 votes

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" ?

sorry bad english :P

Godot version 3.4.2
in Engine by (19 points)

1 Answer

0 votes

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


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 {


by (98 points)

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?

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.

How an integer is represented in a computer

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


enum Player {

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.
To better understand how this works read this wikipedia page
I also recommend watching a youtube video about binary and bitwise operators on youtube if you need even more information.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.