AGS Development > Editor Development

[EXPERIMENTAL] Gamepad in AGS - v4

(1/5) > >>

Experimental Editor with AGS with Gamepad support!

Download Editor Here: >><< | Code Source | GitHub Issue

Here's just a gamepad check, running in the browser: gamepad_check web example
Here's the AGS Game Project of the above:

ok, this is going to take a long time, but hopefully we can figure out an API that is alright. The above editor adds a new struct in AGS, which is a Gamepad! A Gamepad maps different types of joysticks to the good old Xbox 360 controller.

I plan to later support manually figuring the joystick by querying every piece of it, which will be named a RawGamepad, which SDL2 names as joysticks, but if you have no idea what you are doing, Gamepad should be fine.

Gamepad Static Methods


--- Code: Adventure Game Studio ---static int Gamepad.GetCount()Get the number of connected joysticks. No joysticks should return 0!


--- Code: Adventure Game Studio ---static Gamepad* Gamepad.Open(int index)Attempts  to open a gamepad, returns null if it's not a valid gamepad! If a joystick is way too different from a Xbox360 this could fail too.
This is how you can connect to a Gamepad, you pass an index, which corresponds to the number of the joystick - if you actually have a Xbox360 Joystick, it's the green light.


--- Code: Adventure Game Studio ---static String Gamepad.GetName(int index)If the index is a valid gamepad, get it's name!

Gamepad Instance Attributes and Methods


--- Code: Adventure Game Studio ---bool Gamepad.IsConnected()True if gamepad is really connected.


--- Code: Adventure Game Studio ---bool Gamepad.IsButtonDown(eGamepad_Button button)checks if a gamepad button is pressed, including dpad.

Possible buttons:

* eGamepad_ButtonA
* eGamepad_ButtonB
* eGamepad_ButtonX
* eGamepad_ButtonY
* eGamepad_ButtonBack
* eGamepad_ButtonGuide
* eGamepad_ButtonStart
* eGamepad_ButtonLeftStick
* eGamepad_ButtonRightStick
* eGamepad_ButtonLeftShoulder
* eGamepad_ButtonRightShoulder
* eGamepad_ButtonDpadUp
* eGamepad_ButtonDpadDown
* eGamepad_ButtonDpadLeft
* eGamepad_ButtonDpadRight

--- Code: Adventure Game Studio ---float Gamepad.GetAxis(eGamepad_Axis axis, optional float deadzone)get gamepad axis or trigger, trigger only has positive values. Values varies from -1.0 to 1.0 for axis, and 0.0 to 1.0 for triggers.

You can optionally pass an additional parameter to use as deadzone. If an axis absolute value is smaller than the value of deadzone, it will return 0.0. Default value is GAMEPAD_DEFAULT_DEADZONE, which is for now 0.125, use the name if you need a number.

Possible axis and triggers:

* eGamepad_AxisLeftX
* eGamepad_AxisLeftY
* eGamepad_AxisRightX
* eGamepad_AxisRightY
* eGamepad_AxisTriggerLeft
* eGamepad_AxisTriggerRight

--- Code: Adventure Game Studio ---readonly String Gamepad.Namegamepad name.


* v1: initial release
* v2: GetAxis now returns a float
* v3: GetAxis now has an additional dead_zone parameter. Default value is GAMEPAD_DEFAULT_DEADZONE, which is for now 0.125, use the name if you need a number.
* v4: Using correspondent A,B,X,Y buttons can skip Speech and Display messages, as long as you Connect to the Gamepad.

Wow!. Super useful. Nice job @eri0o

Thanks Mehrdad! I still need to figure more things, but I wanted to throw together a basic one, since with SDL2, we can roll joysticks for all platforms at the same time when support is in the engine itself. :)

Also sketching the API with users because things on the Engine are hard to change.

Small update, just switched GetAxis to provide a float value. An example usage is added in the code of the example project as follows:

--- Code: Adventure Game Studio ---#define DEAD_ZONE 0.125float gamepad_mouse_x, gamepad_mouse_y;bool update_mouse;bool prev_button_a_pressed; void repeatedly_execute_always(){  if(pad != null)  {    if(pad.IsButtonDown(eGamepad_ButtonA) && !prev_button_a_pressed)    {      mouse.Click(eMouseLeft);    }        prev_button_a_pressed = pad.IsButtonDown(eGamepad_ButtonA);        float x_lstick = pad.GetAxis(eGamepad_AxisLeftX);    float y_lstick = pad.GetAxis(eGamepad_AxisLeftY);      if( x_lstick < -DEAD_ZONE || x_lstick > DEAD_ZONE ||        y_lstick < -DEAD_ZONE || y_lstick > DEAD_ZONE )    {      if(update_mouse) {        gamepad_mouse_x = IntToFloat(mouse.x);        gamepad_mouse_y = IntToFloat(mouse.y);        update_mouse = false;      }            gamepad_mouse_x += 10.0 * x_lstick;      gamepad_mouse_y += 10.0 * y_lstick;            mouse.SetPosition(FloatToInt(gamepad_mouse_x, eRoundDown) ,                         FloatToInt(gamepad_mouse_y, eRoundDown));    } else {      update_mouse = true;    }  }}

Oh, man! This is exactly what I need right now for my projects. Very exciting to see this, would love to get my games working on SteamDeck!


[0] Message Index

[#] Next page

Go to full version