© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_6

6. Objects and Events

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 
Objects are the lifeblood of GameMaker. Your game will consist of lots of objects that will interact with each other and make things happen accordingly.
  • You will use objects for such things as:

  • Making players and enemies move and interact

  • For placing and executing code

  • Checking and changing variables to make things happen

  • Collision detection

  • Processing mouse and keyboard input

  • Drawing variables and sprites

  • Creating sound effects and playing music

Objects will generally consist of several events. Code is placed in these events to make things happen within your game.

Note

Objects are the items in the assets of the game assets tree in the IDE. When you place an object into the room it is referred to as an instance. This is an important distinction. For example, in the resources tree, you may have an object named obj_enemy, but have three instances of it in the room, each acting as separate entity.

The most important and commonly used events will be:

Create Event

This event is processed once when an instance is placed within the room (when the room starts) or upon creation using code from another object. The code may look like this:
/// @description example
hp=80;
motion_set(90,5);

which sets an initial value of hp and starts the instance moving in direction 90 at a speed of 5. It will continue moving at this speed and direction, until you tell it do something else.

Figure 6-1 shows what this code would like when placed in the Create Event.

An illustration of 3 panels. Object example has name, sprite, and collision mask. Events has a create examples and add events options. Object example create has 3 line code.

Figure 6-1

Showing code in create event

Mouse Events

These events trigger upon input from the mouse, such as clicking a button or scrolling the middle mouse wheel.

Note

Global mouse events allow actions to be performed if the mouse button is clicked anywhere on the screen, not just over the sprite of the object.

Standard mouse events trigger when clicked over the sprite assigned to the object (actually the mask set for the sprite).

Note

A mask is the shape/area that will be used for collision events and functions. You can set a mask as a shape using the sprite editor, for example, as shown in Figure 6-2.

A screenshot of a dialog box titled Sprite s p r text. It has a name, image sizes, texture settings, and collision mask. Highlights collision mask mode and tolerance as automatic and ellipse slow and exhibits a dollar sign on the right side.

Figure 6-2

Showing a basic circle sprite mask

The detection of mouse input can be done through GML code in a Step Event (as shown in a previous chapter), or through use of events.

Showing mouse events in Figure 6-3.

An illustration of available mouse events. It lists 14 options, selected the mouse options display dropdown menus with15 options. Enable the global option from the mouse option.

Figure 6-3

Showing the available mouse events

Quick Summary of the Mouse Events

  • Down Events – These events trigger whilst the mouse button is being held down.

  • Clicked Events – These trigger once when the mouse button is clicked.

  • Released Events – These trigger once when the mouse button is released.

  • Mouse Enter Event – Triggers once when the mouse enters the sprite area.

  • Mouse Leave Event – Triggers once when the mouse leaves the sprite.

  • Mouse Wheel Events – Triggers once when the mouse wheel is moved up or down.

  • Global Mouse Events – Work in a similar way to the top three events previously, but will trigger anywhere within the game window.

Destroy Event

This event triggers when the instance is destroyed, which can be in code from itself or set from a Collision Event with another object. The code for making an instance destroy itself is:
instance_destroy();
In the Destroy Event you could put:
audio_play_sound(snd_ouch,1,false);

This will play the sound snd_ouch once when the instance destroys itself.

Alarm Event

Alarms will trigger when they have counted down to 0. (They then go to -1 unless reset to a new value.)

An alarm will lose a value of 1 for each step of the game (in GameMaker there are by default 60 steps per second). So, an alarm set at 180 will trigger after three seconds. For example, the following would play a beep sound every two seconds.

Create Event:
alarm[0]= game_get_speed(gamespeed_fps)*2;

This sets the alarm to twice the room speed, which is equivalent to two seconds.

and in an Alarm0 Event:
audio_play_sound(snd_beep,1,false);
alarm[0]= game_get_speed(gamespeed_fps)*2;

This plays a sound then sets the alarm back to two seconds. It will countdown and repeat the sound every two seconds.

Draw Event

Your code actions for drawing should be put here, drawing text, shapes, or sprites.

If there is other code within a Draw Event you will have to tell GameMaker to draw the sprite, for example, with:
draw_self();

