Can I assign a single value to a var from the list?

Godot Version

4.0 stable

Question

Hi! I’m new. I am creating a fighting game right now, and I made some decisions that led me to a huge if elif statements block. I decided to use match instead. The problem I ran into: I can’t understand if there’s a way to assign a single value from enum list of values based on the event.action list. I have inputCheck variable that gets an input and puts it in the inputReg array, I have enum INPUTNAME with, well, input names. I need to assign a single value from this enum to inputCheck in as few lines as possible according to event.is_action_pressed without huge elif statements. Basically:

func _unhandled_input(event):
    if event.is_action_pressed("button A", ..., "Button Y"): inputCheck = INPUTNAME.key()[INPUTNAME.A, ... INPUTNAME.Y]

I would appreciate any alternatives you can provide, but the priority is to resolve given problem in gdscript, as I may need something similar in the future, please!

Use a dictionary to map one set of values to another.

Unfortunately, I cant put a function inside a dictionary. Could you, please, extend what you mean?

You can actually put a function inside a dictionary entry, in the form of a Callable(see link).

Beside the fact that you can put functions into dictionaries as @hexgrid already noted, why would you need to do so? The pseudocode you posted appears to be mapping strings to enums. I don’t see any functions there.

I think the input API deliberately makes it difficult to simply match on action names. There is a reason for this. It is possible for a single InputEvent to represent multiple actions (e.g. the ‘A’ button is “ui_accept” AND ”attack"), which makes a match statement unsuitable (as it will only ever reach one clause). If you want to match on actions, you must first getba list of all actions the current event represents (via InputMap ultimately)