Scope determines where you can access a variable inside the editor’s scripts. In JavaScript—and therefore in the engine’s scripting system—variables can have different visibility levels depending on how they are declared.
| Keyword | Scope | Behavior |
|---|---|---|
let |
Local to the script | Cannot be used from other scripts. |
const |
Local to the script | Like let, but cannot be reassigned. |
var |
Global | Can be accessed from any other script in the engine. |
// Script A
let score = 0;
function Update(obj, dt) {
score += dt;
console.log(score);
}
In this case, score only exists inside this script.
If another script tries to access it, it will throw an error.
// Script A
var Score = 0;
// Script B
function Start(obj) {
console.log("Current score:", Score);
Score += 10;
}
Because var is used, the variable Score is available
in any script loaded in the scene.
// Player script
function Start(obj) {
obj.Score = 0;
}
function Update(obj, dt) {
obj.Score += 1;
}
From another script you can read that value easily:
let player = getObjectByName("Player");
console.log(player.Score);
This method is ideal for data belonging to each object (lives, energy, inventory...).
window.MyVariable (Persistent Global Variable)
In JavaScript, anything stored in the window object becomes a true
global variable.
This means it is accessible from any script and it does not reset when
you change scenes inside the engine.
This is extremely useful for data that must survive between levels, such as:
// Declare only once
if (!window.playerCoins) window.playerCoins = 0;
// Use anywhere:
window.playerCoins += 10;
console.log(window.playerCoins);
Because the value is stored in window, it is never lost unless:
window variables behave like permanent storage for the whole game session.
They remain intact when changing scenes or loading/unloading objects.
window with buttonsfunction Start(obj) {
if (!window.totalClicks) window.totalClicks = 0;
_onClickUp(obj, () => {
window.totalClicks++;
console.log("Click count:", window.totalClicks);
});
}
This counter will increase across all scenes and will remain valid until the player closes or reloads the game.
// Main script
if (!window.GameData) window.GameData = {};
GameData.Score = 0;
// Any other script:
GameData.Score += 5;
console.log(GameData.Score);
This technique allows you to have a controlled global “container” for your game values without polluting the global scope with loose variables.
let and const for local data,obj.Property for per-object data,window.MyVariable or GameData for shared global data.
This example shows how to combine global variables with UI events.
Here we declare a global variable Score in one script and modify it from another
that controls a button.
// Script: ScoreController.js
var Score = 0;
function Update(obj, dt) {
console.log("Current score:", Score);
}
// Script: ShopButton.js
function Start(obj) {
_onClickUp(obj, () => {
ShowInAppPurchases();
Score += 1;
});
}
Because the variable Score was declared with var,
both scripts share the same value.
Each click on the button will increase the global counter.
Score with GameData.Score
or window.Score.