… Okay, I think to myself, I used [lua]local ccp = require(“variableStore”)[/lua], this means that I’m probably holding a copy of the table, so I need to get the most recent copy.
== first, this code:
[lua]local ccp = require(“variableStore”)[/lua]
doesn’t give you a *copy* of the table, it gives you the *real* (original) table… always. there’s no need to call refresh(). the parts of your code which have this line will be sharing a references to the same table thus you will instantly see changes anywhere you make them.
== second, you missed the important ‘local’ when declaring your variable store. without that, you’re essentially setting _G.variableStore, which is what you’re trying to avoid:
local variableStore = {} -- \<\<\<\< don't forget this 'local' -- refresh() is unneeded variableStore.refresh = function() print("in var store refresh") return variableStore end return variableStore
be sure to put ‘local’ in front of all of your variables when you first declare/define them, else they will be global. this could lead to unintended consequences and hard-to-find bugs (eg, perhaps ‘ccp’ being blown away somewhere else and needing to reset it with require() ). i’ve included an example below.
== third, i tried, but was unable to re-create your error. i used the following code:
[lua]
– main.lua
local varStore = require(“variableStore”)
print( varStore.a_var )
print( varStore.b_var )
local AClass = require(“aclass”)
print( varStore.a_var.this )
print( varStore.b_var )
local BClass = require(“bclass”)
print( varStore.a_var.this )
print( varStore.b_var.this )
varStore.refresh()
print( varStore.a_var.this )
print( varStore.b_var.this )
varStore.b_var.this=‘b_var changed in main’
print( varStore.a_var.this )
print( varStore.b_var.this )
print( aclass_var )
[/lua]
-- variableStore.lua local variableStore = {} -- refresh() is unneeded variableStore.refresh = function() print("in var store refresh") return variableStore end return variableStore
[lua]
– aclass.lua
local varStore = require(“variableStore”)
aclass_var = 45 – this is global, bad news !
varStore.a_var = { this=‘avar’}
[/lua]
[lua]
– bclass.lua
local varStore = require(“variableStore”)
varStore.b_var = { this=‘bvar’}
varStore.a_var.this=‘a_var changed in bclass’
[/lua]
-- output from main.lua, works as expected 2013-06-29 14:27:18.235 Corona Simulator[16594:f03] nil 2013-06-29 14:27:18.304 Corona Simulator[16594:f03] nil 2013-06-29 14:27:18.305 Corona Simulator[16594:f03] avar 2013-06-29 14:27:18.305 Corona Simulator[16594:f03] nil 2013-06-29 14:27:18.305 Corona Simulator[16594:f03] a\_var changed in bclass 2013-06-29 14:27:18.305 Corona Simulator[16594:f03] bvar 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] in var store refresh 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] a\_var changed in bclass 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] bvar 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] a\_var changed in bclass 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] b\_var changed in main 2013-06-29 14:27:18.306 Corona Simulator[16594:f03] 45
what did you do differently ?
cheers,
dmc