ContentsUsing Variables
Here are some general notes on using variables:
Variables are automatically "created" the first time a value is assigned to them.
Variables are essentially typeless. You don't have to "declare" variables to be of a specific type, like in some programming languages. Instead, variables can hold any type of value. You can freely assign a string to a variable that currently contains a number, and vice-versa. (This doesn't mean that types aren't important...the type of the value itself still matters for actions and functions and such.)
Variables are global by default. This means that once a variable is created, it is available throughout your project (unless you specifically make it a local variable).
Note: Variables persist across pages and dialogs. This means that variables hold their values across pages and dialogs. If you assign a value to a variable on one page or dialog, it will still have that value on another page or dialog—and another—and another—until eventually you assign something else to that variable.
Once a value
has been assigned to a variable, it will continue to contain that value
until you assign something else to it.
It's important to remember this when you're using variables to control
the "state" of things on a page or dialog (like remembering
whether the user turned the background music off). If you need
a variable to have a default value whenever a page or dialog is shown,
you'll need to set that default value yourself, by assigning a value to
the variable in that page's On Preload event.
Order is important.
When creating and using variables, be sure to think about when and
where they will be used, and what their values will be at that point in
time. A variable that is only assigned a value on the last page of your
AutoPlay application won't have a value when your application starts up.
For example, let's say you use a Dialog.Input
action that asks the user for their name and stores it in a variable called
UserName. It's important to realize that UserName won't contain the user's
name until after that Dialog.Input action is performed. If the action
is only performed when Page2 opens, UserName won't have a value earlier
on when Page1 is shown. In this case, using UserName in a Label.SetText
action on Page1 would result in an error because Label.SetText is expecting
a string value, but UserName doesn't contain a string. (It contains nothing,
represented by the keyword "nil.")
The same is true for using variables within a script. When you assign
a value to a variable, the value doesn't get assigned to the variable
until that line of script is performed; in other words, it's value is
only available to the actions that come "later." If you try
to use the variable in an action higher "up" in the same script,
or use it in another event that happens "earlier," the variable
either won't have a value yet, or it won't have the value you expected
it to have, and your AutoPlay application will either generate an error
or not behave as you intended. (This is why it's important to test your
application thoroughly before releasing it to the public.)
A variable is only available to the parts of the your application that
are performed after the variable
is created. If you want to use a variable in an action on Page4, make
sure a value is assigned to the variable before
that action is performed.
The built-in variables are listed along with all of the actions and constants when you press Ctrl+Space in the script editor. Since all of the built-in variables start with an underscore, if you type _ and then press Ctrl+Space, you'll be taken right to the first built-in variable in the list.