ENIGMA Development Environment
Website is in read-only mode due to a recent attack.

Pages: 1
Author Topic: Using Joystick Functions in your game  (Read 552 times)
Offline (Male) hpg678
Posted on: December 06, 2018, 07:24:16 AM

Location: Barbados
Joined: Mar 2017
Posts: 275

View Profile Email
When developing your game/project, adding control for your gamepad can greatly enhance the fun playing. After all you may not want to use your keyboard all the time........at least for me anyways.

The gamepad functions don't work, you'll have to use the joystick functions. I did a short example using an Xbox One Classic controller which was modded for USB. My usual PS2 controller wasn't working well so i had to opt for this one. However a few days later, a friend brought one for me, so I now have two to use.

having written the code I did realize is that the Dpad and  Analog Sticks functions are very much the same for both controllers. The buttons assignment are different however. Bear that in mind when you assign buttons for either controller. You can create an options screen to do this.

To write the code, i used a program called 'jstest' on Linux a joystick/Gamepad diagnostic tool, which showed me the assignments for the buttons and analog sticks. From there I was able to match up what was what in the functions list and have it working.

the main segment of the code, i placed in the Draw event, as I wanted text to be written when I applied an action. Since then I had added graphics tp provide a better and more pleasant appearance. The code looks like this

Code: [Select]

if (joystick_exists(0))
var name;
name = joystick_name(0); //gets the name of the controller
// show_message(name + " is connected.");
draw_text(64, 32, name + " is connected.")
draw_text(65,33, name + " is connected.");

if !(joystick_exists(0))
  show_message("Joystick is not connected");
  //draw_text(64, 32, "Joystick is not connected")
// ///////////////////////////////////////////////////////////////////////////////

if (joystick_check_button(0, 0))
   //draw_text(448, 96, " 'A' button is pressed")
   draw_sprite(spr_highlight,0, 330, 200)
   draw_sprite( spr_highlight2,0, 462, 90)

if (joystick_check_button(0, 1))
   //draw_text(448, 128, " 'B' button is pressed")
   draw_sprite( spr_highlight,0, 360, 169)
   draw_sprite( spr_highlight2,0, 462, 129)

if (joystick_check_button(0, 2))
   //draw_text(448, 128, " 'Black ' button is pressed")
   draw_sprite( spr_highlight,0, 364, 229)
   draw_sprite( spr_highlight2,0, 462, 363)

if (joystick_check_button(0, 3))
   //draw_text(448, 128, " 'X' button is pressed")
   draw_sprite( spr_highlight,0, 299, 170)
   draw_sprite( spr_highlight2,0, 462, 168)

if (joystick_check_button(0, 4))
   //draw_text(448, 128, " 'Y' button is pressed")
   draw_sprite( spr_highlight,0, 330, 139)
   draw_sprite( spr_highlight2,0, 462, 207)

if (joystick_check_button(0, 5))
   //draw_text(64, 128, " 'White' button is pressed")
   draw_sprite( spr_highlight,0, 334, 252)
   draw_sprite( spr_highlight2,0, 462, 324)

if (joystick_check_button(0, 6))
   //draw_text(64, 128, " 'Back' button is pressed")
   draw_sprite( spr_highlight,0, 64, 213)
   draw_sprite( spr_highlight2,0, 462, 285)

if (joystick_check_button(0, 7))
   //draw_text(64, 128, " 'Start' button is pressed")
   draw_sprite( spr_highlight,0, 84, 239)
   draw_sprite( spr_highlight2,0, 462, 246)

Remember this was originally written using an Xbox One controller which has 8-Axis and 10 button assignments. My PS2 clone gamepad (DragonRise Inc. Generic USB  joystick) has 7-Axis and 12 buttons.

if you want you can use the code as a starting point. Feel free to improve it and use it as you want. hope it has been a little helpful to you.
« Last Edit: December 06, 2018, 07:41:30 AM by hpg678 » Logged
Offline (Unknown gender) ProtoLink[Glyphic Enigma]
Reply #1 Posted on: December 06, 2018, 11:12:11 AM

Location: United States
Joined: Dec 2018
Posts: 5

View Profile WWW Email
This is helpful when coding controller support, yes, but there are a couple of issues with this;

1. This only works if everyone who plays the game is using the exact same controller model
2. This doesn't take into account controller reassignment (which would ultimately fix the first issue).

Im going to work on making a controller reassignment script and/or object and Ill post the tutorial on my Youtube channel. I may even post it here as well.
An aspiring indie game dev, searching for how to do everything.
Pages: 1