Home > SoulHow > SoulHow to code with GML (Game Maker): Part 1

SoulHow to code with GML (Game Maker): Part 1

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.

Skill Level: Rookie (3)

Well, well, here’s yet another SoulHow guide. I think I’m on a roll. Anyways, today I want to talk about learning to code in Game Maker. That’s right, after reading this article, you’ll no longer be limited to those clunky actions. Scripts will be an everyday occurance! Seems foreign and scary, but let’s jump right into that freezing water. If you just downloaded the program and want to learn how to use it at all, check out my article on Game Maker’s basics. If you don’t have the program at all yet, get it here.

The first thing I’d like to say is that coding in general requires that you think abstractly. You have to visualize things; you can’t just try and think about them. For example, try to think about “one”.  Not the word “one”, not the number 1, not 1 apple, not 1 elephant, just “one.”  The concept of “one.”  Can you do it?  This is the type of thinking you need to do in algebra.  If you have taken algebra or if you can complete the exercise above (and if you’ve been using Game Maker for at least a little while), you’re ready to learn GML.

As with algebra, the first thing you need to understand is variables. Variables are just little containers which hold numbers and words.

These “variables” have names. Just like “X” in the generic problem X+3=4, every variable must have a name. When you create one (you have the power!), you give it a name straight off, and you use this name when you use the variable. Variables can be used to hold things like the number of bullets a character has left; after all, the a character’s bullet count varies, thus the name “variable”. Yep, it’s because they change all the time (shocker).

Creating a variable is much like making a box. When you tell Game Maker to create a variable, you’re basically telling it to make a new box to hold stuff in. This box might end up holding the number 7, for instance. Later, you might get tired of that pointy number and change it to the number 22. Even later you might add 3 to it, making it 25.

Now that we (hopefully) understand variables, let’s make some. Open up Game Maker, and click “new game”. Don’t bother with a sprite for now, just make a new object. Name this object, “objMyObject”. Original, I know.

Inside this object we want to add a create event. So click add event-> create event. Instead of using the actions with the fancy arrows or circles on them, we’re going to scooch over to the “control” tab and right click the dull little piece of paper , called “execute a piece of code”. Not the one with a green arrow, that’s something different which I’ll get to later. A new window pops up, but this time it looks like a notepad box. Yes, this is what you’ll use to write all that dreaded code.

Now let’s make our first variable.  What better to name it than, well, “a”.  In that box that popped up, type the following:


Yep, that’s all it takes. Anyways, this is a fully functional piece of code. What does it all do? Well, let’s read it out loud. “A equals 5.” That semicolon just means you’re done with a statement (a “statement” is like, a GML sentence).  Let’s look at another example.


Now we have two variables, “a”, and “b”. They are totally separate; giving a value to one has no effect on the other. Okay, let’s add one more line to our code:


What is this wizardry???

It’s called addition. Yes, the same one as in third grade math. Read that new line aloud, just as you did with the first line: “C equals A plus B”. Judging from this, what number do you think is now inside of c? If you guessed 153,323,112, you’re wrong. It’s actually 8. Here’s why:

The computer takes a look at the first two lines. (Remember, code is always read from left to right, top to bottom, just like English) It sees the first line, and says, “Okay, this guy wants me to make a variable with the name ‘a’ and stick the number 5 inside it. Hmm…on the next line, he wants me to make another variable and name it ‘b’. Looks like a 3 goes into this one. What’s this? A third variable, ‘c’. This one has to equal the variable ‘a’ plus the variable ‘b’. Well, the variable ‘a’ has the value 5 inside it, and the variable ‘b’ has the value 3 inside it. So because 5 plus 3 equals 8, I should put an 8 inside ‘c’.”

That’s really all there is to it. Game Maker reads your code just like a book, and does everything just as you type it. Sometimes it won’t work the way you want it to work, but no offense, it’d be your fault, not the computers. This should give actually you some hope though; it means there’s always a way to make your code work if you look closely enough for errors.

Back to your code, see that “1010” button near the top of the code window? If you hover your mouse over it, it says “check for syntax errors”. This button is your dearest friend. It will save you tons of time running the game and getting flogged and kicked out of your game for the tiniest errors. When you click it, it highlights any areas of code the computer doesn’t understand. If nothing is highlighted, your code (usually) is free of errors.

That brings me to my next point: syntax. Syntax is the way you have to type code. Just like you have to adhere to grammar when writing a report, you have to follow syntax when you code; but where a few grammar mistakes in a report may simply mean the difference between an A and A-, in Game Maker, a single syntax error will mean you get an automatic F.  A single syntax error will literally halt your entire game.  It makes sense though; if you tell Game Maker something it doesn’t understand, how is it supposed to do any more than if I were to give you instructions in Japamongolionesquese?

Don’t worry though; overall, it’s really not that difficult.  Certainly not as difficult as it is to learn Japamongolionesquese, mostly because that language doesn’t exist.  Go figure.

Anyways, close this code by clicking the little green check mark on the top left. Your code is saved. But we want to learn everything about GML, so we’re going to work on learning what a script is. Take the plunge and click “new script” up at the top near “new object,” “new sprite,” etc. A new window opens, much like the last one. In fact, almost exactly like the last one. Make a variable of your own, call it ‘d’, and make it equal to whatever number you want. See if you can get it so that nothing happens when you click the check syntax button.

Now click the green check mark on the script window. Go back to your object, and then back to the control tab. This time, though, right click the action that shows a piece of paper with a green arrow next to it . This is called, “execute script”. You’re telling Game Maker to read the code contained in a particular script as if you had typed it in a regular code window there instead.

Why would you do this if you have to type the code in another window anyways? Well, first of all, we’re only doing a demonstration. Second of all, sometimes you want to execute the same exact code more than once. If you write it in a script, you only have to use that action to call the script whenever you need to run the code rather than having to write out or copy+paste all the code every single time you need it. And lastly, what if you want to change that code? Can you imagine the horrors of having to make changes to the same code hundreds of times just because you were lazy and didn’t use a script like you remember I told you to? If you had made a script, on the other hand, you’d only have to change the script in one place.

Okay, so now that I’m done berating you for something you didn’t actually do, right click that action (if you haven’t done so already) and in the box that pops up, click the drop down menu and select your newly created script. Probably “script0” (that’s script-zero) if you haven’t renamed it. You can if you want, it doesn’t matter at this point. Just remember the naming rules: no spaces, don’t start it with a number, etc.

Your object will now have four variables. ‘a’, equaling 5, ‘b’, equaling 3, ‘c’, equaling a+b which is 8, and ‘d’, equaling whatever you made it equal to. Let’s actually do something with these variables, shall we? We’re going to draw them on the screen. Check Part 2 of this article for the rest of the guide (don’t want to make any individual article too long).

Categories: SoulHow Tags: , , , ,
  1. jochemboy5
    July 6, 2009 at 3:21 pm

    omg, nice first i think its unposseble to understand that code for me but now i know it :D
    very nice guide, if i ca rate 5/5

  2. reven17
    September 25, 2008 at 2:12 am

    hey this is just awsome i have never know about that 1010 button! you helped me so f***ing much thx thx!

  3. ell
    September 18, 2008 at 8:28 am

    I no-loger fear code thanks so much

  1. March 28, 2014 at 9:32 am
Comments are closed.
%d bloggers like this: