Proper Table Cleanup

I understand that you can use “table.remove(table)”, when i do this, does it also destroy and nil all variables inside the table for cleanup? If i have several tables in one MAIN table, and i do table.remove(maintable), will all the sub tables inside of MAIN be destroyed as well?

Thank you [import]uid: 19620 topic_id: 19677 reply_id: 319677[/import]

[lua]local t = {

r = {
var = “one”
},
l = “two”
}

table.remove(t)
for i,v in pairs(t) do
print(i,v)
end[/lua]

table.remove(t) function did nothing at all [import]uid: 16142 topic_id: 19677 reply_id: 76114[/import]

table.remove() does not remove whole tables, it only removes an element from a table.
If you don’t specify an element number, then the last element is assumed.

Also, this function only works on tables with numerical indices (or implicit numerical indices as given below).

[lua]local t =
{
{var = “one”},
“two”
}

for i,v in pairs(t) do
print(i,v)
end

print"—"

table.remove(t) – last element is removed
for i,v in pairs(t) do
print(i,v)
end[/lua]

output:

1 table: 0x18ddd80
2 two
---
1 table: 0x18ddd80

.

If you want to clean up all elements from a table you will have to write your own function to iterate trough the table and call table.remove() for each element.
Please note that if some elements are display-objects you will have to make sure you call removeSelf() on those before you remove them from the table. [import]uid: 70847 topic_id: 19677 reply_id: 76146[/import]

Ok maybe you could help me out, honestly i am just finally starting to use tables and love the power behind them. So perhaps i could do something similar to this…

[lua]tableStep = 1

for i=1, #mainTable do
table.remove(mainTable[tableStep])
tableStep = tableStep + 1
end[/lua]

Is this something close to what i could do to go through the table and remove all the elements? by the way my table is only holding numbers.
thanks for your help. [import]uid: 19620 topic_id: 19677 reply_id: 76217[/import]

It’s better to remove from the end of the table like so:

[lua]for i = #mainTable, 1, -1 do
table.remove(mainTable, i)
end

mainTable = nil; [/lua] [import]uid: 70847 topic_id: 19677 reply_id: 76221[/import]

Great, that seems to be working perfectly. I forgot that table.remove starts at the last of the tables list so that makes sense. Thank you for your help! I haven’t removed tables this method in the past and haven’t come across to much trouble in my app, is it just freeing up some memory when this is done properly? [import]uid: 19620 topic_id: 19677 reply_id: 76225[/import]

Yep. you got it! [import]uid: 70847 topic_id: 19677 reply_id: 76226[/import]

Hey sorry to bother again, hoping you can shed some light on a issue im having. So in my game, i am creating a table and recording movement that the player makes, and at the end of the level im saving the table using the “ice” module. when i am leaving the level and doing clean up, i am doing what you suggested above to remove the table, which seems to work fine. But then when i return to replay the same level, it finds the table as nil. Now this only occurs when i return immediately to the same level, if i quit out, or play another level and go back to the previous, then my code is able to find the saved table information.

Its like my code isnt creating a new table if i do immediate replay. Anyways not sure if you would have any clue of what might be going on there. here is a bit of code.

[lua]local ghostTableTempX = {}
local ghostTableTempY = {}

local ghostTableX = {}
local ghostTableY = {}

–These two lines retrieve the saved “ghost” data from a table i save the first time you play a level
ghostTableX = global.tireData:retrieve(“level”…levelNumber…“GhostX”)
ghostTableY = global.tireData:retrieve(“level”…levelNumber…“GhostY”)

–I use this at the end of my level to store the table that holds the movements
global.tireData:store( “level”…levelNumber…“GhostX”, ghostTableTempX )
global.tireData:store( “level”…levelNumber…“GhostY”, ghostTableTempY )
global.tireData:save()

–Destroy ghost tables (i call this when im cleaning up a level)

for i = #ghostTableTempX, 1, -1 do
table.remove(ghostTableTempX, i)
end
ghostTableTempX = nil

for i = #ghostTableTempY, 1, -1 do
table.remove(ghostTableTempY, i)
end
ghostTableTempY = nil

if wasGhostOpen == true then
for i = #ghostTableX, 1, -1 do
table.remove(ghostTableX, i)
print(i)
end
ghostTableX = nil

for i = #ghostTableY, 1, -1 do
table.remove(ghostTableY, i)
end
ghostTableY = nil
end

–These functions record movement and play them back
frameStepRecord = 1
local trackGhost = function()
if isGhostRecord == true then

ghostTableTempX[frameStepRecord] = M.tireObject.x

ghostTableTempY[frameStepRecord] = M.tireObject.y

frameStepRecord = frameStepRecord + 1
end
end

frameStepPlay = 1

local playGhost = function()

if ghostActive == true and ghostRun == true then

if frameStepPlay == ghostEndPoint then
ghostRun = false
end

ghostTire.x = ghostTableX[frameStepPlay]

ghostTire.y = ghostTableY[frameStepPlay]

frameStepPlay = frameStepPlay + 1

end
end[/lua]

So basically when i replay the same current level im on then it says ghostTableX and ghostTableY are nil values, but if i go back to the main menu of my game, load up that same level again, the first run it will play back the data just fine, do replay, and those are shown as nil values again. [import]uid: 19620 topic_id: 19677 reply_id: 77105[/import]

Hi,

I am assuming that’s not the way your code is setup (i.e.: you do not retrieve and save right away for instance)

I will try to check if ghostTableX/Y are not nil (~= nil) and if they are then set them again local ghostTableX = {} )

Hope this help.

Mo [import]uid: 49236 topic_id: 19677 reply_id: 77128[/import]

Thanks for the reply, yea my code isnt setup exactly like this, i was just trying to show the gist of what is going on. [import]uid: 19620 topic_id: 19677 reply_id: 77133[/import]