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 pauseAgain, 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.