which will draw the currently assigned sprite and index.

Drawing order is from back to front, so in the following would draw the sprite then the text, the text will appear over the sprite, for example:
/// @description Draw sprite and text
draw_self();
draw_set_font(font_text);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_set_colour(c_red);
draw_text(x,y,"Score "+string( score));

This will draw the currently assigned sprite and the text Score followed by the score.

An example would be like that shown in Figure 6-4, assuming you have added this sprite to the object.

An image of an animated monster with horns and a wide open mouth. The text above the image reads, score 80.

Figure 6-4

Drawing text above a sprite

Step Event

With the default settings, the Step Event is processed 60 times per second. The following code would check the player's health and reduce the lives’ value and reset the health. If the player is out of lives, they are taken to the room room_game_over.
if health<0
{
      lives-=1;
      health=100;
}
if lives<=0 room_goto(room_game_over);

This will check if the health is below 0. If it is, it reduces lives by 1 and resets health to a value of 100. If the player is out of lives (equal to 0), the player will then be taken to the room room_game_over.

Key Events

These events work in a similar way to the key press code shown previously in this book. You may want to use code or events, do whatever you find easiest depending upon your game setup and design. Events can make it easier to organize your code, but perhaps harder to apply any changes needed.

Note

There is nothing wrong in using keyboard events over GML code. In fact, sometimes it is preferable as it keeps your project more organized.

Figure 6-5 shows the options of Keyboard Events for keyboard interaction.

An illustration of add event has 14 options and selected a key down option. The key down option has 16 options.

Figure 6-5

Showing key press events

Keyboard Events work in a similar way to Mouse Events, with a distinction between down, pressed, and released.

Collision Event

A Collision Event happens when two instances collide (actually, when their collision masks collide).

Upon a collision between two instances, code in the event will be executed. If you create two objects, obj_enemy and obj_player and assign them a basic sprite, you can set a Collision Event, as shown in Figure 6-6.

An illustration of add event has 14 options and selected a collision option. The collision option has objects with object enemy and player.

Figure 6-6

Setting up a collision

Some example code, for example, increasing the score by 1 for each frame the collision happens, as shown in Figure 6-7.

An illustration of the collision code has two panels. The event has an example code of object enemy. Object player of object enemy has 2 line code of example code.

Figure 6-7

Example collision code

Draw GUI Event

This event draws independent of any view, and above any drawing that happens within a main draw event. It is generally used for HUD elements that do not interact with other instances in game. Some example code would be:
/// @description Draw hud
draw_set_colour(c_white);
draw_roundrect(50,50,1316,718,true);
draw_roundrect(70,70,270,140,true);
draw_set_font(font_text);
draw_set_halign(fa_middle);
draw_set_valign(fa_center);
draw_text(170,105,"Score "+string(score));
which would look as shown in Figure 6-8.

A screenshot of game maker studio 2. It has two animated characters of monsters with horns and a score of 0.

Figure 6-8

Showing a basic hud drawn with Draw GUI

Basic Projects

  1. A)

    Allow keyboard movement of a player object. Draw health as a value over the player. Draw this health in white, changing to red when the player's health is less than 20. Allow P and L keypresses to change the health.

     
  2. B)

    Draw text that changes color each time the spacebar is pressed.

     
  3. C)

    Create an object that changes color when the mouse hovers over it. Use a different subimage for each color.

     

Advance Project

  1. D)

    Create a mini game with three instances of an object. Make them move in a random direction upon start. If player clicks them, award points and move in a new random direction.

     

Useful Functions

You can force an event to happen, for example:
event_perform(ev_keypress, ord("X"));
which would run any code in an event set up for detecting a press of the key X. See event_perform in the manual for all available options.
event_inherited()
This allows the instance to inherit code from a parent object. So, for example, if you have a parent object with the following code in a Create Event:
/// @description Parent Create Event Code
size=6;
and the child also has a Create Event, you can force it to also run the parent’s code, for example, with:
/// @description Child Create Event Code
type=8;
event_inherited();

It will also get the value of size from the parent.

Summary

You should now understand commonly used events and what they are used for. You should have a strong basic understanding of the code you can put in such events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.89