SuperTux2 Level Editor - Global and Local Variables

This topic was published by and viewed 1539 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts

  • Jarret Buse
    Participant
    • Topics - 25
    • @jbuse1

    Within a SuperTux, level there are two types of variables which can be used to hold values.

    The values which can be held by a variable are integers, floats, boolean, strings, etc. The values in the variables can be used for later purposes, but the two types are different.

    A Local Variable is one which is stored and retrieved within a script. For example, if you have a ScriptTrigger Object and Tux enters it, any local variables are only kept while Tux is within the Object. When Tux enters another object, the variable and its contents are lost.

    A Global Variable is one which is stored and is still accessible in another scripting Object. Values can be kept throughout the level to be retrieved and/or changed.

    The way a Global Variable works in Squirrel is that it is stored in the Root Table. The Root Table is kept intact until the Squirrel Virtual Machine is shut down. A little more clearly stated, a Global Variable within a Sector will remain intact and accessible within all scripts and events.

    To create a Global Variable, a value must be assigned to it. To create and assign a value to a Global Variable is done by "GlobalVariableName

    A Variable (Global or Local) name must begin with a letter (a-z or A-Z) as well as an underscore (_). Examples are "_value", "A", "a", "value1", etc.

    NOTE: Be aware that Variables are case-sensitive. "A" and "a" are two separate variables.

    After a Global Variable is created, the value can be changed by simply using an Equal Sign (=). A Global variable, except when created, is always proceeded by two colons (::) to signify it is in the Root Table.

    So, to change a Global Variable which is already created, the statement would be "::a=5;".

    Local Variables are created by using the word "local". To create a Local variable called "test" and assign it the value of 100, the statement would be "local test=100;". Local Variables only exist while Tux is within the ScriptObject or event. Once the script ends, all Local Variables are lost.

    Another thing you may notice is that all statements end with a semicolon (;). The semicolon is used to end a statement. There are a few exceptions when a semicolon is not needed, but these will be covered later when needed.

    So, let's look at an example. Let's say Tux enters an are that is a ScriptTrigger. The following code is in the script:

    a<-1;
    local b=2;
    local c=a+b;

    At the end of this script, "a" = 1, "b" = 2 and "c" = 3. If Tux leaves the ScriptTrigger area and enters another, the only variable which still exists is "::a" since it is a Global Variable. The variables "b" and "c" do not exist once Tux leaves the first ScriptTrigger.

    Local Variables are accessed faster than Global Variables. If you have a script which uses a lot of Global Variables, even the same one over and over, simply copy its contents to a Local Variable and use the Local Variable. For example, at the beginning of the script, copy the contents to a Local Variable by:

    local lcount = ::gcount;

    Now, throughout the rest of the script use "lcount" and not "::gcount". If the value should be changed, at the end of the script copy the contents back to the Global Variable by:

    ::gcount = lcount;

    Only use Global Variables when some information needs to be kept throughout a whole Sector.

    Further Reading

Viewing 1 post (of 1 total)