GameMaker: Studio

GameMaker: Studio

43 ratings
Extremely Basic Coding Concepts (GML)
By pax
Learn the very first coding principles you need to know and how to use them in Game Maker Language, GameMaker's own coding language.
2
   
Award
Favorite
Favorited
Unfavorite
Introduction
GML (Game Maker Language) is GameMaker's offering for those who would rather code in a type-based language than with the Drag'n'Drop system. It allows for a greater control and (once you get aquainted with it) readability.
An example of GML looks like this:

if keyboard_check(vk_up)&& place_free(x,y-4) { y-=4 } if keyboard_check(vk_down)&& place_free(x,y+4) { y+=4 } if keyboard_check(vk_left)&& place_free(x-4,y) { x-=4 } if keyboard_check(vk_right)&& place_free(x+4,y) { x+=4 }

Hopefully, by the end of this guide, you will be able to understand what this means.
In this guide I will cover:
  • variables
  • 'if' statements
  • keyboard input
  • alarms
  • object location changing

I do not intend to cover every concept in coding. I do not even intend to cover a few of them. This is simply what I believe to be the very basics needed for game programming in GML.

NB: I learnt in C#. Some practices here (like the semicolon on the end on commands) may not actually apply, and are redundant (but not code-breaking).

Please comment!
Variables
Variables are like buckets. They hold things.
A variable in most cases contains a number, but sometimes it can be another form of value storage. This number (we will refer to this as the value from now on) can be changed according to instructions given to the variable.
Say the number in the variable was 12. Using code, that number can be changed into 10. Or 18. Or 1,002,359. When you change a variable in GameMaker using the Drag'n'Drop system, it activates code within the GameMaker program which changes the variable in your computer. Sometimes it is in a piece of software, clicking a button or dragging a slider can activate the code that changes the variable. Any form of data that is created in a program is stored in variables (with the exception of files).
Variables have a multitude of uses, too many to name here. They can be used to hold scores, where the player avatar is, what colour something is, etc. You will find out in the next section how to use them to make decisions.

To change a variable in GML, you need to do two things.
  1. make it exist
  2. tell the computer to change it

So, before you can change a variable, you have to tell the computer what it is.
First, once you have created your object, select an event, usually the 'Create' event. Then select the 'Control' tab and under the code section drag and drop in the 'Execute Code' icon.



Double click this, and an editor will come up:













Think of a name for your variable. It should be relevant and not too long. It is recommended to add '_var' suffix or another suffix to denote it as a variable in case you have an object, sprite, sound or something else with the same name (GM does not allow two things with the same name to exist).
I chose the name test_var. Type this into the code editor:
test_var=0
Variables are often started at 0, but you can choose any value that you need to. You can even set a variable to another variable among other things.

When you need to change the variable, simply do the same piece of code but with a different value. It's that simple:
test_var=100

If you need to change a variable in an object that is not the one you are currently working in, type in the object's name plus a period, then type the variable:
tester_obj.test_var=100
Variables do not work across all objects unless you use this method because they are what we call 'private', which means they only apply to the object they're in.

