Saving joypad buttons to config?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

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.

:bust_in_silhouette: Reply From: Dlean Jeans

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)

Thank you. That worked perfectly.

9BitStrider | 2019-06-16 12:54

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.

9BitStrider | 2019-06-16 15:09

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.

Dlean Jeans | 2019-06-17 13:24