#Controller support through code

1 messages · Page 1 of 1 (latest)

molten wren
#

Quick question, I'm trying to get my controller working through code, if I add an event using the project settings, it just works, if I add it through code it doesn't.

This is my current code (only the important parts (to my knowledge)) (got most from a guide that didn't cover controller support.)

const controls: Dictionary[String, Array] = {
    "walk_left": [KEY_LEFT, KEY_A, JOY_AXIS_LEFT_X],
    "walk_right": [KEY_RIGHT, KEY_D, JOY_AXIS_RIGHT_X],
    "jump": [KEY_UP, KEY_W, KEY_SPACE, JOY_BUTTON_A, JOY_BUTTON_B],
    "ground_pound": [KEY_DOWN, KEY_S, JOY_BUTTON_LEFT_SHOULDER]
    }
var key_to_add
for action in controls:
    if not InputMap.has_action(action):
        InputMap.add_action(action)
    for key in controls[action]:
        key_to_add = InputEventKey.new()
        key_to_add.keycode = key
        #do i have to add something to allow all devices? (Like the button in the project settings)
        InputMap.action_add_event(action, key_to_add)

goal: 
var direction: float = Input.get_axis("walk_left", "walk_right")
but working with controllers too and not just wasd

Ps: I'm new to posting questions, if anything is unclear or I did something wrong please let me know. :)
Have a nice day!

#

It already works with wasd, the code is placed within the correct functions (ready, physics_process and so on)

covert kiln
# molten wren Quick question, I'm trying to get my controller working through code, if I add a...

I tried to do smth similar, if you have time you could check this
https://github.com/MarageDev/YAMIH
Maybe it can help you ( I can't help more rn because I have an issue too on another project )

GitHub

YAMIH ( Yet Another Multiplayer Input Handler ) is an attempt at creating a more modular approach to Godot's Input system, especially for local multiplayer games. - MarageDev/YAMIH

molten wren
#

Alr I'll look, ty.

#

Yeah I don't think I understand github enough to find anything I can make use of there.

upper turret
#

controllers aren’t handled as keys. You’ll likely need InputEventJoypadMotion/Button events

covert kiln
molten wren
#

Do you know a good guide that would explain controller support from the ground up in that case?

upper turret
#

sorry, I’m not aware of anything. You could try search for godot starter projects. Not tutorials, but project templates. Some of them have a lot of settings and menus already set up, so might be doing a similar thing to you

covert kiln
molten wren
upper turret
#

I’m sure I’ve seen simpler

covert kiln
#

yes simpler projects must exist

molten wren
#

going on a break so i wont check back here for a few hours fyi

sand wyvern
#

Once you set a key trough the InputMap, you can check the properties of the event assigned in the project.godot file. That way you can see the class and properties that the controller event produces and how to reproduce it.

You are really better of leaving the default inputs to the editor's InputMap editor. If you want to add rebindable actions, you can just select an action name and wait for the user to press something.

molten wren
#

no way

#

i think i actually solved it

#
var test = InputEventJoypadMotion.new()
    test.device = -1
    test.axis = 0
    test.axis_value = -1.0
    InputMap.action_add_event("walk_left", test)
#

gotta try to map the entire controller

#

YOO IT WORKS!

#

I can walk left and right now, using only the script and the joystick

#

Now I just gotta try to map buttons

#

and after that Ill mark this as completed

#

ground_pound got executed when I pressed the home button, all according to the script

#

ill send the updated script

#

its a bit longer

but the current version is like this, perhaps I'll optimise it later:

const base_controls: Dictionary[String, Array] = {
    "walk_left": [KEY_LEFT, KEY_A],
    "walk_right": [KEY_RIGHT, KEY_D],
    "jump": [KEY_UP, KEY_W, KEY_SPACE],
    "ground_pound": [KEY_DOWN, KEY_S]
    }

func _ready() -> void:
    _add_keybinds()

func _add_keybinds():
    var key_to_add
    for action in base_controls:
        if not InputMap.has_action(action):
            InputMap.add_action(action)
        for key in base_controls[action]:
            key_to_add = InputEventKey.new()
            key_to_add.keycode = key
            key_to_add.device = -1
            InputMap.action_add_event(action, key_to_add)
    var test = InputEventJoypadMotion.new()
    test.device = -1
    test.axis = 0
    test.axis_value = -1.0
    InputMap.action_add_event("walk_left", test)
    test = InputEventJoypadMotion.new()
    test.device = -1
    test.axis = 0
    test.axis_value = 1.0
    InputMap.action_add_event("walk_right", test)
    test = InputEventJoypadMotion.new()
    test.device = -1
    test.axis = 1
    test.axis_value = -1.0
    InputMap.action_add_event("jump", test)
    test = InputEventJoypadMotion.new()
    test.device = -1
    test.axis = 1
    test.axis_value = 1.0
    InputMap.action_add_event("ground_pound", test)
    test = InputEventJoypadButton.new()
    test.device = -1
    test.button_index = 0
    InputMap.action_add_event("jump", test)
    test = InputEventJoypadButton.new()
    test.device = -1
    test.button_index = 1
    InputMap.action_add_event("jump", test)
    test = InputEventJoypadButton.new()
    test.device = -1
    test.button_index = 5
    InputMap.action_add_event("ground_pound", test)