What is the "<-" meaning ??

Started by ngocson389, Apr 24, 2019, 09:29 AM

Previous topic - Next topic

ngocson389

Hi guy, i'm new in squirrel, idk what is "<-" mean and usefor ? Can u explain it for me. Thanks!!

NicusorN5

You use the <- operator for creating a global variable.

Example
MyCar <- 5;
MyCarInstance <- FindVehicle(5);

MyCar is a global variable with the value 5, And MyCarInstance references to the car with ID 5.

dracc

Athanatos is not wrong, however he simplified a bit.

`<-` is the "new slot" assignment operator. It inserts a new slot in a table if there is not already one with that name and if there is, it's just regular assignment to that slot. Squirrel has a root table which means that assigning to a slot without a parent table will put a new slot in the root table leading to it being global.

Example:
local myLocalTable = {}; // An empty table.
myLocalTable.myInt <- 5; // Local integer value stored in the slot 'myInt' in the table 'myLocalTable'.

myInt <- 5; // Notice the lack of an explicit parent table.
            // This will become a slot 'myInt' in the root table effectively creating a global variable.

Sources and more information:
http://www.squirrel-lang.org/squirreldoc/reference/language/expressions.html#assignment
http://www.squirrel-lang.org/squirreldoc/reference/language/tables.html