How local variables work in .script file

I just want to know how local variables work.

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)

I’m not what you want to do… is it printing the content of the table? if then, you need to specify which index, like so:

local my_table = { 1 }
function init (self)
 print (my_table[1])
end
local my_table = { 1 }
print(my_table) -- printed the address, where table:'my_table' located in memory. 

I just want to know, why local variable in .script file have different address in memory in different collections.

“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.

But two game objects (and more) placing in one collection and using the same script will get same ‘my_table’ (one address for all scripts)

1 Like

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.

Check out http://www.defold.com/manuals/lua/#_locals_globals_and_lexical_scoping

1 Like

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

1 Like

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:

  1. Copy an object several times.
  2. Look address of local variable in “init” or “update”
  3. Delete all objects.
  4. Create one same object.
  5. 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.

What do you mean by “copy an object several times”? Deep copy of a Lua table? Spawn game object using factory?

“Look address of local variable in “init” or “update””? Do you mean local as in local keyword or something declared on self?

I think you better share the project with me. bjorn.ritzl@king.com