Scene scripts can be written in TypeScript as well as JavaScript. TypeScript is JavaScript with types added, so the editor can check your code as you type and tell you exactly what's available on every object.
You can mix both in one project. Each script picks its own language.
Choosing a language
New scenes start with TypeScript. Existing scenes keep the language they already use.
- A scene's own script: select the scene root and, in the inspector's Scene section, set Script language to TypeScript or JavaScript.
- A shared script: the file extension decides.
.mmtsfiles are TypeScript and.mmjsfiles are JavaScript. See Shared Scripts. - Project scripts:
.tsfiles are TypeScript and.jsfiles are JavaScript. See Project Scripts.
Switching a scene script from JavaScript to TypeScript is instant, because JavaScript is already valid TypeScript. Switching back asks first, then removes the types for you. The code may be reformatted when that happens, and if the script has errors you'll be asked to fix them first.
What you get
In TypeScript scripts:
- Completions for everything in the engine:
SceneManager.,AudioManager.,Revealer., your scene's nodes onthis.dom.and its variables onthis.vars.. - Names from your project suggested inside strings. For example
AudioManager.PlaySFX("lists your sound effects, andSceneManager.SwitchScene("lists your scenes. - Hover info: hover any name to see its type and a short description.
- Errors as you type, underlined in red, such as passing a number where a scene name is expected or using a node that doesn't exist.
JavaScript scripts get the same completions and hover info, but only show syntax errors, never type errors.
Press Tab or Enter to accept the highlighted suggestion.
See Script Types for how this.dom, this.vars and your own script members are typed.
A short example
// Called when scene is ready
onStart() {
this.dom.title.textContent = `Welcome back, ${this.vars.playerName}`;
AudioManager.PlayMusic("theme", 2);
},
async onPlayClicked() {
await this.dom.title.setState("fadeOut");
SceneManager.SwitchScene("level_1");
},
Building
When you build or preview your game, TypeScript is turned into plain JavaScript, so players' browsers run it like any other script.
- Type errors don't stop a build. They're there to help you while writing.
- Syntax errors do. The build log shows the scene and line number, and that scene runs with an empty script until it's fixed.
Good to know
- Checking is relaxed: you don't have to add types to everything. Untyped function parameters are fine, for example.
- Scene scripts can't use
importorexport. Share code between scenes with Project Scripts instead.