Variable declaration

From ZCWiki
Jump to: navigation, search

You use variables to store values - numbers, for instance - that your scripts can recall and modify at later time during the script's execution. You must declare variables before you can use them. When you declare variable, you give it a name, a data type, and (optionally) an initial value.


Variables are declared as follows:

data-type name initial-value;

data-type is one of the following types:

  • int - an integer.
  • float - a floating point number (a number with a decimal point).
  • bool - a boolean value which is either true or false.
  • npc - a pointer (a kind of reference) to a Non-playing Character (such as an enemy) on the screen.
  • ffc - a pointer to a freeform combo on the screen.
  • item - a pointer to an item on the screen.
  • lweapon - a pointer to a weapon, usable by Link, on the screen.
  • eweapon - a pointer to an enemy's weapon on the screen.

You can define the name to be any useful name, as long as it does not contain any illegal characters (spaces, characters used as operators, etc.). Underscores are legal characters.

initial-value is an optional initial value for the variable, including the assignment operator. See the examples below.


A simple declaration for an integer:

int Hitpoints;

Declaring a float and assigning an initial value:

float Temperature = 98.6;


Variables have a defined "scope" - a visibility - depending on where they are declared. You may declare variables in two different areas of your scripts:

  • Inside a function block.
  • Outside of a script block.

For the sake of talking about scope, variables declared inside a function block are called local variables and variables declared outside of a script block are called global variables.

Local Variables

Local variables are declared inside a function block, as shown below:

global script Damage_Control{

  void run(){
    // Local declarations
    int Major_Damage = 4;

    // Link got hit!  Take away some hearts.

  void Hurt_Link(int Num_Hearts){
    // Local declarations
    int Hitpoints_per_Heart = 16;

    Link->HP = Link->HP - (Num_Hearts * Hitpoints_per_Heart);

In the above example, the run function contains a local variable named Major_Damage. Only function run can use this variable; no other function even knows it exists. Even though the function Hurt_Link is in the same script, it cannot use Major_Damage.

This is why these types of variables are referred to as "local"; they "stay" in the function block in which they are declared. When the function is called, the variable is created (and initialized if you specified a value). When the function is done running, the variable is destroyed.

Global Variables

Global variables are declared outside of a script block:

int Major_Damage = 4;

global script Damage_Control{

  void run(){

  void Hurt_Link(){
    // Local declarations
    int Hitpoints_per_Heart = 16;

    Link->HP = Link->HP - (Major_Damage * Hitpoints_per_Heart);

In the above example, the variable Major_Damage is declared outside the script Damage_Control. Because of this, every function can use this variable. Additionally, the value of the variable is stored with the game's "save data", so when you quit playing a quest and then start again later, the value of Major_Damage (or any other global variable) is restored from the save data.

Note that global variables only get initialized when the game is first started. For instance, in the example above, Major_Damage would only get initialized to 4 when a player first starts the quest. Subsequent sessions of the game would not cause the variable to be re-initialized.