Home > SoulHow > SoulHow to code in GML (Game Maker): Part 2

SoulHow to code in GML (Game Maker): Part 2

May 31, 2008

NOTE: Comments are locked. I no longer answer questions about the Game Maker tutorials on this blog; I suggest you take any questions to the Game Maker Community. For more info, view the FAQ page.

Welcome back to the SoulHow guide to coding in GML, the Game Maker Language. The first part of this article was becoming too lengthy, so I decide to split it into two different blog entries.

In part 1, we left off just as we’d created our fourth variable inside a script, and were going to use our variables to draw something on the screen.

Because we’re drawing something, we need to utilize the draw event. Therefore, click add event-> draw event. In this, place an “execute code” action just like you did at the start of part 1. Because this code is in the draw event, anything we put here gets run when Game Maker is drawing on the screen and therefore that’s just what we can do (draw on the screen).

We want to use what’s called a function to draw some text. A function is something you use in Game Maker to do something. Pretty vague, but I’ll explain.  Functions can be thought of as GM’s nerves; they make the program’s muscles move to perform some task.  In this example, we’re going to be using a function called draw_text. Note again the underscore in the name of the function, because we can’t use spaces.

This function requires something called “arguments“. Arguments are values you give to a function to tell it the specifics of how to perform the task.  You can be told to speak, but does that mean you should yell, whisper, speak quickly, in French, etc.?  The function we’re using, draw_text, requires three arguments: the x (horizontal) coordinate, the y (vertical) coordinate, and the actual text to draw.

When you use a function in code, you just type its name, and then place some parenthesis. Inside the parenthesis, you’re going to put your arguments, in the correct order, separated by commas. Always check to make sure each opening parenthesis in any bit of code always has a closing parenthesis; otherwise GM will scream at you.

So inside the code box that you put in the draw event, type this code:


More tomfoolery. Let’s examine it little by little.

First we have the function name, draw_text. Then, an open parenthesis. Next, we have the first argument. 100. Yep, that’s a number. Game Maker scans the line of code just like we are, and when it encounters the first argument you typed, it realizes that it should use that number for the x coordinate of where to place the text. Next, it sees the comma, which signals the end of the first argument. After that it finds the 50, the second argument, and uses that for the text’s y coordinate.

The third argument, “Hello!” is a little more complicated. First you should know that there are different “types” of variables. Some are numbers like 100 or 12.5 or 2723, called real numbers (“integers” when they’re whole numbers), and some are letters put together, called strings. Strings can be used by typing something between double quotes (“). That’s why the third argument has them.

An interesting thing about variable types is this: strings can have numbers in them. Consider the difference between 10, and “10”. 10 is a number. “10”, however, is the combination of a “1” followed by a “0”. Imagine having ten apples, and writing the number ten on a piece of paper. Similarly, “Hello!” can be thought of as a series of letters and symbols (well, one symbol) put next to each other.

Like this: “H | e | l | l | o | ! “.

So back to draw_text, click the green check mark to close out and save the code. Make a room and put one instance of objMyObject in it (it doesn’t have a sprite so it should show up as a blue sphere with a red “?”), and then run the game. See the “Hello!” on the screen? Congratulations, you’ve done something in game maker by writing code!

Now let’s do something a little cooler. We want to actually use one of the variables. We will use it with the draw_text function, so instead of just “Hello!”, let’s write the value of one of our variables. We’ll use the one you made, ‘d’. So go back to the draw event of objMyObject, and double click the “execute code” action. We can leave the x and y coordinate arguments alone, but we’ve got to change the last argument.

First let’s do a little planning. I want the text to show up like this:

The value of the variable is: #

The # should be replaced by the number contained in ‘d’. First we can do the easy part. Change the code in the draw event to this:

draw_text(100,50,"The value of the variable is: ");

But we need to get the number inside the variable to show up. You’re in luck, though. Game Maker swaps in the number inside a variable when you use it by name. We have to tack it on to the end of that string so it goes into the draw_text function and shows up on the screen. However, once again, this is a real number variable, and the rules of GML say we can’t put a real number variable on to the end of a string. We have to “change” it to work with a string, so for example instead of representing seven of something, it’s actually a drawn 7. To do this, we use another function, called “string”.

