Jump to content


Photo
- - - - -

Adding Joystick Support


  • Please log in to reply
18 replies to this topic

#1 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 04 February 2004 - 03:01 AM

Adding joystick support by Andrew Barber aka Chronic

Intro

In this tutorial i will show you a quick and simple method of adding joystick support to your already keyboard controlled game.

This tutorial was written for use in Game Maker 5.2. If you do not understand the functions and arguments they use, please consult the help file.

First off, i recommend you download a utility i made called Joystick Report, it can be found on my site on the utilities page. This will help you to find which button returns what value.


Setting up the object

Open the game you wish to add support to, and add a new script and name it "joystick_support".

Open the object you wish to control with the joystick, Add a step event if it does not already have one.

Call the newly made script from the step event with the following code.

joystick_support();

Writing the script

Open the "joystick_support" script.

This part of the tutorial will vary from game to game, choose which parts are suitable for you.


Normal keyboard events

If you are using normal keyboard events (not press/release) then you only need something like this..

switch (joystick_direction(1)) {
  case 102:          // Joystick right
    keyboard_key_press(vk_right);
    break;
  case 100:          // Joystick left
    keyboard_key_press(vk_left);
    break;
}
The number i've wrote for the case, is found using my Joystick Report tool, add more cases if needed.

I should mention that if your using this method, it might be best to use the joystick events instead of a single script. This will avoid needlessly running a script and help the speed of your game. Doing this will only require the keyboard_key_press function.


Press/Release/Normal events

In your game you might be using all 3 keyboard events for a certain key, for example you might set a sprite change in the "press event", move the object in a "normal event", and change the sprite again in the "release event"

Because there is no press/release events for joystick, you'll need make your own varibles to handle this.

Open your object again and add a creation event if its not got one already, and an "execute a piece of code" action. In it write...

joy_right = false;
joy_left = false;
Now go back to joystick_support script.

Because "no direction pressed" also generates a direction of 101, this can be our "joystick release" event.

switch (joystick_direction(1)) {
  case 101:
    if (joy_right = true) {          // Joystick release right
      joy_right = false;
      keyboard_key_release(vk_right);
    }
    if (joy_left = true) {          // Joystick release left
      joy_left = false;
      keyboard_key_release(vk_left);
    }
    break;
  case 102:          // Joystick press right
    joy_right = true;
    keyboard_key_press(vk_right);
    break;
  case 100:          // Joystick press left
    joy_left = true;
    keyboard_key_press(vk_left);
    break;
}

The script so far

At this point whatever method you have used, your object should be moving (at least) left and right. If you are having any troubles, recheck over the scripts for any typos and missing "break" statments.


Capturing Button status

Like for the movement, buttons can also have different methods of controlling your object. However, you should take in to consideration that your game will be played by people using different controllers to what you are using.

For example, if i made a game with 4 buttons, and someone with a 2 button joystick plays it. Obviously they will not have access to the other 2 buttons.

Therefore, you need to make sure the player has enough buttons to play this game correctly. More on this later.


Normal events

Adding button support is rather simple.

if (joystick_check_button(1,1)) { keyboard_key_press(vk_space); }          // Joystick jump
if (joystick_check_button(1,2)) { keyboard_key_press(vk_enter); }          // Joystick pause
Again, you could also use the joystick button events at this point.


Press/Release/Normal events

This is basicly the same as above, except an "else" statment is needed, and a few more variables like with directions.

Open the creation event of your object again, and add the following.

joy_jump = false;
joy_pause = false;

Back to the "joystick_report" script

if (joystick_check_button(1,1)) {          // Joystick press jump
  joy_jump = true;
  keyboard_key_press(vk_space);
}
else {
  if (joy_jump = true) {          // Joystick release jump
    joy_jump = false;
    keyboard_key_release(vk_space);
  }
}

if (joystick_check_button(1,2)) {          // Joystick press pause
  joy_pause = true;
  keyboard_key_press(vk_enter);
}
else {
  if (joy_pause = true) {          // Joystick release jump
    joy_pause = false;
    keyboard_key_release(vk_enter);
  }
}

Taking the script further

1) As i mentioned earlier, you should stop the joystick being used if it does not have enough buttons to play your game correctly This is done by checking the amount of buttons the joystick has using joystick_buttons(). Game Maker will does not need the joystick attatched to return the amount of buttons your joystick has. So this is also the time to check if its actualy connected.

To avoid any un-needed processing power, you should check this at the title screen and store the result in a global varible.

Title screen object:
if (joystick_exists(1) && joystick_buttons(1) >= 4) { global.joystick1_supported = true; }

Joystick controlled object:
if (global.joystick1_supported) { joystick_support(); }

2) If your game has 2 players, you can modify the script slightly by changing the joystick id arguments of the functions in the script to argument0, and calling the script with an argument of 1 or 2 where needed.

Thats about it

Thats all for this tutorial, i hope this has helped you with your game.
  • 4

#2 Armageddon Potato

