Debugging
While you make a game, the engine helps you find mistakes in two ways: error dialogs that open over the game and say what went wrong and where, and messages on the browser’s console. The debug module turns the dialogs on, and the debug level decides how much the console prints. When Something Goes Wrong shows how to open the console in each browser; Diagnosing Errors lists every error dialog.
The debug module
The starter game’s index.html loads engine/debug/debug.js before the engine:
<!-- Debug Library. Remove this file for release builds -->
<script src="./engine/debug/debug.js"></script>With it, the engine shows its errors as dialogs. The module:
- holds the text of every error dialog;
- opens a dialog titled
There is a mistake inand the file, with the line, when one of your files has an error the browser can’t run past while the page loads (a missing comma injs/script.js, say), and one titledAn Unknown Error Occurredfor an error your code throws later; - highlights the code shown in the dialogs;
- makes the console list each component as the engine registers and mounts it (the lines that start with
[Pandora]); - turns on the engine’s step-by-step trace in the console, at levels 4 and 5.
A dialog shows when the module is loaded and the debug level is above 0. With the module loaded, the level starts at 1; the starter game’s js/main.js sets it to 5 on its first lines:
monogatari.debug.level(5);Before you release the game, remove the <script> line of debug.js from index.html. Players then never see an error dialog, and the console only gets errors and warnings. Most of the errors the dialogs report aren’t printed on the console either, so keep the module while you test.
debug
The engine’s console. It has the methods of the browser’s console, but each one prints only when the debug level is high enough.
debug: Debug;| Level | Name | What the console shows |
|---|---|---|
0 |
NONE |
Nothing from debug. The default, when no code sets a level and the debug module isn’t loaded. |
1 |
ERROR |
debug.error(). The default with the debug module loaded. |
2 |
WARNING |
Also debug.warn() and debug.warning(). |
3 |
INFO |
Also debug.info(). |
4 |
DEBUG |
Also debug.log(), debug.debug(), debug.table(), debug.trace(), debug.time() and the rest. |
5 |
ALL |
The same as 4. |
The names are in Monogatari.DebugLevel: Monogatari.DebugLevel.WARNING is 2.
| Method | What it does |
|---|---|
debug.level(level) |
Sets the level and returns it. Without an argument, returns the level. |
debug.setLevel(level) |
Sets the level. |
debug.isEnabled(level) |
true if the current level is at least level. |
debug.log(…), debug.debug(…), debug.info(…), debug.warn(…), debug.error(…) |
Print, at their level. |
debug.group(…), debug.groupCollapsed(…), debug.groupEnd() |
Group the lines printed between them. |
debug.table(data), debug.dir(data), debug.trace(), debug.assert(condition, …), debug.time(label), debug.timeEnd(label), debug.count(label) |
As the console’s methods of the same name. |
Errors and warnings print with or without the debug module: only the level decides. The rest (log(), debug(), info(), the groups, tables, timers and traces) only print with the debug module loaded, so a released game’s console stays quiet whatever its level. At level 4 or 5, with the module, the engine itself prints a lot: a collapsed group for each statement it runs (Run Cycle [Dialog]), the checks before each move (shouldProceed Check, with the action that refused), and the keyboard shortcuts it binds. Lower the level to see only your own messages and the warnings:
monogatari.debug.level(2);
monogatari.debug.warn('Chapter 2 has no music yet');monogatari.debug can’t be replaced: assigning to it throws Debug reference cannot be overriden.
FancyError
The class behind the error dialogs, Monogatari.FancyError. Your own code can open a dialog with it, which shows under the same conditions as the engine’s: the debug module loaded and a debug level above 0.
FancyError.show()
FancyError.show(title: string, message?: string, details?: object): void
FancyError.show(id: string, context?: object): voidWith a title, a message and an object of details, it opens a dialog with them. Each key of details becomes a line of the dialog’s Details; a value that’s an object becomes a group of lines.
const { FancyError } = Monogatari;
FancyError.show('The chapter has no ending', 'Chapter 2 doesn\'t end with a jump or an end statement.', {
'Label': 'Chapter2',
'Help': {
'_': 'Add an end statement as its last line.',
},
});With the identifier of a registered error, it opens that error’s dialog and fills the {{…}} in its text with the values of context.
While one dialog is open, the next ones wait their turn, and closing a dialog with its Ok button shows the next.
A value in details or context shows as text, except for a few kinds: an element of the page shows as its markup, an array as its items separated by commas, a function or a class by its name, and undefined or null as (none). A {{…}} whose key isn’t in context stays as written.
FancyError.register()
Registers an error under an identifier, for show() to open.
FancyError.register(id: string, template: { title: string; message: string; props: object }): voidFancyError.register('game:chapter:no_ending', {
title: 'Chapter "{{label}}" has no ending',
message: 'The label doesn\'t end with a jump or an end statement.',
props: {
'Label': '{{label}}',
},
});
FancyError.show('game:chapter:no_ending', { label: 'Chapter2' });The engine’s identifiers follow the pattern type:part:error, such as action:jump:label_not_found. Register yours with a prefix of your own, so they don’t clash with the engine’s.