Returning a table does just that: you get a reference to the table itself. No copying. So changes to one are a change to the “other”.
Also, if you nil the table reference anyhow, wiping its contents clean sounds unnecessary. If that’s the last reference (say, you aren’t still holding the reference in the other file), it will become unreachable and the garbage collector will deal with it at some point for you.
The formatting codes are and without the space after the [
You should not use a variable called table. When you nil it, you loose all of the table functions like table.maxn. This is why globals are bad. At that point you’ve pretty much broken Lua.
I didn’t actually call it table, that was just to show what it was, I used something far more relevant to what it was actually doing. The table it’s self is not global, I made sure of it by printing out every global lua was running using:
for k,v in pairs(\_G) do print("Global key", k, "value", v) end
and the phantom global never showed it self. I have zero idea whats going on. If I was sure ignoring it wouldn’t affect users I wouldn’t bother to nil it out but I’m not and need to find a solution.
Can you post your real code. It’s very difficult for anyone in the forum community to help you without seeing it. Trying to guess based on your psuedo code isn’t helpful.
Here is how my app works. I have a page called data.lua that basically acts as server for all the data (so everything can stay local to it’s respective scene).
In game.lua I have a table called my_defeated_enemies. my_defeated_enemies is a multi deminisional.
Futhermore at the top of the top I declare the variable like so:
local my\_deafed\_enemies
Then on the hide scene in composer I do:
for i = 1, table.maxn(my\_deafed\_enemies) do my\_deafed\_enemies['type'][i] = nil my\_deafed\_enemies['health'] = nil my\_deafed\_enemies[i] = nil end
And no matter how I do this it is still over riding the variable in data.lua.
I’m not sure I really see the difference there, isn’t the local declaration making it local to that file?
Anyway I built something similar to this in a brand new app and it still did the same thing. I think there is a bug somewhere in composer or lua that is causing some sort of global override.
I’m assuming that you have a table called data in your data module.
If you just do:
local function doSomething()
return something
end
That function is local to that module only can can’t be accessed outside of it. To make a function available in another module it has to be a member of the table that gets returned. You can do that one of two ways:
local M = {} – my module table
local function doSomething()
return something
end
M.doSomething = doSomething
In that form, you create a local function and assign the pointer to the function to the table. In the second form:
function M.doSomething()
return something
end
You directly assign the function to the table (in effect creating an anonymous function, but the result is the same to the caller).
The code you posted for the first one isn’t doing this. You create a local function but in the next line you’re assigning the function to itself:
At this point data.returnMyDefetedEnemies is nil since you made a function without the data. piece local above. In effect no function. I’m not sure how the second one is compiling at all. You can’t local a table method like that. Presumably you’ve already done:
It took some research but I did. It turns out returning a table to another scene like I did sets both tables as exactly the same thing (quasi globals) which meant nilling it in one file nilled it in the other.
My solution was to rewrite the table in the scene that had to display it.
Either it’s a Lua bug or I misunderstood how lua works in this regard.