Home > SoulHow > SoulHow to deal with errors (Game Maker)

SoulHow to deal with errors (Game Maker)

June 12, 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: Intermediate user (5)

Here we have another SoulHow Game Maker assistance guide. This one’s about errors. Note: I’ve update the article a tad (6/14/08) due to incorrect use of certain terms, I apologize. It was about 3:00 in the morning when I first wrote this; you never know what can happen at such earliness =p

So you’ve finally learned GML, and you’ve been writing your very own code for a while now. But then one day, you’re innocently writing some code, and you run your game. Suddenly, the screen glows bright red, an alarm blares, and your computer screams at you that you’ve done something wrong. Another thirty seconds and you will have already been thrown in jail for breach of syntax.

Fine, ruin my fun. Ok, in reality, errors are a lot less dramatic, but they’re still a pain. In this article, I will explain what kinds of errors exist (yes, they come in varieties!) and how to deal with each kind.

Three types of horrible error-ness

Type 1 – Syntax errors

There are indeed three types of errors that you will undoubtedly encounter in your GM endeavors, the first of which being syntax errors. Syntax errors are those errors which arise when your code doesn’t make sense to Game Maker. Maybe you typed “varable” instead of “variable”, or maybe you forgot a parenthesis. Whatever you did, if you’re getting a syntax error, you did something wrong.

How do you know if you have a syntax error? Well Game Maker will tell you. It will either say “Expected symbol” or “unexpected symbol”. There are a few other types of rare syntax error messages, but they aren’t important enough to go into in detail. Basically anything you type which Game Maker can’t understand will generate a syntax error.

Type 2 – Run-time errors

There are really only a few types of run-time errors you’ll run into, the first of these being the divide by zero error. That occurs when you divide by zero. Shocker. Anyways, because you can’t divide by zero (I dare you to try it on paper), Game Maker gets brain freeze and can’t continue. So check your math to make sure that there can never be an arrangement of variables to cause a divide by zero.

A second type of run-time error is the horrid unknown variable error. You’ll get one of these if you try to use a variable before you initialize it. Imagine if I all of the sudden told you to add three apples to a box and tell me how many are in the box total now, when I haven’t ever actually shown you the box. So make sure you define your variables somewhere (usually in the create event) using the assignment operator, something like this: “a=5;”, before you use something like this: “a+=1;”.

Another thing that can be considered a run-time error, I guess, is an infinite loop. Infinite loops are those which never terminate. They just keep going and going. There is a SoulHow article about infinite loops, which I’d suggest you read for more complete info on this subject.

Mnementh has suggested another runtime error that I agree should be covered.  The “can’t compare arguments” error.  I feel this deserves its own paragraph because not only is it indeed a runtime error, but I have seen many people on the GMC posting question asking why they’re getting this error.  The can’t compare arguments error occurs when you attempt to compare a number with a string.  For example, tell me which message would be shown after executing the following code.

if (12>”Hello”) show_message(“Yes”);
else show_message(“No”);

So which is greater, 12 or “Hello”?


That’s right, the answer is, that there is no answer. Clearly you can’t compare a number with a word (string), and that’s just what GM tells you with its fancy “cannot compare arguments”. Even something as close as comparing 12 with “12” will return the same error because the string “12” is not really a number; just the text digits ‘1’ and ‘2’ shown next to each other. You might as well be comparing computers to fish. Thanks again to Mnementh for suggesting this addition.

Type 3 – Logic errors

Logic errors are the real killers. They don’t cause game maker to spit out an actual error message, but they cause your game to screw up in some way (so it does something you didn’t want it to do). The reason these are the beasts is because Game Maker can’t tell you where your error is; GM thinks it’s doing what you want, but you can see that it’s not. For example, maybe your character runs through walls, or he can jump on a spikeball to kill it (when he should really die from a single touch).

So how the heck are you ever supposed to get rid of these? Well, you have to test your game, thoroughly. Run it in debug mode and watch some variables. Make sure they’re what they’re supposed to be, and if they’re not, go to the parts of code where you set them or add to them, etc. Next, you can actually go through your code, keeping mental notes of what variables “would be equal to” if the code were actually running. In this way, you can “read” your code, find areas which run incorrectly, and then “sound out” methods that do work. Unfortunately that’s really the only way to get rid of non-error-message-showing runtime errors.

The syntax check button is your friend

This button, which can be found in both code and script windows, looks like 4 digits of binary code. When you press it, it automatically checks your code for syntax errors. When it finds one, it’ll highlight the line it’s on. When it doesn’t, it’ll do nothing. This will save you tons and tons of bugsquashing time, because you don’t have to consistently run the game only to hit a stupid error message which then kicks you out so you can fix it, and run the game again, only to encounter another error and have to do the same thing over. With the syntax button, all you have to do is keep pressing it and fixing a line or two until it stops highlighting. Much easier and quicker.

Good luck with your bugtesting.

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, go to this blog’s FAQ page.

  1. soulred12
    July 2, 2008 at 12:34 pm

    You are correct sir. Thanks for pointing that out.

  2. July 2, 2008 at 8:15 am

    Looks great, except for one line. The first one in Type 2 – Run-time errors.

    You put:
    “There’s really only one type of run-time error you’ll run into – the divide by zero error. ”

    Then you proceed to tell us about two other [more?] common types of Run-time errors. :)

  3. soulred12
    June 27, 2008 at 6:46 pm

    Excellent suggestion, thanks.

  4. Mnementh
    June 27, 2008 at 5:08 pm

    And what about this one?

    “string” == real

    That isn’t a syntax error, nor what you call “logic errors”. I suggest that you add a few things to the run-time errors section.

    Very nice, besides that.

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