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?

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

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.

@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

@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

@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

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

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. :/

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
2 years later