How to do IF (a or b or c) == true?

Attention Topic was automatically imported from the old Question2Answer platform.

Doing FSM and sometimes you want the same logic to be applied for multiple states, for instance, aiming a weapon should be allowed when the player is idle, walking or running.

For such common situation normally I would do, as you guessed:

``````#AIM WEAPON - logic
func Something_Foobar() -> void:
if STATE == "IDLE" or STATE == "WALK" or STATE == "RUN": pass
# Allow aim weapon code check.
# Allow firing weapon.
``````

How I would go about on doing them like this: `if STATE == ("IDLE" or "WALK" or "RUN"):` or better even `if STATE == ("IDLE","WALK","RUN"):` or the inverse `if ("IDLE" or "WALK" or "RUN") in STATE:`

I would avoid using (`arrays` or `dictionaries` or `for`) loops just for this.

Arrays seem to be the correct answer here

``````if STATE in ["IDLE","WALK","RUN"]:
``````

Oh~ just remembered. Since you’re doing FSM you can also use `match`

``````match STATE:
"IDLE", "WALK", "RUN":
# Allow aim weapon code check.
continue
"FIRE":
break
"WALK":
#do only walk related stuff
break
``````

Pay attention to the “WALK” `STATE` being used multiple times
And this can also be all placed in one line if you’re one of those fanatics

Wakatta | 2021-10-31 18:50

In addition to the array suggested by Wakatta, a second alternative would be using `match`, especially if you’ve got more than one group with specific code:

``````match STATE:
"IDLE", "WALK", "RUN":
# Code when standing on the ground
"JUMP", "FALL":
# Code when in the air
"SWIM", "FLOAT":
# Code when in a liquid
``````