Variables and scripts
Variables store game state that game mechanics are built on: collected items, points, decisions, counters.
Dynamic variables
Section titled “Dynamic variables”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 and implicit variables
Section titled “System and implicit 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 |
Placeholders
Section titled “Placeholders”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
variabledirective instead::variable[keysFound]{numberDecimals="0"}, see Content and Markdown.
Scripts
Section titled “Scripts”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" }- Placeholders in
exprare replaced, e.g.{{d:keysFound}} + 1becomes2.0 + 1. - The repository script (
defaults.scriptof the repository) is put in front of it – define shared helper functions there. - 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 // incrementMath.max({{d:bestScore}}, {{i:score}}) // keep the maximum"{{i:solutionText}}".toLowerCase() === "yes" // boolean from a text answer{{s:dayOfWeek}} >= 6 // true on weekendsString placeholders have to be quoted in scripts ("{{i:solutionText}}"), number and boolean placeholders not.