How to map a joystick axis to the inputMap from code ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leo-pnt

Hi !
In the project settings > input map, it is possible to assign 4 axis per stick (which works perfectly fine)
But in code, InputEventJoystickMotion has only 2 informations per stick (vertical or horizontal axis).

How to set the third value (-1, +1) to set full stick mapping ?
It seems to be accessible from the editor internals but not from gdscript api.

SOLVED
for instance :

event = InputEventJoypadMotion.new()
event.axis = JOY_AXIS_0
event.axis_value = -1.0 # or 1.0, depends on the axis direction
InputMap.action_add_event(actionName, event)
:bust_in_silhouette: Reply From: Wakatta

Ok so InputEventJoystickMotion has two properties that i’m aware of axis and “axis_value
The axis property lets you know which axis is being pressed

for example:
JOY_AXIS_0 and JOY_AXIS_1 map my joypad’s Left analog stick (could be different for you)
and “axis_value” maps the direction that axis is being held:

for another example:

event.axis = JOY_AXIS_0: #horizontal movement (meaning left to right) 
event.axis_value = -4: a #negative value for left and positive value for right

for another extra example:

event.axis = JOY_AXIS_1: #vertical movement (meaning up and down) 
event.axis_value = -4: a #negative value for up and a positive value for down

Haven’t read the Docs but pretty sure its in there somewhere, the above info remembered from AHK

Yes, you can retrieve the information like that
(By the way, I get axis_value from -1.0 to 1.0)
But then let’s say I store this in a json, There is no way to
put those “negative-positive” information in the input map from code
I mean by using

event = InputEventJoypadMotion.new()
event.axis = JOY_AXIS_0
InputMap.action_add_event(action, event)

When I do that, event.get_action_strength() returns only positive values.
So I have lost the negative values of the stick in the process

Why -4 for the axis_value ?

leo-pnt | 2020-12-01 11:20

That -4 was my error drawing from memory

You’re correct that the axis_value strength can only be determined from within the range of -1 to +1

But why are you using event.get_action_strength() in lieu of event.axis_value?

Wakatta | 2020-12-01 15:28

Ok solved thank you very much.

(I use event.get_action_strenght() in my code to move the player)

Because what I want to do is allow the user to save custom controller axis mapping.
So when the event arrives, I need to know exactly where he is moving the stick and then store that information. So it representes 8 values in total (counting both sticks).
But there are only 4 available in godot.
Furthermore, in my configuration, I’m able to store only 1 number per event.
As the first axis is 0, I can’t save -0 because I lose the “minus” information.
So I created custom numbers:
-1000, -1001, -1002, -1003 which representes JOY_AXIS_XX with the minus sign

So when the user moves the stick, if event.axis_value is negative,
I can store -(event.axis + 1000)
Then when the file is loaded, if the stored axis is negative, I can do

event.axis = -(customAxis + 1000)
event.axis_value = -1.0

What really confused me is that axis_value is in fact the information that stores -1.0 and 1.0 so it gives the 8 values I needed. BUT they are not constants so when running the game it’s no longer -1.0 or 1.0.

By some sort of way, the game reads the inputMap, saves the axis_value somewhere invisible and use it to display the correct event.get_action_strenght()

leo-pnt | 2020-12-02 19:34