You can make a variable global by typing global.theVariable when you make it like so:
global.test_var=0
This means that you do not have to type the object's name when you want to use the variable across different objects. So you would change a global variable the same way you do for a private variable.
IF Statements
An IF (read 'if') statement is a way of telling the computer what decision to make based on certain specific factors. These are often either variables or triggers.
If statements can easily be turned into human language. IE,
if (test_var=7) { instance_destroy() }
becomes "If the test_var variable has the value of seven, destroy the instance."
IF statements can easily contain hundreds of lines of code, or just one. The syntax of an IF statement goes as so:
if (//put your variable here// = //put the value here//) { //put your code here }
**Words that come after ' // ' are called comments, they are ignored by GameMaker. Beware, though, if you put one in the middle of a line of code (which is seriously not recommended) you need to put the rest of the line on a new line. The general ettiquette for comments is on a line of it's own and with a blank line above it.**
Both the parts of the section in parentheses can be variables that are not obviouly variables (such as the X and Y locations of an object among others), and the second part (the value) can also be a variable.
So you could have
if (test_obj.x=test_var) { instance_destroy() }
That means "if the X coordinate of the test_obj object is equal to the value of the test_var variable, destroy the instance.

There is also the option for the variable not equaling the value presented. This is called an IF NOT statement, and looks like this:
if not(test_var=2) { instance_destroy() }
This means that if test_var does not equal 2, then destroy the instance.

IF statements can also have one or more variables and their values in the parentheses. There are IF OR statements, and IF AND statements.
IF AND statements look like
if (test_var=2 && test2_var=2) { instance_destroy() }
This means that if test_var equals 2 and test2_var equals 2, then destroy the instance.
IF OR statements look like
if (test_var=2 || test2_var=2) { instance_destroy() }
This means that if test_var equals 2 or test2_var equals 2, destroy the instance.
**The || are double bars, found by holding SHIFT and pressing the key that also holds the \ character.**

You can also have ELSE statements, which is where if the first OF statement is not executed because the conditions were not met, execute the next statement. They look like
if (test_var=2) { instance_destroy() } else { test2_var=7 }
This means that if test_var equals 2, destroy the instance, and if test_var does not equal 2, make test2_var equal 7.
There are also ELSE IF statements, which incorporates a second IF stement into the ELSE statement.
if (test_var=2) { instance_destroy(); } else if (test_var=10) { test2_var=7 }
You can have multiple ELSE IF statements one after the other.
Finally, a note on what happens in the parentheses. You can apply most of your common comparing mathematical operators into IF statements, including ('=', '<', '>', '<=', and '>=').
The '<=' and '>=' mean "less than or equal" and "more than or equal" respectively.
if (test_var<2) if (test_var>2) if (test_var<=2) if (test_var>=2)
It is possible to apply NOT stements to individual parts of the parenthese, like so:
if (test_var!=2) { instance_destroy() }
This means if test_var does not equal 2, destroy the instance.
This is useful when you have an IF AND statement and you want one part of the parentheses to be a NOT stement and the other to be a normal IF statement.
Keyboard Input
Keyboard input is an essential part of making a game if an object is to be moved by keys on the keyboard or variables to be affected by keyboard use.
There are two states that a key can be in: up or down. In this example, we will alter a variable using the spacebar.
if (keyboard_check(vk_space) { test_var=2 }
Here is an IF statement asking if the space bar was pressed during the current step (there are usually 30 steps in a second; this can be changed in the room settings menu). After this is the code to change the variable.

There are also two other basic keyboard commands: keyboard_check_pressed and keyboard_check_released. The former checks if that key was pressed since the last step, and the latter checks if the key was released since the last step. A less-used command is keyboard_check_direct, which is used to check the hardware of the computer itself, regardless of which window currently has focus (so this will work even if the user has selected a different application's window).

Here is a list of the more common keys to be checked. The parentheses go after the keyboard_check (or whatever you chose of that type) command.
keyboard_check(//put the letters here//) { //code// }
  • Letters:
    (ord("A"))
    Replace A with any letter you choose, just make sure it is capitalised, and keep the double quotation marks.
  • Numbers: Use the same as letters, except use '0', '1', etc. For numberpad keys, use
    (vk_numpad0)
    and replace the 0 with any number between 0 and 9.
  • Control keys:
    (vk_alt)
    Replace 'alt' with any of the control keys you wish, including the spacebar (vk_space) and backspace (vk_backspace).
Alarms
Alarms are essentially variables that are reduced by one for every step. This can be done manually, but it is easier to use the built-in function. For some reason, GameMaker only allows up to twelve alarms per object.

To set alarm 0 to one second, use this:
alarm[0]=30
Replace '0' with any number from 0 to 11 to set different alarms.
As I mentioned earlier, this all depends on the amount of steps per second in your room. The default is 30, and so 30 steps elapse in one second.
The other way to set an alarm to one second is like this:
alarm[0]=(room_speed)
This is a smarter way to program as you can have varying room speeds or change the room speed and not affect this code. For half a second or some other second-related time, you can put equasions into the parentheses like so:
alarm[0]=(room_speed/2)
This will set the alarm to half a second.

To cancel an alarm, set the value to -1:
alarm[0]=-1

To set an alarm relative to something (add the new value and the current alarm value together) use this:
alarm[0]+=20
You can replace 20 with another value or variable.

To change an alarm that is in another object, it is the same as for variables:
test_obj.alarm[0]=20
Changing the Location of Objects
In almost every game you will need to change the location of an object, whether it be incrementally or to a set coordinate.

To change the location of an object incrementally (as in 'move to the left ten pixels') all you need to do is alter the X or Y variables of that object. Each coordinate is a certain pixel.
To move left 10 pixels:
x-=10
Right:
x+=10
Up:
y-=10
Down:
y+=10
You can set the value to a variable:
x+=tester_var
Or even use an equation:
x+=(tester_var*2)
Remember, you can use these is IF statements and other parts of coding as well.

To set the location of an object to a specific coordinate, it's even easier.
If I want to make the object go to (10, -20):
x=10 y=-20
Note that (0, 0) in GameMaker is at the very top left corner of the room.

Now, if you want to change the coordinates of an object that is not the object you are working in, it is the same as with the alarms or variables; add the name of the object plus a period before stating the X or Y properties:
tester_obj.x=20
Just make sure you spell the names exactly right!
Closing
So there, you've learnt the very basic concepts of GML. Now go test them out!

Please rate and comment, I loooove feedback.

Thanks for reading, and I hope you enjoyed!

------------------------------- |-+~X~+-| -------------------------------

.
20 Comments
pax  [author] 20 Nov, 2020 @ 5:57pm 
lmao I'll change it, I was lowkey 14 when I wrote this haha. Might help you to know I've almost finished a software engineering degree so I have learned my lesson.
The Winter Bud 16 Nov, 2020 @ 3:44pm 
I know this is old but your movement code in the 2nd half is wrong. Adding to y makes the instance go down. subtracting from y makes the instance go up
Zappy 26 Nov, 2015 @ 12:05pm 
@~ [ Dr. Glickenstine ] ~
By out of bounds, I simply meant outside the room width/height itself.
pax  [author] 26 Nov, 2015 @ 12:02pm 
Ok. I haven't used GM for a while. Just loaded it up, it seems the Y scale is inverted to the usual for some reason, with the Y decreasing the higher you go in the room. So my intro code was actually right. I've changed it back to decreasing the Y makes you go up, and vice versa.
For the record, there is no out of bounds. The rooms are infinite.
Zappy 26 Nov, 2015 @ 11:35am 
@~ [ Dr. Glickenstine ] ~
But in the room editor, positive Y = downwards, so if you have an object at Y -52, it's already out of bounds above the room.
pax  [author] 26 Nov, 2015 @ 10:43am 
You probaby didn't learn from it as you're probably not a beginner :P

So about this Y thing. If I have an object at Y-52, and I use my code from the intro, the Y is going to be decreased by 4. -52 - 4 = -56. -56 is lower down than -52 on the coordinate chart. The object has gone down. If I were to then press up, it would add 4. -56 + 4 = -52. We're back at where we started.
Zappy 26 Nov, 2015 @ 1:27am 
@~ [ Dr. Glickenstine ] ~
Well I did mean like adding/subtracting to/from Y. I don't see why if the top of a room is Y 0, and downwards is a positive Y, how subtracting from Y makes something go downwards, but I haven't played around with Game Maker in a pretty long while.

All positive I have to say is many people are probably happy about this guide, but I personally did not learn anything from it. (Which I suppose might be a good thing?) Not that it's a bad guide, of course.
pax  [author] 26 Nov, 2015 @ 1:21am 
I have a feeling you're right. But doesn't have any effect on readers in regards to movement, and tbh I can't think of many situations when you would move objects around like that anyway. Best methods would always be either adding or subtracting from the current x/y, or moving to the coordinates of an object.

Is there anything positive you have to add? Thanks btw for pointing out the errors, it's just positive feedback is also part of constructive criticism.
Zappy 26 Nov, 2015 @ 1:15am 
@~ [ Dr. Glickenstine ] ~
Huh. Alright. If you say so, I guess. I haven't tested, but I was pretty sure I remembered it like that from memory. I may be wrong though.
pax  [author] 26 Nov, 2015 @ 1:13am 
Ok, right, the +/- were wrong in the intro. Also, regardless of the coordinate of the top of the room, adding to y will still make the object go up.