Controller Support not working without Input.get_joy_axis


In both MovingShip and EightWayMovement, I needed to add these lines for controller support:

direction.x += Input.get_joy_axis(0, 0)
direction.y += Input.get_joy_axis(0, 1)

When I was originally experimenting with controller support in MovingShip, I thought I had to add input maps (Project Settings -> Input Maps), but now when I delete those and only have the physical keys, controller support still works, but only if I have those two lines above. 

Can you let me know:

a) Why controller support seems to need those two lines? What might I be doing wrong that it's not supported natively?

b) Why I don't need to add input maps for the controller? I went back and tested my MovingShip and it still worked. I thought it might be something to do with KinematicBody2D but that doesn't seem to be the case.

Here is my EightWayMovement script for reference; thanks in advance:

  • Anthony SOLER(AnthonyS) replied


    From what I understand from the course, there are two ways to configure inputs:

    1. Associate name (move_left, ...) to device buttons/axis through the Input Map settings and use these names to retrieve values in your code.
    2. Get the device buttons/axis values directly in your code.

    In the first case (1.), you must configure the Input Map as shown below:

    ap configuration.png

    Using this kind of configuration, in your code, you do not have to know if you are retrieving values from a Keyboard or a Controller. You simply use "move_left"  "move_right" "move_up" "move_down".

    # Based on the configuration in the Input Map, Godot will retrieve the value from the buttons/axis you set up.
    var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")

    In the other case (2.), you do not configure anything in the Input Map and manually tell Godot to get values from a device button/axis.

    direction.x = Input.get_joy_axis(0, JOY_AXIS_0)

    Here you are mixing these two approaches.

    • You configured the Input Map for the keyboard so when you are calling the get_vector method, it returns a value only when the keyboard is used.
    • You manually add values retrieve from Device 0 > JOY_AXIS_0

    I would recommend doing the configuration via the Input Map setting to centralize the Input management. However, I believe that detecting which device is used can be useful in the case where you want to adapt the HUD of your game (display keyboard key versus controller button to indicate to the user how he/she can interact with an item, ...)

    Have a good day,

    1 love
  • Nathan Lovato replied

    Thanks for the excellent explanation, AnthonyS! Indeed, in Godot, you can either use the input maps, or you can always listen to input using pure code. In the community, we favor the input map because it simplifies code, and it makes it easy to update input mappings later, or even to recreate an input remapping menu.

    1 love