GameMaker Linguistic communication – Beginner'due south Guide



Contents:

  1. Introduction
  2. Events
  3. Variables
  4. Functions
  5. Conditions – if Statements
  6. Conditions and Functions
  7. Conditions – switch Statements
  8. The repeat part
  9. The while Loop
  10. The do…while Loop
  11. The do…until Loop
  12. The for Loop
  13. Arrays
  14. Conclusion


Introduction


Welcome! This tutorial is aimed at beginners who have lilliputian to no experience in GML or programming in general. It will introduce you to the nuts of programming and how GML works. After this tutorial, you'll be able to utilise GML to build your own games finer!

Bookmark this page now (Printing CTRL + D) as this guide is quite long and you will need to close information technology and come dorsum subsequently. Kudos if y'all can read it all in one go!

Click here to read the Russian translation of this guide, by Artem Darkov.



Events


events.PNG
A lot of events to choose from

You put code inside an event, and when that code runs depends on the type of event you put it in. In that location are a lot of events to cull from in GameMaker.

Hither's a quick introduction to the events we'll be using the most:

Create


Code within the Create effect is executed only once: when the case running the code is first created. Hither you lot can initialize well-nigh of the stuff related to your example.

Stride


The about of import and the near used issue – the Footstep event runs every step – if your game/room speed is prepare to 30, a Step effect will run 30 times in a second. This is used for things y'all want to go along on happening.

Draw


This effect is used to execute the describe codes. For instance, functions like draw_sprite, which is used to describe a sprite at a location, or draw_rectangle, which is used to draw a rectangle, only piece of work in the Draw event. The Describe event overrides the default draw of the instance, meaning your example won't exist fatigued unless you use draw_self().

Alarms


Alert events run after you gear up them. Then if I set Alarm 0 to sixty in the Create consequence, the code within the Alarm 0 upshot will run subsequently 60 steps.

Collision


While adding a collision consequence, you can select an object to create the event. That event volition only run when the instance running the code collides with any instance of the object specified while creating the standoff event.

Read more about events



Variables


Variables are containers that contain some value/data. They take a proper noun. For example, a variable named player_health may incorporate 100, or a variable named player_name may contain the player'due south proper name ("Peter", "Lindsey", etc.). It'due south upwards to yous what you lot want to name your variables and what yous want to store within them.

A variable in GML tin store:

  1. Numerical values – 100, 45.534, -42.2
  2. String values – "Teacher", "Peter"
  3. Boolean values – truthful or false

Example:


Initialization:

cost = 20;

Hither nosotros accept initialized a variable named price which contains twenty equally a value. If the variable has already been initialized before, this serves to change its value to 20.

Side annotation: Information technology is not required in GML to put a semicolon (;) afterward each statement. So feel free to skip information technology and focus on the master code.

At that place are many ways of assigning a value…

cost = 4 * five; price = 40 / 2;

To increment the value…

price += 20;

To decrease the value…

price -= 20;

To multiply, or to carve up…

price *= 2; price /= ii;

Use:


Yous tin also employ variables in mathematical expressions…

a = 4; b = 5; c = a + b;

Here, c would shop nine because of the expression a + b (which means 4 + five as a is four and b is 5).


Unlike Types of Variables


Local Variables

These variables are the ones initialized with the var keyword. They are discarded when the event they were initialized in ends. They tin only exist used inside that upshot only, unless initialized once again.

var price = 2;

This initializes a local variable called cost. Suppose the upshot this lawmaking was in were the Step event; so the variable could only exist used inside the Step event. If you endeavour to utilise it without initializing it in some other event, it would render an fault as information technology doesn't exist there.


Instance Variables

These are normal variables which are initialized by assigning a value.

price = twenty;

These kinds of variables can be accessed within all the events of the object/example it was initialized in afterward it has been initialized.


Global Variables

These are variables which tin exist accessed by all the objects in your game – hence the proper noun "global". There are two ways of creating such variables:

Initializing with the globalvar keyword…

