0 votes

I am making a 4-player local co-op game. I have 4 full sets of all of the inputs, each with a suffix representing the player number, i.e. moveup1, moveup2, etc.

Each player has an input action to switch directly to one of 9 weapons, for a total of 36 weapon-related inputs.

Currently what I have started writing is this:

func weapon_action(event):
    if event.is_action_pressed("weapon11"):
        return {"weapon": 1, "player": 1}
    elif event.is_action_pressed("weapon12"):
        return {"weapon": 1, "player": 2}

Is there a good way to get the pressed weapon # and player # without having 36 if...elif branches?

Godot version 3.5
in Engine by (15 points)

1 Answer

+1 vote
Best answer

You could store the info in a Dictionary and process that instead. So, something like:

var input_info = {
    "weapon11": {"weapon": 1, "player": 1},
    "weapon12": {"weapon": 1, "player": 2},
    #......
}

func weapon_action(event):
    for key in input_info.keys():
        if event.is_action_pressed(key):
            return(input_info[key]
by (19,298 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.