🧩 Variable Scope in Scripts

What is scope?

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.

📖 Ways to declare variables

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.

✅ Example: local (private) variable

// 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.

🌍 Example: global variable (shared between scripts)

// 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.

⚠️ Use global variables carefully. If two different scripts use the same name, they will overwrite each other.

🎮 Example: variable stored inside an object

// 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...).

🌐 Using 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:

✅ Example: declaring a persistent global variable

// 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:

💡 Important: window variables behave like permanent storage for the whole game session. They remain intact when changing scenes or loading/unloading objects.

🎮 Example using window with buttons

function 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.

🧠 Example: global data system (GameData)

// 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.

💡 Recommendation:
Use let and const for local data,
obj.Property for per-object data,
window.MyVariable or GameData for shared global data.

🖱️ Practical example with buttons and global variables

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 1 – Score controller

// Script: ScoreController.js
var Score = 0;

function Update(obj, dt) {
  console.log("Current score:", Score);
}

🖱️ Script 2 – Shop button

// 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.

💡 If you prefer a cleaner structure for global data, you can replace Score with GameData.Score or window.Score.