Yep, just “string”. What this does, is it takes a real number (given to it as an argument within the parenthesis) and swaps itself out for a string version of that number. Only then can it be added to a string. Typing “string(8)” spits out “8”. Not 8, but “8”. Make sure you understand the difference between those before moving on. In case you don’t understand yet, let me try to explain it one more time.

This is what 8 ‘looks’ like to GM:

@ @ @ @ @ @ @ @

This is what “8” looks like to GM:


The first one is 8 of something, and the second one is the actual drawn number 8.

Okay, so how exactly do we put this converted number onto our string? We just add it to the end, exactly as you might think is too easy: using the + sign. It’s called concatenation, or the combining of two or more strings. The final code we’re going to use is this:

draw_text(100,50,"The value of the variable is: " + string(d));

Again, because typing the name of the variable ‘d’ tells Game Maker to swap it out for what’s actually inside ‘d’, we can use it inside the “string” function. Also, notice how because there’s two open parenthesis, we need to make sure there are also two close parenthesis in the code to match.

We’re going to make this example even cooler. To show you the dynamic nature of variables, we’re going to make a button that adds to the variable every time it’s pressed, and watch as the drawn text changes before our eyes.

Add another event in objMyObject, this time a key press->spacebar event. Inside here, we’re going to learn how to change variables without having to tell Game Maker what the exact new value should be. Instead, we’re going to tell GM to just add 1 to the number. So in the key press spacebar event, right click the “execute code” action, and inside, type:


Now we can’t really read this out loud to get what it means. The “+=” basically means, “this variable (‘d’) gets 1 added to it.” Change the 1 to a 3, and ‘d’ gets three added to it. Keep it at 1 for now, and click the green check mark to save the code and close out of the window.

Now run the game again, and see how the text is different. It now says, the value of the variable is: and whatever you set your variable ‘d’ to in that script. Close out of the game, go back into the script (by double clicking), and change the number you put there. Run the game again, and see how the text has changed, just by changing the number you put inside ‘d’. This time, though, let’s try out our spacebar code. Press the spacebar and see the number dynamically increase by 1 every time. Every time you press the spacebar, the number goes up, no matter how many times you do it.

And there you go. You have just successfully coded something. As practice, try changing the name of ‘d’ and making it work in the game without an error. (Hint – remember to also change the name of ‘d’ in the other places you’ve used it: the key press spacebar event and the draw event)

There are tons of other functions for you to learn about, and there are even variables GM makes for you. You use them in the same way you do any other function or variable; you just have to know what they do. That’s where the GM manual comes in. Read up on some new functions every week and try practicing using them. Also start making games with as much code as you can for experience, and when you hit a road block, search the manual for how to get past it. With some practice (okay, a lot of practice), coding with GML will become like a second language.

All comments/criticism is welcome, so feel free to post a comment.

Good luck with all your magical GML endeavors.

Wait!  Want to learn how to actually do something useful in GML?  Check out the new article, called “SoulHow to actually do something with GML”.  It can be found here.

If you enjoyed this article, please consider checking out the rest of the blog.

If you have a technical question, such as “How do I do this” or “this is not working right” (relative to something with your Game Maker game after reading this tutorial), please head over to the Game Maker Community and ask there.  I can’t answer any such questions in this blog and the members at the GMC will be more than capable to help you.  For more info, please view this blog’s FAQ page.

Categories: SoulHow Tags: , , , ,
  1. Marcus
    October 31, 2009 at 5:39 pm

    Thanks it has got my head arround the subject. It gets quiet hard when I’m only 12 so thanks =)

  2. Aaron
    June 9, 2009 at 5:36 am

    Thanks for the great tutorial! Refreshes my dead memory.

  3. Ian Simpson
    February 7, 2009 at 4:25 pm

    This was an excellent introduction to GML. I’m moving over from VB and your explanations made perfect sense. The debugger and in-line help in GameMaker looks useful too!
    All the best,

  4. Grace
    August 4, 2008 at 4:37 pm

    I found this very helpful. Thank you!

  5. D.J.
    June 6, 2008 at 12:27 am

    Tyvm This has helped me so much. I have strugled for 3+weeks trying to figure this out but now that i have read this . You have put a end to my strugels

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: