Home > SoulHow > SoulHow to debug your game (Game Maker)

SoulHow to debug your game (Game Maker)

June 29, 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: Experienced user (6)

Welcome to another SoulHow article.  This one is about debugging your game.  Ever get that problem during programming that’s just so elusive that it makes you want to take your keyboard, rip out every single key one by one, ram it into your monitor and finally burn it in fiery destruction?  But you never did it, because, then, well, you know, you couldn’t use it anymore.

Well there’s a way to fix this problem, or at least figure out how, aside from running to the GMC and posting an obscure thread with no details other than “Mai gaem duzn’t worrx!”  Okay, so you probably wouldn’t have done that (I hope) but you still might not need to post anything on the GMC.  Through wizardry, you might ask?  Absolutely positively not.  Okay, maybe.  But in this guide, it’ll be through debugging techniques which you can use with virtually every problem.  Read on, young game maker. My apologies for that if you’re actually an old game maker.

(Note: this article assumes you understand about code, that you know what scripts are, and that you understand while and for loops.  It’s not really much if you’ve been using GM for a while but I felt I should mention it anyways.)

So you’ve got a problem.  You’ve just entered in this code:

a=6;
b=3;
GoForward();
while (d<e) d+=1;
for (c=a; c!=5; c+=1) b+=1;
DoStuff();

When you run this code, your game suddenly freezes. (due to the fact that we don’t actually have any scripts written, don’t actually type this in Game Maker; just follow along) Without even knowing what code is inside GoForward() and DoStuff(), we can deduce exactly where our problem is. How do we do this? With debugging.

Now let’s see what we know. Freezing without an error message is generally the result of an infinite loop. But where is this infinite loop? And how do we find it? We use the comment-out method. This means we change various lines into comments (which causes GM to skip over them) and retest to see if this changes our result. So let’s begin.

The first two lines are simple declarations. It is impossible for those to cause an infinite loop, so skip those. The next line is a call to the “GoForward()” script. So imagine we comment out this line (by typing “//” at the beginning of the line, which makes it look like this: “//GoForward();“), and run the game again. Darn, still doesn’t work. Because getting rid of that line did not fix our problem, the cause does not lie inside GoForward(). We’ve ruled out the whole script without having to spend the time searching through it.

Thus, we uncomment that line, and comment out the next line (the while loop). Run it again, and the problem is still there. Next! Upon uncommenting that line and commenting the next (the for loop), we run the game again to see that our problem is gone! Amazing! Therefore, our problem definitely lies inside the line, “for (c=a; c!=5; c+=1) b+=1;” We’ve ruled out the DoStuff() script without changing it (e.g. commenting it out) at all.

So let’s examine our problem line.

for (c=a; c!=5; c+=1) b+=1;

Well let’s see. First we set c equal to a, and if we look back a few lines we find out that means we set it to 6. Next, we check if c is not equal to 5, and because it doesn’t equal 5, we increment b by one and then increment c by one as well and check the expression again. c now equals 7, and 7 does not equal 5, so we continue on…but wait! Something is wrong. c (7) is greater than 5, yet we’re adding to c. If you’re thinking correctly, then you’re thinking that c will never equal 5 and the loop will never end. Problem found.

You may be thinking one of two things, or maybe even both. One, that the problem was pretty obvious. Two, the example was pretty useless, except in demonstrating the debugging. Why set a to 6 and then use it in a for loop just afterward? Well, no reason, really, I just didn’t want to write a whole functional program to demonstrate a simple technique when a basic example was sufficient. Okay, moving on.

There is a second very effective way to get some serious debugging action done: the show_message() function. It can be used to display values at runtime, or to display messages that represent some point in your program. Let’s get back to our old example; it’ll go faster since we already know where the problem is.

a=6;
b=3;
GoForward();
while (d<e) d+=1;
for (c=a; c!=5; c+=1) b+=1;
DoStuff();

So instead of commenting out, we’re going to place some show_message()s to tell us where we get to in the code. Here’s how it might look:

a=6;
b=3;
show_message("Starting point");
GoForward();
show_message("After Go Forward script");
while (d<e) d+=1;
show_message("After while loop");
for (c=a; c!=5; c+=1) b+=1;
show_message("After for loop");
DoStuff();
show_message("After Do Stuff script");

As we run the game, we find that we get “Starting point”, “After Go Forward script”, and “After while loop” messages, but after that, nothing else.  The game freezes.

We’ve just found our problem point. Because the “After while loop” message shows but not the “After for loop” message, the problem definitely lies between those two points in the code. The only line between those show_message()’s is the for loop, so we search that line and eventually notice the cause of the freeze the same way we did in the first example.

These techniques can and should be used for every problem you encounter; you’ll have to adapt your strategies for the specifics, but by keeping the basic ideas in mind and practicing debugging instead of going straight to the GMC for everything, you’ll eventually get the hang of it, become quite self-sufficient, and save a heck of a lot of time both looking for bugs and waiting for GMC replies.

One last word.  I may have implied that you shouldn’t run to the GMC for anything and that you should instead sit in front of the computer for hours staring at the screen until the error magically jumps out at you; this is not true.  It is true that you should not use the GMC as your first resort, but it is well known in the world of programming that a fresh set of eyes can do wonders.  The reason is that you may have already accidentally convinced yourself that a faulty part of code works correctly (I’ve done this myself countless times…ugh).  Another person will not have made this mistake, and may be more able to identify what you did wrong.  Therefore I will say that the GMC is a great place to take your problem as a final resort.

Good luck, my friend.

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 the FAQ page of this blog.

Advertisements
  1. M@
    December 11, 2008 at 1:12 am

    show_debug_message(“Debug Message”);

  1. January 7, 2013 at 5:09 pm
Comments are closed.
%d bloggers like this: