Learn the Game Maker Language (GML) through tutorials and examples

Game Maker Language Tutorials and Examples

Introduction


So, you're interested in learning about programming in GML, eh? Well, lucky for you GML is very easy to learn. Before we get started, you should realize that programming takes time and practice. Good luck.


Syntax

Okay, first things first, syntax. Syntax in programming is like grammer in writting; if you don't know the grammer, you cannot write correctly. Likewise, if you do not use syntax correctly, you cannot program. If you are completely new to programming, syntax will seem quite strange to you at first. Let me assure you, however, that after time, that same syntax that seemed so strange will become second-nature to you. Trust me, it happened with me when I first started learning GML.

Coding Order

Now then, the first thing to you should know is that GML is processed from the top of your code first and then it works its way down. So, if your code is this:

draw_sprite(spr,0,x,y);
draw_rectangle(0,0,5,5,1);


The draw_sprite() code will be executed first, then the draw_rectangle() code will be drawn afterwards. This becomes crucial later on because, taking the code above, if you want the sprite to be drawn above the rectangle, then the code to draw the sprite, the draw_sprite() stuff, should be last. The current code has the sprite drawn first, so if the object's location that is drawing the sprite is located within the drawing area of the rectangle, then the rectangle will cover some or even all of the sprite. Just remember, the order that your code is in is very important and can result in several bugs if you are not careful. As a rule of thumb, the code in the drawing event is the most important to have the correct order for.

Arguments

Okay, now that we know that GML is order-based, we should move on to arguments, and no not the type of arguments that politicians use. Arguments are essentially the stuff within the parentheses. Take this code:

draw_sprite(spr,0,x,y);

Okay, so in order to break down this code, we must first take a look at the part before the parentheses. This is called the function. In this case, the name of the function is the "draw_sprite" part. Please realize that whenever we talk about functions, that the name of the function is always followed by parentheses. So, in this case, we are using the draw_sprite() function. Now, if you look up the draw_sprite() function in that handy-dandy Game Maker help file, you will notice that it says "draw_sprite(sprite,subimage,x,y)." Okay, so we now know that sprite, subimage, x, and y are our arguments. What this means is that these four arguments will directly affect what the funcion will do. Let's take another look at that draw_sprite() code:

draw_sprite(spr,0,x,y);

Okay, so we know now that we're calling the function draw_sprite() and are assigning the arguments for the sprite to be set equal to spr, the arguments for subimage to 0, the argument of x to x, and the argument of y to y. In English, this function will draw the sprite spr with subimage 0 at the x-coordinate of the x value of the object and at the y-coordinate of the y value of the object doing the drawing.

Take a breather, it really isn't that hard to understand. I'll go through another example for you. Just know this, once you conquer the sytnax, then you really don't need to know much more after that because, quite frankly, you can simply use the Game Maker help file to look up any and all functions that you need for your games. That means no memorizing functions.

Okay, now let's look at another example, shall we?

string_copy("Hello, I am new at GML",1,5);

Now, looking up the string_copy() function in the Game Maker help file gives us this: "string_copy(str,index,count) Returns a substring of str, starting at position index, and of length count." So, we know that this funciton will copy a part of the string str starting a location of index for a length of count characters. Phew. Sounds nasty, huh? Trust me, it really isn't at all. Looking at the code above, notice that for the argument str, we assigned it as "Hello, I am new at GML." Okay, so we know that that will be our string that we will be copying from. For the argument index, we assigned it to be equal to 1. 1? Why 1? Good questions. Well, in order to start at the beginning of the string, we must set the index equal to 1. This is how it would look:

Character of StringHello, I am new at GML
Character Index12345678910111213141516171819202122


So, referring to the above table, we see that we are starting at the "H" character. Next, we see that we have set the argument of length equal to 5. This obviously means that we will be copying 5 characters from our string. So this is how it would work. string_copy() would take our string and start it at the beginning (since we set the index equal to 1) and would copy five characters from there. So, the string_copy() function would return "Hello". Here's a visual aid of it:

Character of StringHello, I am new at GML
Character Index12345678910111213141516171819202122


Pretty easy, right? Well, let's say that instead, we used this:

string_copy("Hello, I am new at GML",8,15);

Same function and everything, but now we changed up the index argument and the length argument. Now the function starts at the "I" and lasts for 15 characters. Realize that that 15 characters includes your index argument. So, really, you're not doing 8+15=23 but rather 8+15-1=22. This means that the string will copy from the 8th character until the 22nd character. So, if you use the above chart to figure it out, you'll notice that this function will return "I am new at GML".

Return

Now, you'll notice that I kept bolding return. Now, why would I do this...? Well, you must realize that almost all functions return something. In our example above that was "I am new at GML". Now let's suppose that we wanted to define a variable to equal that "I am new at GML". First off, variables are essentially names that define some piece of information. They really can be anything. I can have the variable "today" equal to today's date. Let's take a quick look at this before continuing. Let's define some variables by simply using the equal sign ("=").

a = 1;
b = 2;
c = a + b;


Huh? Well, you see, we are setting the variable "a" to equal 1, the variable "b" to equal 2, and the variable "c" equal to the variable "a" plus the variable "b". This kinda reminds me of good old substition in Alegbra. Since a = 1 and b = 2, and c = a + b, that really means that c = 1 + 2. See? "a" is really 1 and "b" is really 2. Therefore, c = 1 + 2 and so c = 3. Makes sense, right? Here's another example:

start = 5;
end = 10;
startend = start*end;


So, we defined "start" to be equal to 5, "end" to be equal to 10, and "startend" to be equal to "start" multiplied by "end". By doing some good ole' substitution, we figure out that "startend" really equals 5*10. So, "startend" equals 50.

Now, let's take a look at our function, string_copy(). The Game Maker help file says that it "returns a substring of str, starting at position index, and of length count." It's Game Maker help file gibberish again. Yet, we now know that if we say:

string_copy("Hello, I am new at GML",8,15);

Then we will get "I am new at GML". Yet, if we don't assign this to anything, then it will vanish from your computer's virtual memory. Scary, right? Well, let's keep that little string safe and secure by assinging it to a variable.

string3 = string_copy("Hello, I am new at GML",8,15);

Okay, so what we have now done is we have used a variable to snag what our string_copy() function returns. A.K.A. string3 will equal "I am new at GML". So, if you wanted to pop up a message box saying "I am new at GML", you could do this:

string3 = string_copy("Hello, I am new at GML",8,15);
show_message(string3);


Okay, so we now know by looking at the Game Maker help file (are you sensing a trend yet?) that the show_message(str) function "displays a dialog box with the string as a message." So, we set the argument of str equal to our variable, string3. Since string3 equals "I am new at GML", the message box will pop up saying "I am new at GML". Easy, right? Don't worry, we still have a ways to go yet. Go on to the next page where we'll continue to talk about syntax, but first take a break. Relax. I know I will after typing so much, so why don't you. Heck, courts have recess periods, so why shouldn't we?

Previous...Next




Contact Dan Hubley: djh5331@psu.edu. Feel free to contact me for suggestions on tutorials and examples.


Beginner Tutorial Parts:

GML Intro
GML Part 2
GML Part 3
GML Part 4
GML Part 5
GML Arrays
GM Resources
GM INI Files
GM Text Files
GM User Input
GML Closing

First Game Tutorial

Planning
Sprites
Sounds
Movement
Balloons
Power Ups
Closing GML
Finishing

Examples

Missile Example
Missile Example
Race AI
Race AI
Mario Party
Missile Example2
Missile Example2
Point of View
Point of View
Mouse Follow
Password Creator
Circle Targeting