Skip to main content

Variables

A variable is a named value the game remembers while it runs: Score, Lives, PlayerName, DoorOpen. Each variable is a Number, Text or Yes / No (boolean).

Kinds of variables​

KindLives…Reset when…Create it in
Globalin every scenenever (while the game runs)Scene tab → Global variables, or a variable row at the top of an event sheet
Scenein one scenethe scene startsScene tab → Scene variables
Localinside one event, group or function (and its sub-events)every time that event runs (unless static)Event menu → Add local variable
Instanceon each copy of an objectthe object is createdInspector → Instance variables
Scene and global variables (Scene tab)
Scene and global variables (Scene tab)
Instance variables (Inspector)
Instance variables (Inspector)

You can also create a variable while filling in an action: pick + New variable… in the Variable list.

Using variables in events​

To…Use
Test a valueCompare variable (Score ≥ 10), Compare instance variable (Enemy.health ≤ 0)
Change a valueSet variable, Add to variable, Subtract from variable, Toggle variable (Yes/No)
Change an object’s valueSet / Add to / Subtract from instance variable
Show itIn any expression: "Score: " + Score, Enemy.health

Text variables: Add to variable appends text.

Saving between game sessions​

Variables are forgotten when the game closes. To keep a high score or unlocked levels, use:

  • Save value — stores a value on the device under a save name. Leave Value empty to save the variable with the same name.
  • Load value — reads it back into a variable (nothing happens if it was never saved). Leave Variable empty to load into the variable with the same name.

Example — best score:

ConditionsActions
⚡ On start of sceneLoad value best into Best
Score > BestSet Best to Score · Save value best = Best

Scripts: game.save('best', 10), game.load('best').

Saving the whole game​

Save value keeps one value. A save slot keeps the whole game, so the player can stop in the middle of a level and carry on later, like Construct's Save game to slot:

  • Save game slot — writes the scene that is open, every global and scene variable, and every object as it is now: where it is, its instance variables (health among them), its text, slider value, colour, the animation it plays and how fast it moves. Objects destroyed since the scene started are left out; objects created while playing are in. The event sheet's static variables and running timers are kept too.
  • Load game slot — brings it back. The current scene ends and the saved one is rebuilt from the save, after the tick. On start of scene does not run again; On game loaded does.
  • Has saved game slot — true when the slot holds a save: show a Continue button.
  • Delete saved game slot — removes it from the device.
  • On game saved, On game loaded, On load failed — triggers; leave their Slot empty to hear about every slot. On load failed fires when nothing is saved there (or the scene it was saved in has been deleted from the game), and the game goes on unchanged.

Slot names are free text (mysave, Slot 1); each game's saves are kept apart on the device.

Example — a pause menu with Save and Continue:

ConditionsActions
⚡ On clicked SaveButtonSave game to slot mysave
⚡ On game savedSet text of Toast to "Saved"
⚡ On clicked ContinueButtonLoad game from slot mysave
⚡ On start of scene · Slot mysave has a saved gameShow ContinueButton
⚡ On game loadedPlay music Level

What a save does not hold: sounds and music playing, particles in the air, tweens and timelines half-way, a Wait that was counting down, and the local lets of code scripts. Start those again in On game loaded (the music that was playing comes back by itself unless the event plays another). A script reads its object's variables in onGameLoaded(slot) to pick up where it was.

A save is as big as the scene's objects; a very large scene (big terrains, thousands of tiles) may not fit the device's storage, and Save game then reports it in the console and returns without saving.

Scripts: game.saveGame('mysave'), game.loadGame('mysave'), game.hasSaveGame('mysave'), game.deleteSaveGame('mysave'); hooks onGameSaved(slot), onGameLoaded(slot); events gameSaved, gameLoaded, loadFailed for game.on.

Names​

Variable names use letters, numbers and _, and can’t start with a number. They are case-sensitive: score and Score are different.