Project Mammut Wiki
projectmammut.com
Project Mammut is currently in open beta. The UI is being updated a lot during this time, and you may run into bugs. If you'd like to help develop the engine or suggest features, come find us on Bluesky or Discord.

JavaScript in Mammut

Written 19 Nov 2026

A scene script is ordinary JavaScript, but it doesn't look like a normal JavaScript file. There's no let at the top, every function ends with a comma, and values are written as name: value. This page explains why, and what that means when you write one.

Everything here applies to TypeScript scripts too. For the parts of a script and when they run, see Scene Script. If you're new to JavaScript, start with Learning JavaScript.

A script is the inside of an object

In JavaScript, an object is a list of named members between { and }, separated by commas. When your game is built, Mammut pastes your scene script inside an object like that:

// What you write
retries: 0,

onStart() {
  this.retries++;
},
// Roughly what the engine builds from it
const scene = {
  // the engine's own members come first
  dom: null,
  vars: { health: 100 },
  render() { /* builds the scene's elements */ },

  // then your script, exactly as you wrote it
  retries: 0,

  onStart() {
    this.retries++;
  },
};

So the rules for writing a scene script are the rules for writing the inside of an object. The rest of this page follows from that.

Why there's a comma after every function

Commas separate the members of an object, and a method is a member like any other. onStart() { ... } is a short way of writing onStart: function () { ... }, so it needs a comma after its closing } just like retries: 0 does.

onStart() {
  const name = "Sam"; // statements inside a method end with a semicolon
  this.dom.title.textContent = name;
}, // members end with a comma

onExit() {
  log("bye");
},
  • A missing comma between two members is an error. In TypeScript the editor shows ',' expected. at the start of the next member.
  • A semicolon after a method (};) is the same error. Semicolons end statements inside a method. Commas go between members.
  • A comma after the last member is allowed, so you can end every member with one and never worry about which is last.

Why values are written name: value

let, const and var create variables while code runs. The top level of a scene script isn't code that runs. It's a list of members, so a value there is written as a member, with a colon:

retries: 0,         // a number
playerName: "",     // some text
collected: [],      // a list
lastHit: null,      // nothing yet

In TypeScript, writing let retries = 0; there shows ',' expected., and retries = 0, asks "Did you mean to use a ':'?".

Keeping values as members, rather than variables, is what makes scenes work:

  • Every copy gets its own. The engine builds a new object from your script each time a scene is shown, and one for every Scene node that uses it. Each copy starts with retries at 0 and its own empty collected list. A variable at the top of a file would exist only once, shared by every copy.
  • Other scenes can reach them. A member can be read from outside, as this.dom.card_1.scene.retries or SceneManager.GetScene("battle")?.retries. A variable couldn't be.
  • The engine can find your methods by name. It runs your hooks as this.onStart(), and an inspector field such as a Button's Event calls the method with that name on the scene. That only works because your methods are members of the scene.

See Variable Scope for how members compare with scene and project variables.

Use this. to reach a member

Inside a method, a member is this.retries, not retries. Without this., JavaScript looks for a variable called retries, which doesn't exist. TypeScript shows Cannot find name 'retries'., and in a JavaScript script the line fails with an error in the console when it runs.

Use let and const inside methods

Inside a method you're writing code that runs, so let and const work as normal. They only last until the method finishes, which makes them right for working values. Anything you need next time goes on a member.

lives: 3,

onHit() {
  const damage = 1;     // gone once onHit finishes
  this.lives -= damage; // kept on the scene for the next hit
  if (this.lives <= 0) SceneManager.SwitchScene("game_over");
},

Use const for values you won't change and let for ones you will. You don't need var.

Starting values can't use the scene

A member's starting value is worked out when the copy is made, before the scene's elements exist and before this is the scene. title: this.dom.title, stops the scene from being created. Start with null and fill it in from a hook:

title: null,

onEnter() {
  this.title = this.dom.title; // this.dom is ready from onEnter()
},

In TypeScript, give a member like this its full type with as, for example title: null as MammutElement | null,. See Script Types.

A starting value can use anything declared in a Project Script, because those load before any scene:

// MAX_LIVES is a const in a project script
lives: MAX_LIVES,

Why methods aren't arrow functions

In JavaScript, what this means depends on how a function is written:

  • A method written as onHit() { ... } gets the object it's called on as this, which is your scene.
  • An arrow function () => { ... } keeps the this of the place it was written. At the top level of a script that isn't the scene.

So members are always written as methods. onHit: () => { this.lives--; }, looks fine but fails when it runs.

Inside a method it's the other way round. An arrow function keeps the method's this, so it's the right choice for callbacks:

timer: 0,

onStart() {
  // the arrow keeps this, so this.tick() is the scene's tick()
  this.timer = setInterval(() => this.tick(), 1000);
},

onExit() {
  clearInterval(this.timer);
},

tick() {
  log("tick");
},

A function () { ... } callback inside a method loses this, and the editor doesn't warn you, so use an arrow there.

Why there's no import or export

import and export only work at the top of a file of their own. A scene script is pasted inside an object, so neither can appear in it. Put code you want to share in a Project Script instead. Everything declared there, such as functions, classes and constants, can be used in every scene script.

Names the engine uses

Your members share the object with the engine's own, so a member with the same name replaces the engine's, or is overwritten by it. Don't name your own members:

  • dom, vars, root, parentScene or attachedScenes
  • render, onMounted, onUnmounted or startAttachedScenes
  • anything starting with _

Summary

At the top level of the script Inside a method
Making a value retries: 0, const damage = 1;
Ends with A comma A semicolon
Lasts As long as that copy of the scene Until the method finishes
Read it with this.retries damage
Functions Methods, onHit() { }, Arrow functions, () => this.tick()