globalvar toll; price = ii;

Once the variable has been initialized with the globalvar keyword, it tin can be used by any instance present in the room.

or use with global. prefix…

global.price = 2;

This way you don't have to initialize information technology with the globalvar keyword but take to apply the global. prefix every time you want to use this variable.


Built-In Variables


In that location are also some born variables which mean something special in GameMaker. Hither are a few examples…


Built-in Case Variables

These are the built-in variables which are unique to each instance. They can as well be known equally backdrop of an instance. Here are a few important examples…

                x: horizontal location of the instance inside the room (in pixels)                y: vertical location of the instance inside the room (in pixels)                speed: speed of the instance (in pixels per footstep)                management: the direction the instance moves towards (in degrees), default: 0                hspeed: horizontal speed (in pixels/step)                vspeed: vertical speed (in pixels/step)                image_angle: the rotation of the sprite (in degrees), default: 0                image_xscale: horizontal scaling of the sprite, default: i                image_yscale: vertical scaling of the sprite, default: 1                image_index: the sub-image of the sprite the example is showing                image_speed: the speed at which the sprite animates through its sub-images                sprite_index: the sprite used past the instance

You tin can modify or use these variables just like ordinary ones.

                //modify location to 200, 150                ten = 200; y = 150;                //brand sprite 2x bigger                image_xscale = 2; image_yscale = ii;                //rotate sprite half-way around                image_angle = 180;

Text that comes subsequently // is a annotate. Information technology has no effect on code; information technology's there so that yous can explain what your code does or write of import things you lot'd like to think when looking at your code.

You can write multi-line comments besides – only beginning them with /* and finish with */.


Built-in Global Variables

These are the congenital-in variables which are global in scope and same for each instance.

Here are a few examples…

                room_speed:                the number of steps run by a room in one 2nd, default: 30                score: the score in your game, can store any numerical value though                wellness: your player's health, can shop any numerical value besides                lives: number of lives, tin shop any numerical value as well

Here is a list of all the built-in variables in GameMaker.



Functions


Functions perform an activity and/or return a value based on the arguments provided in the parentheses that come afterwards the function name. If the function is supposed to just perform an activeness, it is written like…

function(arg0, arg1, arg2...);

…only if information technology besides returns something later performing the activity and you desire to store it in a variable, you practice it like…

variable = function(arg0, arg1, arg2...);

A function may or may non require arguments.


Hither are a few examples…

                instance_create_layer(ten, y, layer, object);                  What it does: Creates an instance of                                  object                                at position                                  x,y                                inside                                  layer                instance_create_layer(48, 48, "Instances", obj_enemy);                  What it returns: The case id of the instance created                enemy_id = instance_create_layer(48, 48, "Instances", obj_enemy);       draw_sprite(sprite, sub-epitome, x, y);                                  What information technology does:  Draws                                sprite                'southward                                sub-image                                  at position                                ten,y                draw_sprite(spr_ball, 0, x+v, y+five);                  Returns nothing.                                random(number);                  Does goose egg.  What it returns:  Returns a random real number between 0 and                                number                .                speed = random(5);


Conditions – if Statements


Conditions are used to command the execution of some code. Using conditions y'all can control whether or not a piece of lawmaking runs based on weather condition. if statements are the well-nigh commonly used conditions. Using if you can ensure that a piece of code runs only when a condition or a gear up of conditions is true.


Example


Say you're making a game, and you make a shop. Here, the player has to buy some upgrades. The first upgrade is a weapon upgrade. It costs 200 coins. So the player can only buy information technology if that condition is satisfied – that is, if they have at least 200 coins. For such a case, we can use the if condition:

