Author Topic: A curiosity of a code seen here  (Read 2647 times)

iOtero

  • Sr. Member
  • ****
  • Posts: 414
    • View Profile
A curiosity of a code seen here
« 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");
Nacer a los 15 años Una novela de iOtero

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: A curiosity of a code seen here
« Reply #1 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;
   }
}

iOtero

  • Sr. Member
  • ****
  • Posts: 414
    • View Profile
Re: A curiosity of a code seen here
« Reply #2 on: August 31, 2018, 06:40:18 AM »
so :: it indicates a global variable ... interesting, thanks.
Nacer a los 15 años Una novela de iOtero

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: A curiosity of a code seen here
« Reply #3 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.