Variable

From Castle Wiki
Revision as of 19:43, 13 May 2022 by Ben (talk | contribs) (fix typo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Variable stores a numeric value to be used at play time. Variables can be used for accumulating player scores in a game, tracking player money and transactions, remembering properties of objects in a simulation deck, or can just be used as conveniently editable parameters which you can tweak later.

Variables have a name, such as $score.

Variable scopes

Actor

The actor variables section of a blueprint

Actors can have variables which are different for each instance of the actor. These are sometimes referred to as Local Variables or Actor-local variables. For example, each instance of an enemy might have a different amount of hit points.

To edit actor-local variables, open the Inspector for the actor's Blueprint, and navigate to the Logic tab.

Deck

The global variables tab of the Tools panel

Deck variables, also sometimes referred to as global variables, are variables that are shared among all actors across all Cards in the Deck. One common use for deck variables is to keep track of the player's overall score.

Deck variables have two possible lifetimes, deck play (the default) and persistent per player.

To edit deck variables, press the Gear button at the top of the editor to open the Settings sheet, then navigate to the Variables tab.

Variable lifetime

Deck play

Deck variables that have deck play lifetime will remember their values within a single deck play. If the player exits the deck and comes back (e.g. by swiping away or restarting the app), the variables reset to their initial value.

Note that when a player moves between different Cards in the same deck play, the variables preserve their values. Changing or restarting cards does not reset deck variables.

Persistent per player

Variables with persistent per player lifetime, also known as persistent variables, save progress between deck plays. Leaving and returning to a deck with a persistent variable will preserve the value of the variable, as will restarting the deck. Persistent variables are useful for saving game state and keeping track of a player's progress between plays.

As the name suggests, each player gets their own value for persistent variables. They are not shared among players.

Reading and writing variables at play time

You can read the value of a variable using an Expression. You can change the value of a variable in Rules using the Set variable response. You can also do things when variables change using triggers such as When a variable changes or When a variable reaches a value.

Displaying the value of variables

Inside any Text, you can write the variable name (including the $) to display the value of the variable when the deck is played. For example: "Your score is $score".

Resetting variables

Actor-local variables are initialized when the actor instance is created and forgotten when the actor instance is destroyed. Deck-play variables are reset when the deck restarts.

You might want to reset a variable outside of these cases. To do that, use the Reset a variable to its initial value Response.