if (coins>=200){                //buy upgrade                }

The > sign opens up to the side that is greater, and of grade = ways equal. So, past checking if coins>=200, we're checking if the coins are greater than or equal to 200.

So, the player can only buy the upgrade if they have enough coins. But what if they don't? We need to notify them that they need to get more coins. But that should only be specified when the condition is not satisfied. For that, we use else.

if (coins>=200){                //buy upgrade                } else{                //notify, non plenty coins                }

The lawmaking after else runs merely when the preceding if condition has returned false. And so if the actor has less than 200 coins, they volition be notified.

Yous can also put a condition after the else role, and then that fifty-fifty afterward the former condition has returned false, it would require some other status for the code after else to execute.

This way you lot can add together more else keywords and add different code for different conditions:

if (condition0){                //code0                } else if (condition1){                //code1                } else if (condition2){                //code2                } else{                //code3                }

If condition0 is true, code0 volition run, and the rest of the if statement will be skipped. Merely if condition0 is simulated, it volition move on to condition1. If information technology's true, it'll execute code1 and terminate. Only if it'south false also, then it'll move to condition2. If it's true, code2 will run, but if not, the if statement will finally move on to the final else part and seeing that at that place's no condition, execute code3.


Usage


In the previous example we checked if the coins were greater than or equal to 200. But the conditions tin can be used in many more ways.

                Checking an equal value:                if (coin==400)                For this condition to exist truthful, coin has to be exactly 400.                Checking a smaller value:                if (money<50)                For this status to be true, money has to smaller than 50 (at most                                  49.99..)                Checking if something is not equal:                if (proper name!="Curse")                If the player'southward proper name is CURSE, this condition will return false. So                                                  for this status to work, something should not be equal to a value.                                  Another example:                if (lives!=3)                True but if lives are not 3.                Checking a boolean is true:                if (paused==truthful)   OR   if (paused)                                  True only when a variable is true, here "paused".                                                  You lot can skip the "==true" role and just blazon the variable proper name                                  to check if it's true.                Checking a boolean is false:                if (paused==false)   OR   if (!paused)                                  True when the variable specified is simulated.                                  Exclamation mark (!) tin can be used as a prefix for a condition to flip it.                                  So if the status is imitation, it would return true.              


Weather and Functions


Functions can be used inside conditions likewise. They can either be checked for as booleans (returning truthful or simulated) or returning some specific value (number/string).

Here are a few examples to demonstrate how functions can be used inside atmospheric condition.

place_meeting()


The part place_meeting() can be used to check if there is a collision between the instance running the code and a specified object/case at a position. For example,

                code inside obj_player:                place_meeting(x, y, obj_wall);

This function would return true if obj_wall was colliding with obj_player at the latter'southward position. And then to cheque for collisions and execute some code, you lot would put this function into a status:

                obj_player Pace result:                if (place_meeting(x, y, obj_wall)){     speed = 0; }

When there'south a collision detected between obj_wall and obj_player at the thespian's location, it sets it's speed to 0.

instance_exists()


It returns true if an case of a specified object is present within the room.

                Pace event:                if (instance_exists(obj_player)){     score += one; }

The above code checks if an instance of obj_player exists in the room, and if it does, adds 1 to the score.

floor()


It floors a number specified inside its parentheses and returns the result. For example, 4.94 would become 4, 1.13 would become 1, and so on.

if (floor(image_index)==2){     image_index = 10; }

image_index stores the index of the sub-image the sprite is currently on. Sub-images are in integers, but the variable image_index is non. Then earlier checking what sub-image the example is on, yous demand to flooring the variable.



Conditions – switch Statements


As a beginner, switch statements probably won't exist of much use to you, but still, you should know about them.

In a switch argument, you lot first specify a variable, a part or a combination of those within a mathematical expression. And so you list all the possible results. The switch statement evaluates the expression specified and moves to the case that matches with the outcome. Information technology executes the code following the case until a break is found.

Here's an example:

switch(level){   case one: level_name = "Overworld"; break;   case 2: level_name = "Hole-and-corner"; intermission;   case 3: level_name = "H2o World"; break;   case 4: level_name = "Castle"; intermission;   default: level_name = "Unknown"; }

In this example, level is a variable that contains the level number the player is currently on. When level is 1, the switch will move to case 1. Information technology volition run the lawmaking, where it sets level_name to "Overworld". Then it encounters a break and stops the code.

If you don't utilize a interruption before starting another case, it will go on executing all the cases until a break is plant.

Similarly, when level is ii, case 2 will run. Same for instance 3 and 4.

But what if level matches none of these cases? In such a state of affairs, the switch volition move over to the default part, and run the code after it.



The repeat function


The repeat() function tin repeat a gear up of statements a specified number of times and is used in the aforementioned way as an if argument. Here's an case:

repeat(5){     coins += 1; }

You lot know what coins += 1 does: it adds one to the variable coins. But since we're using repeat(5) before it, the statement will be executed 5 times, ultimately adding v to the variable coins(1 * 5 = 5).

The repeat() function is a kind of loop, because it keeps looping until it meets an end. Go along reading to know more about loops.



The while Loop


So to start off – in that location are different kinds of loops and the while loop is just one of them. Since it'southward the simplest one, I'll exist explaining it first.

Loops are chosen so because they accept the ability to loop. Loops are just like the if statement, equally in they take a condition that needs to be fulfilled for the code following the condition to be executed. Here, have a look at an if statement compared to a similarwhile loop:

if (money > twoscore){                //lawmaking                }  while (money > 40){                //code                }

The if statement checks if money is greater than twoscore, then executes the following code. The while loop too checks the same affair, but the departure is in how loops work.

When the condition specified for a loop becomes true, the lawmaking following it is executed, and when that code block ends, information technology goes back to the status and checks it again. If information technology's true, and then it over again executes the code. Then back again and if it'southward true, again executes the code. Information technology keeps on doing and so and looping through the status and and then the lawmaking until the status turns imitation.

Let's take the case above. Say the value of coin becomes greater than 40. The while loop volition execute the lawmaking following it, and keep on doing so until it turns false. Then for the status to plough false, the value of money needs to become below or equal to xl.

while (money > 40){                //code                money -= 1; }

Now that's right. If nosotros're reducing the value of money by one each loop, at one point information technology's leap to go below 40 and finish the loop.

So, it'southward required that yous implement a fashion of eventually making the condition false and make the loop stop. If you don't, information technology'll become an space loop that'll never stop, and volition crash your game.



The do…while Loop


This is another loop and a variant of the while loop. Have a look at how it looks before I can explicate information technology:

practice{                //lawmaking                } while (condition);

No no, don't be dislocated. It's actually uncomplicated.

Remember how in the while loop we used to check for a condition before executing the code?

while (condition){                //lawmaking                }

In the do…while loop, the while (condition) role has just moved to the bottom, afterwards the lawmaking block has ended, and has been replaced past the keyword do:

                while (condition)                do{                //code                }                while (condition);

This is done considering the practice..while loop showtime executes all the code that is in the code cake, and and then checks for the condition to see if information technology's truthful and if it should loop again. If it is, it goes back to the top and executes the code block. Then again comes to the condition. This way it keeps looping until the status turns false, the deviation being that it first executes the code block without even checking for a condition.

There should be a semicolon (;) at the end of a practise…while loop because without 1, the ending while (condition) part tin get confused for the starting of another while loop.



The do…until Loop


The practise…until loop is the same as the do…while loop, the difference being that the status check in exercise…until is flipped. So in do…while, the loop would run over again if the condition were true, but in do…until, the loop would only run if the status were simulated.

Hither's an instance:

coins = 5; practice{     coins++; }until (coins==10);

It's as elementary as maxim it: "keep adding ane to coins until they are equal to 10". So this would keep on adding 1 to coins and when the condition given becomes truthful, which means the coins go equal to 10, the loop would break.

In GameMaker, you can only use do…until and not practice…while.



The for Loop


The for loop is merely like the while loop, as it checks for a condition first so keeps looping the code until the condition turns false. But information technology has some more than features. Take a look at its syntax:

for(init; condition; increment){                //code                }

There, you can spot the condition in the middle. Just what's all that other stuff?

It's basically for a loop variable. A loop variable in a for loop is a variable that determines how many times the loop will run. More than explanation coming up.

init is where you initialize your loop variable, every bit in give it a proper noun and a value. Runs but on the commencement loop.

condition is the condition that volition determine if the loop is run.

increment is where y'all prepare the loop variable to be increased or decreased by a sure value each loop. Runs at the cease of the loop.

for(i=0; i<iii; i++){                //code                }

Here's a detailed explanation of how this loop will get:

Then first I'one thousand initializing the loop variable i at 0 value. So there's this status checking if i is smaller than 3. Considering it is, which means the condition is truthful, the lawmaking will run. Once the code block has completed execution, the increment office volition run: meaning that i will be increase by i (i++).

At present, the code block has completed its run and i has been increased past i, which means it's 1 now (0 + one). The initialization part volition exist left because it simply runs on the first loop. Then information technology volition move on to the condition and cheque if i is smaller than 3. Because ane is smaller than 3, the status will become true and the lawmaking will be executed.

Again, after executing the code block, i will be added to i, making it 2. Then it'll motion on to the condition, and because 2 is smaller than iii, the condition will become true and the code volition be executed again. Then i volition go iii (ii + 1), and then the status will become faux because i is not smaller than 3, it's equal to information technology. Then the loop will stop.

So, the loop will run three times:

                1st loop:                i is 0. i<3 = true, runs. i++.                2d loop:                i is 1. i<three = truthful, runs. i++.                3rd loop:                i is ii. i<3 = true, runs. i++.                4th loop:                i is three. i<3 = false, doesn't run.

If you lot didn't grasp all of this, reading it a 2nd time might assistance.

Hither's some other example of a for loop:

for(i=three; i>0; i--){                //code                }

This one starts at 3 and keeps decreasing by one until it'south no more greater than 0. Can you calculate how many times information technology will run? Do so and mail your answer in the comments!



Arrays


Remember how variables worked? Yous could give them a name and shop some value…

coins = 10;

That's the corporeality of coins for one histrion. Only what if there are iv players and you lot have shop the corporeality of coins they each take? How would you do it?

coins0 = ten; coins1 = v; coins2 = 12; coins3 = 7;

Like that, correct? Storing all those values in different variables? That would work right, but there is another, ameliorate style of doing this: using arrays.

coins[0] = 10; coins[1] = v; coins[2] = 12; coins[three] = 7;

Arrays are only similar variables, as in they have a proper noun and store some value, but unlike variables, they tin actually store more than one variables (elements) under the aforementioned name.

To assign or access an chemical element inside an array, you put the chemical element id (a number) within square brackets after the array name. Like this:

array[id] = value; variable = array[id];

And then in the previous example, I added 4 elements (0, one, 2, iii) to the assortment coins. If I want to store the second element (with the value 5) in a variable called player_2, I'll do this:

player_2 = coins[1];

You can besides apply a variable in place of the element id within the square brackets, considering what matters is the value, not the keyword. And then I tin can also practice this similar:

i = one; player_2 = coins[i];

This way, you can also use arrays inside a loop:

for(i=0; i<three; i++){     money[i] = coins[i]; }

The above code performs the same function every bit the following ane:

coin[0] = coins[0]; money[1] = coins[i]; money[two] = coins[two];

Because the loop volition only run 3 times when the loop variable i is 0, i and ii respectively, the first 3 elements of the assortment money will go equal to the first three elements of the array coins.



Determination


That's all for the basics. To learn more astonishing things that yous tin do in GML, look at all the tutorials I accept on this website.

If you lot desire to learn making your ain games in GameMaker Studio 2 through a video course, cheque out my new grade on Udemy.

Got anything to inquire, or any suggestions to requite? Feel free to comment below. I'll respond to your annotate equally before long as I tin!

For more than help, bring together our Discord server here (no sign-up required). Nosotros can aid you with anything and teach you GML if you lot couldn't grasp something.

See you lot, and happy dev'ing!