Hello!
Let’s dive into the world of game development with Game Maker 2. And today we’ll start with options in Game Maker. Let’s create a new project by selecting New Blank
and naming its variables.
Object
Parameters can be added to objects. The object can be almost anything, starting with the hero and enemies, and ending with objects that control some logic in the game. Right-click the Objects folder in the Assets Browser to create an object and select Create -> Object.
An object named Object1 will be created. It needs to be renamed immediately. It is recommended to add a prefix to the name because if we have an object with the name hero and we want to add a sprite that will also be called hero, we will get an error that a file with that name already exists. Therefore, each object must have the obj_ prefix. Thus, the object that I will create will be called obj_vars.
After creation, the object will open immediately. Here you can specify a sprite for the object, specify whether it will be visible, and a few more options. Now we are interested in the events window. This is where we will write the code that will control our object.
Click the Add Event button and select Create. Event type Create
is called when the object is created and it is convenient to set some initial characteristics here. Double-click the left mouse button to open the code editor.
Local Variables
In the editor, we will create heroExp
parameters, which will be responsible for the experience of our object. These parameters are of type integer. We will also add the parameter heroName
, which will be of type string, and the parameter heroAlive
, which will be of type bool
heroExp = 50
heroName = "Maksym"
heroAlive = true
Now let’s add another type of event called a draw. It is responsible for displaying the object. To do this, click Add Event
again and select Draw -> Draw in the list. I will open the editor for this type of event and write the following in the code
draw_text(20, 20, heroExp)
draw_text(20, 40, heroName)
draw_text(20, 60, heroAlive)
The draw_text
function is used to display text, the first two parameters are the x and y coordinates, and the third parameter is the text to be displayed. Let’s now add our object to the room and try it and see what happens.
Add the object to a room
In the Assets Browser
you need to open the Rooms
folder and double-click on Room1
. The room window will open. In the top left, Instances
must be selected to be able to add objects to the room. Now our object can be simply dragged into the room. It is also worth changing the background color so that the text displaying the object is visible. To do this, in the room options, you need to select ``Background’’ and choose a color (Color option). I chose black because the text will be white.
Now that everything is ready, press F5 to start the game.
Dynamically change variables value
Now let’s try to add an event that will change the value of our parameters. Let’s add the Key Press - Up
event, which will be called every time the Up
button is pressed on the keyboard, and the Key Press - Space
event. The up button will add experience to our object and the space button will kill our object.
Add the following to the Key Press - Up
code
heroExp++
And next code to Key Press - Space
heroAlive = false
And let’s start the game. If you press Up
or Space
, the parameters will be changed.
Global Vars
Local parameters are available only within one object. If you want to add a parameter to which all objects will have access, you need to add global.
before the name of the parameter. Example
global.heroAlive = true
And now this parameter can be changed or used in other objects