Karla News

Game Maker 8 Tutorial: Keyboard, Mouse and Joystick Input

Drag and Drop, Game Maker, Joystick

Every game in the entire history of gaming receives the users input through either the mouse, keyboard, or some sort of joystick or controller. Detecting user input controls things such as character movement, shooting, reloading, and a host of other actions. In Game Maker there are two ways of detecting input from the user. The first is through drag and drop actions, and the second is by making use of Game Maker’s built in GML coding language. This tutorial will deal mainly with keyboard and mouse input, and will only touch lightly on joysticks and controllers. Proceed to step one below to begin.

Step One – Using Drag-and-Drop Actions to Retrieve Input

Using drag and drop actions, just as in the GML language, to check for input can be performed in any active object, be it either the player object, a sole object for controlling all input, or if each object has its own input retrieval processes.

You can only use events that are always running to detect input. For example, the step event or the draw event. So, in the object you wish to use for input reading add the step event if you do not already have it added.

Perhaps the easiest way to check for mouse input is to simply click on the add event button, select mouse, and click the desired event. Then add any actions into the newly created event.

Another, more complex, but more flexible way for checking if mouse buttons are pressed is, on the right side of the screen, select the ‘control’ tab. In the control tab drag and drop the ‘Check Mouse’ action into the actions portion of the screen. A window will pop up allowing you to select which button you wish to check for, left, right, or middle and to select whether it is or isn’t pressed. Now, add a start and a finish block action under the mouse checking action and place whatever action you want to be executed between these.

See also  Word Count Plus Plugin for Firefox

The fastest way to check for keyboard input is to press the add event button, click keyboard, and select which key you want to check for. Game Maker will make a new event for this specific key where you can place actions.

For a more flexible and in depth look at keyboard, mouse and joystick input continue below to the next step.

Step Two – User Input through GML

As with the drag and drop based events in step one, Game Maker’s GML code, when used for detecting user input, should only be place in either of the step or draw events.

To detect keyboard, mouse or joystick input with GML, simply create a ‘Execute Code’ action in your input object’s step event. You will place the proper GML code in this new action. Given below are Game Maker’s GML keyboard functions:

keyboard_check(vk_key) – This function will return true(1) or false(0) if the key vk_key has been pressed.
keyboard_lastkey – This function returns the keycode of the last pressed keyboard key.
keyboard_key – This keyboard function returns the keycode of whatever key is currently pressed, if any.
keyboard_check_released(vk_key) – This function will tell if the given key has been released.

Here are Game Maker’s GML functions for mouse input:

mouse_check_button(button) – Checks if the given mouse button is pressed or not.
mouse_check_button_released(button) – Checks if the given mouse button has been released or not.
mouse_x – Returns the value of the mouse’s current vertical or ‘x’ position on the screen.
mouse_y – Returns the value of the mouse’s current horizontal or ‘y’ position on the screen.
mouse_button – Returns what mouse button is currently being pressed.
mouse_lastbutton – Returns the last mouse button that was pressed.
mouse_wheel_up – Returns if the mouse wheel has been scrolled up wards.
mouse_wheel_down – Returns if the mouse wheel has been scrolled down wards.

See also  How to Create a Service Invoice Using Microsoft Word

Finally, the GML function for joysticks and controllers:

joystick_exists(number) – Checks if the joystick number is currently plugged in and available.
joystick_check_button(joystick number, button number) – Checks if the given joystick’s given button is currently pressed.

For movement controls on joysticks and for further information on all of these events and actions please consult the Game Maker 8 manual.

Conclusion

Making proper use of one of the methods given in the above steps, you can easily incorporate user interaction with all of your Game Maker 8 games. Using GML is probably more efficient then the drag and drop actions method, but actions are easier to use then GML. Its generally best to choose whatever one method suits you the best.

NOTE:

It is typical in the Game Maker community to ask for credit, however these scripts are provided absolutely free and require no credit what so ever.