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

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Black_Chess_King

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.

Thanks for reading,

:bust_in_silhouette: Reply From: Wakatta

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":
        #allow reload check
        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

:bust_in_silhouette: Reply From: Mario

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
:bust_in_silhouette: Reply From: DaddyMonster

The previous answers are great but I’ve got two more approaches:

  1. OOP with inheritance:

This can handle transitioning states and general behaviour without branching at all.

Tutorials here: Finite State Machine in Godot · GDQuest and here https://docs.godotengine.org/en/stable/tutorials/misc/state_design_pattern.html

  1. Enums:

This is a numeric value so less than operators could be an easy solution.