Table binding (passing tables as reference)

Hi me and my drawings.

I have a table with data, I pass this table to a module, and send a message to other object, script B, then this script B look the data in the module, and take it into another variable inside him.

I noticed that if I change something from this table, in script B , the table is bind to the original table, and the changes are affected in script A.

but, what happen, if I change the variable in the module and set it to nil? (after the script comunication, both scripts already have the table) or, if I modify the table in script A, will it change in script B?

Then I can pass the reference at the start of the objects, and have no more comunication for passing data??

Can anyone explain me this? thanks

Tables are passed as reference. Therefore changing the table anywhere will result in modified table everywhere.
If you want to change the table without affecting the original one - you have to explicitly deepcopy it.
Setting a variable to nil does not destroy the table, it destroys the reference to it.

but, if I do

test[2]=nil

then in script B, the table in index 2 will be nil , correct?

If I understood you correctly - yes. Modifying the table affects every script.

interesting, thanks

I did some test, if you send a table to a module, and another script take that table, works as reference, but if you send.a table with the message passing system, it works as a value

Yes, the table is serialised when sent as a message.

2 Likes