Skip to content

Variables and scripts

Variables store game state that game mechanics are built on: collected items, points, decisions, counters.

Dynamic variables are defined in the repository. They have a type and a scope.

{ "name": "keysFound", "t": "Number", "scope": "Session", "defaultValue": "0" }
Type t Values
Number numbers; stored as decimal numbers (3 is stored as 3.0)
String text
Boolean true / false
Scope One value per … Use for
Session session (one play-through) progress in this game: collected items, decisions
Account player account, across all sessions of the repository player achievements: games finished, badges
Repo repository, shared by all players global counters: how often something happened

Instances of a variable are created lazily with the defaultValue when they are used for the first time. A voucher can set initial values for session variables.

System variables (prefix s:) are provided by the engine:

Name Value
currentTime current time in milliseconds since 1970-01-01
dayOfWeek 1 (Monday) to 7 (Sunday)
dayOfMonth day of the month
dayOfYear day of the year

Implicit variables (prefix i:) describe the current session and event (complete list: Variables):

Name Value
username, screenName, userId the player
teamName, score, sessionId, attendeeLang the session
storyId, storyTitle, repoId, repoTitle the story and repository
challengeId, challengeTitle the current challenge (in challenge and element events)
elementId, elementTitle the current element (in element events)
solutionAt, solutionScore time and score of the processed element
solutionText, solutionTextError, solutionChoice the submitted text / wrong text / multiple choice answer
solutionInput, solutionImageId, solutionImageUrl the submitted user input
solutionLat, solutionLon the position of a geofence solution

Variables are referenced with placeholders:

{{d:keysFound}} dynamic variable keysFound
{{i:username}} implicit variable username
{{s:dayOfWeek}} system variable dayOfWeek
  • In scripts and TotoObject payloads the backend replaces placeholders with the current values before execution.
  • In conditions of conditional content the backend replaces them as well.
  • To show a value in a text, use the variable directive instead: :variable[keysFound]{numberDecimals="0"}, see Content and Markdown.

A script sets a variable: it is attached to a trigger with the id of the variable (parentId) and an expression (expr).

{ "parentId": "<id of keysFound>", "expr": "{{d:keysFound}} + 1", "lang": "Javascript" }
  1. Placeholders in expr are replaced, e.g. {{d:keysFound}} + 1 becomes 2.0 + 1.
  2. The repository script (defaults.script of the repository) is put in front of it – define shared helper functions there.
  3. The script runs as JavaScript (ECMAScript 5, no libraries) in a sandbox. The value of the last expression is the new value of the variable.

If a script fails or runs longer than 1000 ms, the variable keeps its value and the error is logged as activity of the repository (visible to authors).

Examples:

{{d:keysFound}} + 1 // increment
Math.max({{d:bestScore}}, {{i:score}}) // keep the maximum
"{{i:solutionText}}".toLowerCase() === "yes" // boolean from a text answer
{{s:dayOfWeek}} >= 6 // true on weekends

String placeholders have to be quoted in scripts ("{{i:solutionText}}"), number and boolean placeholders not.