Need help with match statements

NathuunnNathuunn Posts: 4Member
if event.is_action_pressed("object_zero"):
    object = 0
elif event.is_action_pressed("object_one"):
    object = 1
elif event.is_action_pressed("object_two"):
    object = 2
elif event.is_action_pressed("object_three"):
    object = 3

is there anyway i can put this into a match statement instead of elifs?

Comments

  • MegalomaniakMegalomaniak Posts: 2,738Admin

    Haven't used match but I'd presume something like the following:

    match event.is_action_pressed(x):
        "object_zero":
            object = 0
        "object_one":
            object = 1
    

    worth trying at least.

  • SIsilicon28SIsilicon28 Posts: 744Moderator
    edited July 24

    I don't think that would work, cause if it did, this wouldn't either.

    match typeof(x):
        TYPE_REAL: print("is float")
        TYPE_BOOL: print("is bool")
    
  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin
    edited July 24

    The documentation has a bit about match/switch statements in GDScript that may be helpful to look at.
    Edit: Looking at the example and the code above, I'm not sure there is a good way to use match/switch statements in this case, simply because each time you are calling a function with different parameters.

  • NathuunnNathuunn Posts: 4Member

    @TwistedTwigleg said:
    The documentation has a bit about match/switch statements in GDScript that may be helpful to look at.
    Edit: Looking at the example and the code above, I'm not sure there is a good way to use match/switch statements in this case, simply because each time you are calling a function with different parameters.

    i see, thanks for helping

  • NathuunnNathuunn Posts: 4Member

    @Megalomaniak said:
    Haven't used match but I'd presume something like the following:

    match event.is_action_pressed(x):
        "object_zero":
            object = 0
        "object_one":
            object = 1
    

    worth trying at least.

    i had thought of that but when i tried it, it didnt work

  • NathuunnNathuunn Posts: 4Member

    @SIsilicon28 said:
    I don't think that would work, cause if it did, this wouldn't either.

    match typeof(x):
        TYPE_REAL: print("is float")
        TYPE_BOOL: print("is bool")
    

    okay thanks

  • bitshift-rbitshift-r Posts: 57Member

    This is not a good case for match, but if the goal is simplification then it is a good case for a for loop:

    object = -1 
    actions = ["object_zero", "object_one", "object_two", "object_three"]
    for i in range(4):
        if event.is_action_pressed(actions[i]):
            object = i
            break
    
  • SIsilicon28SIsilicon28 Posts: 744Moderator
    edited July 25

    Great idea @bitshift-r ! Here, let me simply it further.

    for action in ["object_zero", "object_one", "object_two", "object_three"]:
        if event.is_action_pressed(action):
            object = i
            break
    

    Edit: oops! I forgot about the i variable. Looks like your original function will have to do for now. :/

  • bitshift-rbitshift-r Posts: 57Member

    Yeah, I miss things like Python's enumerate and tuple unpacking in GDScript:

    # reminder: this is python code, not gdscript
    for i, action in enumerate(["object_zero", "object_one", "object_two", "object_three"]):
        if event.is_action_pressed(action):
            object = i
            break
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file