I got a strange thing today.
I have a player_data module in which I have a public variable called SCENE. After my game loops again, that is, all player_data values are set to their default values, I experienced a very very strange thing. I print the value of player_data at two scripts, one my controller script, and my player script, in update function. In my controller script, I get its value as
menu -- which it should be.
And in player script, it prints as
main--which it should not be
How can one variable have two values at the same time? The issue is very intriguing. So I decided to ask for help.
Thanks in advance
I checked every script today, and no typos. Moreover, this issue only shows itself after I play the game once, and the then reassign the player_data to its default values @sicher I think I don’t get this, aren’t lua modules supposed to be global thingies, once you change their value, it is persistent all over the game. Moreover, every assignment that I make to player_data’s other variables seem to work out just fine, everything except this.
After working on a possible workaround, I finally found one. So, I will stay with the solution for a while, while thanking everyone.
BTW I have noticed something
local a = some_table
table.remove(a, 1)
Actually removed an element from some_table, but this should not have been, isn’t it?
That is exactly as expected. If some_table is of type table then a and some_table will both refer to the same table. a will not be a copy of some_table. The same goes for values of type userdata, for instance the Defold vector3, vector4 and matrix.
Oh!! So odd of me, after using Defold for a long time now, I still didnt knew this(and kept thinking that it was a bug ) . Thanks for helping me out @britzl.
And the same doesn’t apply to fundamental data types like floats, Strings, hashes, ints etc, isn’t it?
And BTW do we then copy the table in a new variable, by using the traditional method of for loops?
Yes, sorry. I don’t know why but I totally forgot the module mechanics.
When you require a module, Lua creates a table for the module and caches it. When you require it again you get a reference to the same table from the cache (by default, you can clear the cache if you want to).
If you print the variable holding the ref to the table you will get the table address so you can check if two variables points to the same table.