Project scripts are your own .ts (TypeScript) or .js (JavaScript) files, kept anywhere in the project's assets. Use them for code you want to share between scenes, such as helper functions, classes and constants.
Create one from the File Manager with Create > Project Script (TypeScript), or add any .ts or .js file to the project.
How they work
Project scripts are loaded as plain scripts, before any scene. Everything declared at the top level of a project script can be used in every scene script:
// assets/scripts/utils.ts
function clamp(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value));
}
class Inventory {
items: string[] = [];
add(item: string) {
this.items.push(item);
}
}
// In any scene script (health is one of the scene's variables)
onStart() {
const inventory = new Inventory();
inventory.add("key");
this.vars.health = clamp(this.vars.health, 0, 100);
},
The script editor knows about your project scripts, so scene scripts get completions, hover info and type checking for them as you type, even before you save. Project scripts can also declare types for your scene scripts to use; see Script Types.
Things to know
- Load order: project scripts load in alphabetical order by path. If one script needs another while it loads, give it a name that sorts first, such as
01-utils.ts. Code inside functions can use any project script, whatever the order. - No import or export: project scripts share one global scope, like the engine's own scripts. A script that uses
importorexportis skipped in the build, and the editor warns you about it. - JavaScript files:
.jsfiles are typed from their code and any JSDoc comments. They don't show type errors. - Type declaration files:
.d.tsfiles are only used by the editor for typing, for example to describe a third party library you've added. They're never loaded by the game.