The logging functions write messages to the game log, which you can read in the Debug View and the browser's console. Logger lets your own code see every message as it's written.
| Member | Description |
|---|---|
log() |
Writes a message |
logWarning() |
Writes a warning |
logError() |
Writes an error |
Logger.Register() |
Calls a handler for every message |
Logger.Unregister() |
Removes a handler |
log
log(...args: any[]): void
Writes a message to the game log. Pass as many values as you like. Objects and arrays can be expanded in the browser console.
How it works
- Each message starts with the file and line it came from.
- The engine logs things too, such as every
Store.Setand every sound played.
Example
onStart() {
log("Level started with", Store.Get("lives", 3), "lives");
},
logWarning
logWarning(...args: any[]): void
Writes a warning, for something unexpected that the game can carry on from.
Example
showPortrait(key: string) {
const image = Assets.GetImage(key);
if (!image) return logWarning(`No portrait for ${key}, leaving it blank`);
this.dom.portrait.src = image.url;
},
logError
logError(...args: any[]): void
Writes an error. Errors are counted on the Debug View's button, so you notice them while testing.
Example
async loadLevel(key: string) {
try {
return await Assets.LoadFile(key, "level");
} catch (error) {
logError("Couldn't load level", key, error);
}
},
Logger.Register
Logger.Register(fn: (level: "log" | "warn" | "error", args: any[]) => void, owner?: object): void
Calls fn for every message written with log, logWarning or logError, including the engine's own. level says which, and args holds the values that were passed.
Pass this as the owner to remove it when the scene exits (see The owner argument).
How it works
Calling log from inside fn calls fn again, forever. Write to the page or the browser console instead.
Example
onStart() {
Logger.Register(this.showError, this);
},
showError(level: LogLevel, args: any[]) {
if (level !== "error") return;
this.dom.error_banner.textContent = args.join(" ");
this.dom.error_banner.show();
},
Logger.Unregister
Logger.Unregister(fn: (level: LogLevel, args: any[]) => void, owner?: object): void
Removes a handler added with Logger.Register. Pass the same function, not a bound copy. Logger.UnregisterScene(owner) removes every handler that owner registered.