Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: ngocson389 on April 24th, 2019, 10:29 AM

Title: What is the "<-" meaning ??
Post by: ngocson389 on April 24th, 2019, 10:29 AM
Hi guy, i'm new in squirrel, idk what is "<-" mean and usefor ? Can u explain it for me. Thanks!!
Title: Re: What is the "<-" meaning ??
Post by: Athanatos on April 24th, 2019, 12:01 PM
You use the <- operator for creating a global variable.

Example
Code: [Select]
MyCar <- 5;
MyCarInstance <- FindVehicle(5);

MyCar is a global variable with the value 5, And MyCarInstance references to the car with ID 5.
Title: Re: What is the "<-" meaning ??
Post by: dracc on April 24th, 2019, 01:13 PM
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:
Code: [Select]
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