0 votes

Hello everybody,

I read and watched a lot of tutorial explaining how to bind new inputs to actions.
The objective was to autoload a config file containing the key bindings. I ended up with the following code (full script):

extends Node
var config_file_path : String = "res://keybinds.ini"
var config_file

var keybinds = {}

func _ready():
    config_file = ConfigFile.new()
    if config_file.load(config_file_path) == OK:
        for key in config_file.get_section_keys("keybinds"):
            var key_value = config_file.get_value("keybinds", key)

            keybinds[key] = key_value
    else:
        print("NO CONFIG FILE")
        get_tree().quit()

    set_game_binds()

func set_game_binds():
    for key in keybinds.keys():
        var value = keybinds[key]
        var actionlist = InputMap.get_action_list(key)

        if !actionlist.empty():
            InputMap.action_erase_event(key, actionlist[0])

        var new_key = InputEventKey.new()
        new_key.set_scancode(value)

        InputMap.action_add_event(key, new_key)
        print(key, " : ",  new_key)

The program autoload and run without error. However, the edited inputs don't work anymore after it ran. The tests seem to show that the problem comes from the last six lines:

    if !actionlist.empty():
        InputMap.action_erase_event(key, actionlist[0]) Remove the action from the key

=> This part works : inputs works correctly when I remove this line and don't respond anymore when I add it.

    var new_key = InputEventKey.new()
    new_key.set_scancode(value)

    InputMap.action_add_event(key, new_key)
    print(key, " : ",  new_key)

=> At this point, I get no more answers from the edited inputs (ie : the associated buttons don't reach any input(event)). Inputs that aren't configured by this file work fine.
I also tried to print(OS.getscancodestring(new_key)), but it returns nothing.

My config file (called keybinds.ini) contains :

[keybinds]

ui_accept=0
ui_cancel=1

To test my game, I simply added this code:

func _input(_event):
    if Input.is_action_just_pressed("ui_accept"):
        print("Accept command works")
 if Input.is_action_just_pressed("ui_cancel"):
        print("Cancel command works")

Another possibility is that I missed differences between keys and controller button bindings.
Thank you to tell me if you see any mistake.

Good evening.

in Engine by (16 points)

Please log in or register to answer this question.

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.