Different values of one variable at the same time(SOLVED)

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

You likely have two different instances of the module data. What does the module look like and how do the two scripts use it?

1 Like

The module is like

local M = {}
--some other variables
M.SCENE = ""
return M

Actually only the controller changes the scene’s values whenever I load a scene. All other scripts just access it’s value.

How do the scripts set and read the value?

The controller script sets the value as

local function load(self, scene) 
player_data.SCENE = scene
--animate gui
end

The other scripts access it as

if player_data.SCENE ~= "menu" then
print ("not menu")
end

Yes, but how do the scripts get hold of the variable player_data? Is that a global or a script local?

It’s a global variable. Defined on the top in every script. And officialy a lua module.

What do you mean you define it at the top of every script?

I mean that it is a global module whose values are accesses by all the scripts
In the beginning of every script, It is defined as

local player_data = require "Game.Modules.player_data"
local function init(self)
--do things
end

BTW I am very bad at explaining things, so thanks for bearing with me.

Can you show both of your require lines?
I think you have a difference in paths symbols (some of them are capital)

Yes that’s a strange thing I did, but thats correct, with G of Game and M of Modules capital and rest all small.

What I mean: LUA modules + local variables

local player_data = require “Game.Modules.player_data”
and
local player_data = require “game.Modules.player_data”

both works, but it will be two different modules.

Would be better don’t use capital symbols in files and folders names to avoid this kind of mistakes.

When you write local player_data = ... you create a new script local variable for each script. That is not a global variable.

So player_script.SCENE in one script is a different variable than in the other script.

1 Like

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.

Can you please share a minimal example showing the problem you describe? I’m sure it’s something super trivial that we’re overlooking.

1 Like

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.

2 Likes

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

2 Likes

It depends how you want to copy the table data. Here is a page with two types of copy: http://lua-users.org/wiki/CopyTable

1 Like