Difference between revisions of "Variable"

From Castle Wiki
Line 3: Line 3:
Variables have a '''name''', such as '''$score'''.
Variables have a '''name''', such as '''$score'''.


== Variable types ==
== Variable scopes ==


====Actor variables====
====Actor====
[[File:Actor variables happy.png|thumb|The actor variables section of a blueprint]]
[[File:Actor variables happy.png|thumb|The actor variables section of a blueprint]]
Local variables are variables that are accessible by only the actor that owns it and its values cant be edited by other actors
[[Actor|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.
====Global variables====
 
To edit actor-local variables, open the [[Inspector]] for the actor's [[Blueprint]], and navigate to the '''Logic''' tab.
====Deck====
[[File:Global variables.png|thumb|The global variables tab of the Tools panel]]
[[File:Global variables.png|thumb|The global variables tab of the Tools panel]]
Global variables are variables that are accessed by all actors in all cards in the deck. Global variables have 2 possible lifetimes, 'deck play' and 'persistent per player'.
Deck variables, also sometimes referred to as global variables, are variables that are shared among all actors across all [[Card|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.


== Global variable lifetime ==
== Variable lifetime ==


====Deck play====
====Deck play====
Variables that have deck play lifetime have values that only last for one deck play before reseting to their initial value either by the player restarting the deck or leaving the deck and returning to it.
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 [[Card|Cards]] in the same deck play, the variables preserve their values. Changing or restarting cards does not reset deck variables.


==== Persistent per player ====
==== Persistent per player ====
Variables with 'persistent per player' lifetime 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.
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 persisent variables. They are not shared among players.
== Reading and writing variables at play time ==
== 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 [[Rule|Rules]] using the '''Set variable''' response. You can also do things when variables change using [[Trigger|triggers]] such as '''When a variable changes''' or '''When a variable reaches a value'''.
You can read the value of a variable using an [[Expression]]. You can change the value of a variable in [[Rule|Rules]] using the '''Set variable''' response. You can also do things when variables change using [[Trigger|triggers]] such as '''When a variable changes''' or '''When a variable reaches a value'''.
Line 26: Line 34:
== Displaying the value of variables ==
== 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".
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]].

Revision as of 18:43, 12 May 2022

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