0 votes

I'm using the Input Mapping example to save and load buttons. But I'm having trouble saving values for gamepads.

func load_config():
    var config = ConfigFile.new()
    var err = config.load(CONFIG_FILE)
    if err: #Assuming that options.cfg doesn't exist.
        for action_name in INPUT_ACTIONS:
            var action_list = InputMap.get_action_list(action_name)
            #Since there are two control types, get data for both.
            var keyboard = OS.get_scancode_string(action_list[0].scancode)
            var gamepad = OS.get_scancode_string(action_list[1].scancode)
            config.set_value('input', action_name, keyboard)
            config.set_value('input', action_name, gamepad)
        config.save(CONFIG_FILE)

Using the above gives me an error "Invalid get index 'scancode' (on base: 'InputEventJoyButton')"

Read up about this in the docs, but can't quite to get it working. Saving keyboard keys works just fine.

in Engine by (230 points)

1 Answer

+1 vote

InputEventJoypadButton has no scancode but a button_index property.
Replace this line:

var gamepad = OS.get_scancode_string(action_list[1].scancode)

with this using Input.get_joy_button_string to get the string:

var gamepad = Input.get_joy_button_string(action_list[1].button_index)

If it's a Joy Axis then you need to use Input.get_joy_axis_string:

var gamepad = Input.get_joy_axis_string(action_list[1].axis)
by (4,221 points)
edited by

Thank you. That worked perfectly.

Interesting. The config file actually spells out the name of the key. (IE: Apostrophe) Is there a function that spits out the symbol by chance or do I need to code that myself? I'm asking due to the limited amount of space for text on my options screen.

You can implement a simple shortening function:

const DICT = {
    'Apostrophe': "'",
}

func shorten(key):
    return DICT[key] if key in DICT else key

Just call this shorten function after getting the string from Input:

keyboard = shorten(keyboard)

And you can add anything to the DICT when needed.

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.