User events tutorial

Introduction to User events
User events are useful in event driven games. This tutorial describes ways to have a similar system running through a project by the use of objects. User events are extremely useful in the completion of required tasks. However, this doesn’t mean that the user must select whether to make their project event driven versus loop driven. Rather, the user can maintain a combination of both, and may certainly have to, as only 16 sixteen user events are permitted in Game Maker, and the user will soon run out of user events. Therefore, projects will nearly always be mostly loop driven, with the added possibility of an event driven supplement.

The Basics
This section contains some explanations of the user events. Although user events are generally uncomplicated, it may be useful to both experienced programmers as well as novices to have a more full understanding of the concepts involved with users events, as it will assist with the tutorial.

What is going to be accomplished
Since it is easier to learn with a less complex example, the tutorial will have an object that check if the left mouse button has been released, and if it has, call the user event of the other objects to change their color. This will show the basic principles of using user events.

Setting up the application
To begin, the user should create a room of any size. (The default of 640x480 will suffice) Changing the color is optional. Next, the user should add a sprite to the game. The sprite used in the tutorial is 32x32 and it will contain a variety of colors. The one used in the tutorial is a good example. After the sprite is selected, two objects should be created. The first will be referred to as "oBlock" and should be given the sprite the user created earlier. The second will be referred to as "oMouse" and can be given any random image. In the oMouse object, create a Global Left Mouse event. Whether it is activated on release, when pressed, etc. is up to the user. Since this is an event driven application the event can be called as many times that the program requires, without the program crashing. Then, the user should add an Execute Code action from the left panel in the Control tab. Below is an example of the tutorial code.

with(oBlock) event_user(0);

This will execute the user event 0 for oBlock.

oBlock
To begin creating the oBlock object, the user should add a create event. In that create event add an Execute Code item, and for that code what is desired is to stop the images from cycling through in oBlock's sprite. image_speed = 0;

Adding user events
This is a picture of adding a user event. To add a user event, click on Add Event, Other, User Defined, then User 0. Then, the user should start adding the code. Since the sprite has 6 images in it, what should be done is to pick one of them at random. This will make all the blocks random colors when the mouse is clicked. Here is the code used to accomplish this. image_index = floor(random(6));

image_index is the current frame of the sprite the object has. Since the image_speed is set to 0, the frame won’t change unless the user tells it to like was done in the code. floor rounds the number that random generates down because random will return a decimal number, and fortunately, frames do not go off of decimals.

Result
Here is the finished product:

GMK
Click for a GMK that contains the example described in this tutorial.