Been stumped for weeks... I've tried both numeric and string indices.
All I want to do is take a table of images, back it up into various slots of the table cachedLevels
and then retrieve one of the backed-up tables at will. I'm very open to any approach to that result.
A simple test case has no problem, but my game does... and I have failed to find the difference... Any ideas what mistake could cause this behavior?
I'm definitely drawing a circle into only ONE image of ONE table (I hardcoded both indices) but ALL the tables get the circle in that particular image!
Here's an example of code that fails:
cachedLevels is my table of 9 tables (I'm testing table 7), each with 7 images (I'm testing image 3)
gfx.pushContext(cachedLevels[7][3])
gfx.fillCircleAtPoint(50, 50, 100)
gfx.popContext()
Then I draw cachedLevels[7][3]
and cachedLevels[8][3]
... and BOTH have a circle!
I am not doing any operation on cachedLevels[8]. I want it left alone.
(Meanwhile, cachedLevels [7][4], [8][4] etc. do NOT have a circle. But all the image [3]s in EVERY table 1 through 9 have the circle.)
FWIW, the tables within cachedLevels are initially populated by copying data into them as follows:
function table.clone(from)
return {table.unpack(from)}
end
And then (whether the current level [n] already exists or not)...
cachedLevels[n] = table.clone(levels)
I would have thought the table.clone function (found online) was the problem, except it works fine in my simple test case (which is also nested tables of images).
FWIW, any time I need to dump all the tables and start fresh I do this (which seems awkward but is found all over Lua forums):
cachedLevels = {}
table.insert(cachedLevels, 1)
cachedLevels = {}