How To Fix Runtime Errors in GameMaker


How To Fix Runtime Errors in GameMaker

You run your game, and it crashes with a mysterious error – a common sight for game developers.

undefined

This is a “runtime error” – an error that happened while your game was running.

In this tutorial, let’s look at some common runtime errors and what they mean, before going through some ways of fixing such errors.

Contents

  1. Error Window
  2. Variable Not Set Before Reading
  3. Unable To Find Instance
  4. DoAdd Execution Error
  5. Wrong Argument Type
  6. Variable Index Out Of Range
  7. How To Fix Errors

Here is a video version of this tutorial:

You may also get compile errors while your game is being built, which are not covered in this tutorial.

Error Window

Let’s break down the error window into smaller parts:

undefined

1

The first part tells you where the error occurred. It mentions the event (“Step Event0”, which is the normal Step event) and the object (“Object1”).

2

The second part describes the error, which is “variable <name> not set before reading it”.

It then shows you where the error occurred – in this example, it was a line in the add_variable script function, on line 2.

That line of code is also shown: potatoes += 1;

3

The third part is the stack trace, which shows how GameMaker reached the line of code that caused the error. You read this from the bottom-up:

undefined

This shows that Object1’s Step event called the add_variable() function on line 1. Then inside that function, the error occurred on line 2, as it’s the final (top) line in the stack trace.

With that out of the way, the most important part is still the error itself. Let’s look at what the error shown above means.

Variable Not Set Before Reading

undefined

This error says you tried to use a variable that you didn’t set before. The variable here is Object1.potatoes, meaning the variable itself is called potatoes and it belongs to Object1.

In GameMaker, you create a variable by giving it a value. This error means you didn’t create the variable before you tried to use it, so it has no value.

First of all, check if it’s a typing mistake, which is the most common cause of this error. In my case, I mistyped the variable as potates in my Create event, meaning I never actually created the potatoes variable that I was trying to use.

undefined

If that wasn’t it, think about the “timeline” of your variable: it needs to be created first, which is usually done in the Create event, and after that it can be used and modified. Make sure your code follows this logic.

It may also be an issue with conditional blocks: if you had to meet a condition to create a variable, but not to use it, then you will face this issue when that condition is false.

Here is an example that shows this issue using a local variable, which is being created only when a condition is met:

if (instance_exists(obj_player))
{
    var _damage = obj_player.damage;
}

health -= _damage;

In this case, if the player didn’t exist, the local variable _damage would never be created, which is used later regardless of the condition.

Fix this by moving the health -= damage; line into the conditional block, or create the damage variable before the condition:

var _damage = 0; // Default value

if (instance_exists(obj_player))
{
    _damage = obj_player.damage;
}

health -= _damage;

Room Order

You can get the same error in the Create event, if you try to access an instance that hasn’t been created yet.

Every room has an “instance creation order”. This order is used to create instances that you placed through the Room Editor:

undefined

According to the order above, GameMaker will create the Object1 instance first and run its Create event.

Then, it will create the Object2 instance, and run its Create event.

So if you try to access a variable from Object2 in Object1’s Create event, you may get an error, if that variable is created in Object2’s Create event – which hasn’t run yet.

my_variable = Object2.my_variable;

With this code in Object1’s Create event, I get a “Variable not set before reading it" error. However, if I swap the Instance Creation Order so Object2 is created first, the error goes away.

Unable To Find Instance

undefined

In this code example, I’m trying to get a variable from Object2: Object2.my_variable.

The problem is that Object2 doesn’t exist in the room at all. In this case, GameMaker is “unable to find any instance” for that object.

This can happen if you never placed an object in a room, or you did, but it got destroyed.

my_variable = Object2.my_variable;

instance_create_layer(x, y, "Instances", Object2);

This example is super basic, but shows a clear problem: I tried to pull a variable from Object2 before I even created it.

To fix this, we need to swap the order of these lines. Then, for a bit of good practice, instead of using Object2 directly, use the instance ID that you get from instance_create_layer():

// Get the ID of the instance you just created
var _inst = instance_create_layer(x, y, "Instances", Object2);

// Use that ID to get the variable from the instance
my_variable = _inst.my_variable;

Why is this good practice? Because this way, you know exactly which instance you’re pulling the variable from.