Armageddon Potato

    GMC Member

  • New Member
  • 35 posts

Posted 13 June 2006 - 03:02 PM

How would I change this to make it also detect the D-Pad?
  • 0

#3 Big J

Big J

    GMC Member

  • GMC Member
  • 2849 posts
  • Version:GM8.1

Posted 05 August 2006 - 09:17 PM

You might need joystick_pov(). POV works a bit like direction (in degrees), except:

UP is 0.
DOWN is 180.
LEFT is 270.
RIGHT is 90.
UP LEFT is 315.
UP RIGHT is 45.
DOWN LEFT is 225.
DOWN RIGHT is 135.

So, you can use

if joystick_pov(1)=90
//move right code here

  • 0

#4 metalhead1023

metalhead1023

    GMC's only emo kid

  • New Member
  • 612 posts

Posted 06 August 2006 - 12:37 AM

POV is not the D-Pad the D-pad is the directional buttons not the two joysticks below
  • 0

#5 dankad

dankad

    GMC Member

  • New Member
  • 53 posts

Posted 13 August 2006 - 05:48 AM

i don't use programming much so how wuld i change the script so it would work going up and down as well??
  • 0

#6 70PercentOff

70PercentOff

    GMC Member

  • New Member
  • 4 posts

Posted 14 August 2006 - 04:07 AM

the D-pad of a lot of gamepads/joysticks is read as a POV in a lot of apps/games. [like gamemaker for example :D]
  • 0

#7 silent ken

silent ken

    GMC Member

  • New Member
  • 4 posts

Posted 24 June 2009 - 08:33 PM

Can you make an example? I can't get this right.
  • 0

#8 Alju5

Alju5

    GMC Member

  • New Member
  • 11 posts

Posted 08 December 2010 - 06:14 PM

Can you make an example? I can't get this right.


You are lucky man! :) I made a file that helps me later to create this system in every game that I want easily and if I had done it for myself then why not for others too who wants help like I wanted. :) So I formated this file to example. Here it is!

:)

Edited by Alju5, 08 December 2010 - 06:17 PM.

  • 1

#9 Adequate

Adequate

    GMC Member

  • GMC Member
  • 479 posts

Posted 11 December 2010 - 08:38 PM

Very good tutorial, we need more stuff like this.

Where?

On the GMC.

*puts on sunglasses, guitar music plays*
  • 0

#10 Alju5

Alju5

    GMC Member

  • New Member
  • 11 posts

Posted 24 December 2010 - 07:09 PM

Thank you! :) And I used this system from my new example in my new project: Mario Forever: Alju5 version that description is made with Ting_Thing Mario Builder and that executable is released with Ting_Thing Mario Builder EXE-Maker. I added this joystick system in my new project before I released this example and now in my new project there are also my own Intro.
I don't want to say that my game Mario controlling system is made by me or this that I used Game Maker in doing my own title, my own challenges but if you try this one you can see more how my worked out joystick system works in what ever games. Mario Builder by Ting_Thing don't allow using joystick, but my stand alone game does!

Download it to see...

So it must help!

Administrator post helped me in creating example and some others too but usually I don't get help from internet. Example is the best way to cross the practice! :)

Edited by Alju5, 24 December 2010 - 07:14 PM.

  • 0

#11 Criminon

Criminon

    Final Element

  • GMC Member
  • 174 posts
  • Version:GM:Studio

Posted 30 April 2012 - 01:53 AM

I doubt anyone is still reading this, but I had a few questions. When incorporating this into my game, I noticed the sprites would look choppy compared to using the normal keyboard. I completely got rid of using the joystick, because if you hit left, then right, then left again without letting go of the controller, it wouldn't let you go left again using the method stated above.

I decided to go full d-pad and ran into another problem. If you shift from left to up left using this method, the game will quickly say left=false and change the sprite to it's standing sprite for just a quick second. It makes the sprite look choppy. I tried putting a checker on there to say "if left is true, and you hit upleft, left doesn't equal false" Unfortunately, this didn't go over so well. Is there an easy way to make it do these checks?

if joystick_pov(1)=90 then  // Press right on d-pad
  {if joy_button_right=0 then
    {joy_button_right=1}
        keyboard_key_press(global.control_right)}
else
  {if joy_button_right=1 && joy_button_downright=0 && joy_button_upright=0  then           // Release right on d-pad
    {joy_button_right=0
        keyboard_key_release(global.control_right)}}
        

if joystick_pov(1)=270 then  // Press left on d-pad
  {if joy_button_left=0 then
    {joy_button_left=1}
        keyboard_key_press(global.control_left)}
else
  {if joy_button_left=1 && joy_button_downleft=0 && joy_button_upleft=0   then           // Release left on d-pad
    {joy_button_left=0
        keyboard_key_release(global.control_left)}}
        
        
if joystick_pov(1)=0 then  // Press up on d-pad
  {if joy_button_up=0 then
    {joy_button_up=1}
        keyboard_key_press(global.control_up)}
