Using the Console

Return to Index

If your script doesn't work as intended or causes your game to crash, you can use the console to help figure out what the issue is. This will tell you how to do so.

## Displaying the Console

To display the console, you must be in Map Test or Test Play. Look at the menu bar, click "Debug" and check off "Show Console". You should see another black box appear next to your game screen.

Once you do this, you will be able to see output to the console.

## Outputting to the Console

To output text to the console, use the root.log() command. Put the data you want to be displayed inside the parentheses. For example, if I wanted to output "Hello" to the console, I would type root.log("Hello"); in my script. You can also use this to output numbers or a combination of strings and numbers.

```
root.log("Item name: " + item.getName());
root.log("Item weight: " + item.getWeight());

```

## Debugging using the Console

Let's say my script crashes the game when I move a unit to a new position, and I can't figure out why.

Ever since creating my script, I've been getting this error. Since it never happened before, it must be problem with my script.

Let's say I've created a two functions. One is called createMagicIndexArray and it takes in four parameters: an x position, a y position, an item, and a unit. The last two parameters, the item and the unit, are objects and thus can be null. My other function replaces the default BaseItemAvailability.\_checkMulti function, but all that is important to know here is that it calls my createMagicIndexArray function. First, I will use the console to check which of these two are null with the following lines of code, placed at the top of my createMagicIndexArray function.

```
if(item == null) {
	root.log("Item is null");
}
if(unit == null) {
	root.log("Unit is null");
}

```

When I test play my game, move a unit, and check the console, this is what the console displays when the game crashes:

Aha! I now know that it was the unit parameter causing the problem and not the item parameter. But why is this? Unit couldn't have possibly been changed at this point, and obviously, a null unit can't exist in the game, so what's the problem? Well, let's look at the calling function.

In the calling function, the following line calls createMagicIndexArray:

```
var indexArray = IndexArray.createMagicIndexArray(unit.getMapX(), unit.getMapY(), item);

```

What is the problem with this line of code? This line of code works, and the line itself calls functions that require that unit is not null, so why does unit become null in my new function? In fact, if I put the console output code inside this function, it would output nothing to the console. Therefore, the problem must happen when transitioning from this function to the new function.

Looking at this function call, I can see that createMagicIndexArray takes in four parameters, but I have only passed in three, missing the "unit" parameter. That's why "Unit" is null in my new function. Adding the unit parameter to the end of the call fixes the problem and outputs nothing to the console when I move a unit. All fixed and finished!

```
var indexArray = IndexArray.createMagicIndexArray(unit.getMapX(), unit.getMapY(), item, unit);

```

## Other Tips

You can also use the console to output variables to see their values if you are getting unwanted output or undesirable values. Just put the variable you want to output inside the root.log() call similar to the following line.

```
root.log(item.getWeight());

```