When you just use Object2.my_variable, you leave it up to GameMaker to decide which instance it pulls from. If there are more than one instances of Object2 in the room, you won’t know which one you’re reading, and that can cause problems.

You'll get the same error if you destroy an instance, and then try to access it:

var _inst = instance_create_layer(x, y, "Instances", Object2);

instance_destroy(_inst);

my_variable = _inst.my_variable;

DoAdd Execution Error

undefined

This error means you’re trying to join different types of values, which can’t be joined together. Similar to DoAdd, you can also get DoMul, DoSub, etc. which are listed in the manual.

In my code, I’m adding a variable (my_variable) to a string ("Score: "), but the variable stores a number. You can’t add numbers and strings together, so you get this error.

The solution is to convert the number variable to a string, so in the end, you’re only adding strings together:

// Wrong: adding string and number
draw_text(4, 4, "Score: " + my_variable);

// Correct: adding string and string (converted)
draw_text(4, 4, "Score: " + string(my_variable));

Wrong Argument Type

undefined

Remember that functions take arguments? They’re also very picky about them.

An argument passed into a function needs to be of the correct type, otherwise it’s going to throw this error.

This error says the fourth argument for instance_create_layer() is wrong – I passed it a string, when it was expecting a number.

instance_create_layer(x, y, "Instances", "Object2");

If you look at the last argument, I’m trying to tell GameMaker which object to create an instance of. However I’m passing the name of that object as a string (“Object2”).

The function needs an object asset, which you can get by typing the object’s name as it is, without “quotes” around it. You can check this in the manual.

So when you remove the quotes around Object2, the error is gone:

instance_create_layer(x, y, "Instances", Object2);

Whenever you’re unsure about the argument types for a function, check the manual. Also try using Feather which gives you smarter info about functions.

Variable Index Out Of Range

undefined

You get this one with arrays, when you try to access an element that doesn’t exist in an array.

In the error above, it says the index I’m trying to access (3) is out of the array’s range (3). This means the array has three elements, which are 0, 1 and 2.

So there’s no element with the index ‘3’. If it existed, the array would have a range of four.

array = [0, 1, 2];

show_debug_message(array[3]);

This is the code that threw the error – you can see how the array only has elements 0 to 2, so trying to access 3 would throw an error.

If you’re using arrays in your game, make sure the index you’re trying to access is smaller than the array’s length.

It should also be 0 or above, as a negative index would give you the same error.

How To Fix Errors In GameMaker

A simple way to find the source of your error is to use show_debug_message().

This function lets you print a message to the output log, which you can see at the bottom of your GameMaker window:

undefined

This gives you an easy way to keep track of a value, so you can see if anything unexpected is happening.

You can use this function in a condition, so you know when something specific happens:

if (player_attacking)
{
    show_debug_message(“Player attacked!”);
    // Rest of the attack code
}

This can help you find out if a condition is being activated when it shouldn’t be, or vice-versa.

You can also use draw_text() if you want to constantly show a value in-game. Make sure you use it in a Draw event - we recommend the Draw GUI event, so the value always draws on-screen no matter where your camera is going.

Best Debugging Tool in GameMaker

The best way to debug your code is to use GameMaker’s built-in debugger.

To start the debugger, hit F6 on your keyboard, or press this button:

undefined

When you get an error, the debugger will show you the line of code where the error occurred. You can also pause (or “break”) the game manually at any time:

undefined

While your game is paused, you can run each line one-by-one by pressing the “Step into function call” button. This will also take you through all the lines of code inside your custom functions.

You can use the button on the right to execute functions while passing “over” them, so it doesn't open each custom function it encounters.

The third button is used to skip the whole script/event that is currently open, to take you “outside” of a function.

undefined

While your game is paused, you can hover over variables to see their values, as shown above.

More Debugger Features

There are various tabs at the bottom that show you various details about your game. The “Instances” tab is particularly useful, which lets you see all instances in the room and their variables.

undefined

Breakpoints

You can make your game pause at specific moments in your code, by placing breakpoints on lines of code.

You do this by pressing F9 with your cursor on a line of code, or clicking on this area on the left of a line:

undefined

When the debugger comes to this line, it will see your breakpoint and automatically pause the game.

For more information on the debugger, I recommend watching this video.

Happy GameMaking!