local my_table = { 1 }
function init (self)
print (my_table)
end
In different collection it print different address, but variable have not destroy.
I think it’s because I have unload collection (delete all this scripts) and then load again. Is it true?
(Sorry for my English)
“local” in this case means that you get a variable that is local to that script instance. You get a new script instance for each time that game objects (or gui) is instanced, for example after loading a new collection. So, two game objects, using the same script will get it’s own “my_table”.
If you don’t specify “local”, it implies that the variable is global, and is instead shared among those script instances.
You also can read more about the “local” keyword in the Lua Documentation
EDIT: I realize i misspoke about how many script instances there are, and @alex.tyk97 is certainly right, that table will have the same address for those game objects that share that script instance. And ofc, if you modify the table, it is seen by the objects that share that script.
Yes, several game objects sharing the same script will have the same local scope. “self” is a reference to the current object and is intended to be used to store object local state.
This is not entirely true. A local variable declaration outside any of the lifecycle functions (init, update, final, on_message, on_input and on_reload) is available and shared among all script instances of that specific type. Here’s on overview of the different Lua scopes: Simple Lua question? Maybe? - #2 by britzl
Yes, i understand this. But my question was not quite in this.
Here are some actions in realtime game to take place, and that i want to understand:
Copy an object several times.
Look address of local variable in “init” or “update”
Delete all objects.
Create one same object.
Look adress of local variable in “init” or “update” now
Address from 2 not equal address from 5.
So local variables in script destroyed, when no one script in lifecycle?
If you can not understand my question, so i create a TestProject and ready to add you to demonstrate my question.