Attract-Mode Support Forum

Attract-Mode Support => Scripting => Topic started by: iOtero on August 30, 2018, 11:57:01 AM

Title: A curiosity of a code seen here
Post by: iOtero on August 30, 2018, 11:57:01 AM
What is the difference between this code:

Code: [Select]
::fe.add_transition_callback(this, "on_transition");
and this one:

Code: [Select]
fe.add_transition_callback(this, "on_transition");
Title: Re: A curiosity of a code seen here
Post by: keilmillerjr on August 30, 2018, 02:19:16 PM
According to 3.0 docs:

Quote
Global variables are stored in a table called the root table. Usually in the global scope the environment object is the root table, but to explicitly access the global table from another scope, the slot name must be prefixed with '::' (::foo).

exp:= '::' id

For instance:

function testy(arg)
{
    local a=10;
    return arg+::foo;
}
         
accesses the global variable 'foo'.

However (since squirrel 2.0) if a variable is not local and is not found in the 'this' object Squirrel will search it in the root table.

function test() {
   foo = 10;
}
is equivalent to write

function test() {
   if("foo" in this) {
      this.foo = 10;
   }else {
      ::foo = 10;
   }
}
Title: Re: A curiosity of a code seen here
Post by: iOtero on August 31, 2018, 06:40:18 AM
so :: it indicates a global variable ... interesting, thanks.
Title: Re: A curiosity of a code seen here
Post by: keilmillerjr on August 31, 2018, 07:31:14 AM
so :: it indicates a global variable ... interesting, thanks.

Global variable must be set as such, but can be accessed with or without if no local variable has same name.