else
  {if joy_button_up=1 && joy_button_upright=0 && joy_button_upleft=0  then           // Release up on d-pad
    {joy_button_up=0
        keyboard_key_release(global.control_up)}}
        
        
if joystick_pov(1)=180 then  // Press down on d-pad
  {if joy_button_down=0 then
    {joy_button_down=1}
        keyboard_key_press(global.control_down)}
else
  {if joy_button_down=1 && joy_button_downright=0 && joy_button_downleft=0  then           // Release down on d-pad
    {joy_button_down=0
        keyboard_key_release(global.control_down)}}
        
        
if joystick_pov(1)=315 then  // Press up & left on d-pad
  {if joy_button_upleft=0 then
    {joy_button_upleft=1}
        keyboard_key_press(global.control_up)
        keyboard_key_press(global.control_left)}
else
  {if joy_button_upleft=1 && joy_button_left=0 && joy_button_up=0  then           // Release up & left on d-pad
    {joy_button_upleft=0
        keyboard_key_release(global.control_up)
        keyboard_key_release(global.control_left)}}
        
if joystick_pov(1)=45 then  // Press up & right on d-pad
  {if joy_button_upright=0 then
    {joy_button_upright=1}
        keyboard_key_press(global.control_up)
        keyboard_key_press(global.control_right)}
else
  {if joy_button_upright=1 then           // Release up & right on d-pad
    {joy_button_upright=0
        keyboard_key_release(global.control_up)
        keyboard_key_release(global.control_right)}}        
        
        

if joystick_pov(1)=135 then  // Press down & right on d-pad
  {if joy_button_downright=0 then
    {joy_button_downright=1}
        keyboard_key_press(global.control_down)
        keyboard_key_press(global.control_right)}
else
  {if joy_button_downright=1 then           // Release down & right on d-pad
    {joy_button_downright=0
        keyboard_key_release(global.control_down)
        keyboard_key_release(global.control_right)}}  


if joystick_pov(1)=225 then  // Press down & left on d-pad
  {if joy_button_downleft=0 then
    {joy_button_downleft=1}
        keyboard_key_press(global.control_down)
        keyboard_key_press(global.control_left)}
else
  {if joy_button_downleft=1 then           // Release down & left on d-pad
    {joy_button_downleft=0
        keyboard_key_release(global.control_down)
        keyboard_key_release(global.control_left)}}

  • 0

#12 trianglegames

trianglegames

    GMC Member

  • GMC Member
  • 36 posts
  • Version:Mac

Posted 16 May 2012 - 11:20 PM

Is there any reason that you know of that this code doesn't work on the Mac version of Game Maker?
  • 0

#13 fishdinner

fishdinner

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM:Studio

Posted 07 January 2013 - 04:59 AM

Thanks for this, helped me a great deal!
I did notice the issue Criminon mentioned, but I've *no* idea how to solve it.
  • 0

#14 frikiloko

frikiloko

    GMC Member

  • GMC Member
  • 260 posts
  • Version:GM:Studio

Posted 02 June 2013 - 06:54 AM

This tutorial is quite old,is it possible for GM Studio?

 

Should I replace the old joystick functions for new gamepad functions?,my game just uses direction and a single button...


Edited by frikiloko, 02 June 2013 - 06:59 AM.

  • 0

#15 xlordt_97248

xlordt_97248

    GMC Member

  • GMC Member
  • 156 posts
  • Version:GM:Studio

Posted 06 June 2013 - 07:08 PM

This tutorial is quite old,is it possible for GM Studio?

 

Should I replace the old joystick functions for new gamepad functions?,my game just uses direction and a single button...

 

I am not sure if using gamepad functions would recognize the joystick.. example.. I currently have a Street Fighter TE Fight Stick.. and using gamepad functions does not acknowledge it. Unless there is something I dont know.. yet the manual states nothing on how to go around it.


  • 0

#16 AshuraAlchemist

AshuraAlchemist

    GMC Member

  • New Member
  • 8 posts
  • Version:GM8

Posted 23 July 2013 - 05:50 AM

Ok, now how do I set it so that it recognizes Z, X, and C instead of enter or space?(sorry for bumping)


  • 0

#17 maximaxis

maximaxis

    GMC Member

  • New Member
  • 6 posts

Posted 03 May 2014 - 08:44 PM

How do you make use of the right stick of the joystick? For example y want my character to look at a direction depending on where you move the right stick and with the left stick it will move


  • 0

#18 Abbas Hussein

Abbas Hussein

    GMC Member

  • New Member
  • 2 posts
  • Version:GM8

Posted 30 July 2014 - 03:25 PM

:sweat: how i can make it like Hmmmmm........... mouse_check_button_pressed :thanks: 


  • 0

#19 Abbas Hussein

Abbas Hussein

    GMC Member

  • New Member
  • 2 posts
  • Version:GM8

Posted 01 September 2014 - 03:00 PM

:P


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users