how to reset or clear a table

Hi

I have a table that I add items too during gameplay. How do I reset this when I complete a level

–global or local table for the project

list = {“apple”,“pear”,“orange”}
function newLevel ()

list = {} --does this reset the table above or simply create a different local table for the function?

–code here

end [import]uid: 3093 topic_id: 16732 reply_id: 316732[/import]

Hmm… dangerous question…

Simple answer is yes… setting table={} will “clear” or “reset” it but… if your table includes much more than what you have shown (In Lua a table can contain almost anything) you should take the time to remove the entries/objects within the table in order to free the memory that they use before “clearing/reseting” the table. (destroying the index for the table.) [import]uid: 40100 topic_id: 16732 reply_id: 62700[/import]

i always declare all my variables that or not local to a function at the top of my code like this

[blockcode]
–functions
local fnNew, fnSetup, fnStart
–images
local imgBG,imgMan,imgBox
–variables
local score,highscore,level,objects

function fnSetup()
score,highscore,level,objects = 0,0,1,{}
end

fnsetup()
[/blockcode]

this way the variables are local to this file and i can change them reset them or remove then from any function i need to
[import]uid: 7911 topic_id: 16732 reply_id: 62707[/import]

If it’s global what I usually do when I need to clear it run a loop that removes each value:

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

Although I think setting the point to nil should work as well (see below):

for i =1, #table do table[i] = nil end [import]uid: 23649 topic_id: 16732 reply_id: 62711[/import]

Declaring your variables is a good practice but… in reguards to tables… they can contain almost anything… and some things should be removed before you destroy the index that points to it…

hmm…

...  
local object = {}  
object[1]= display.newimage("sample1.png")  
object[2]= display.newimage("sample2.png")  
object[3]= display.newimage("sample3.png")  
object[4]= display.newimage("sample4.png")  
object[5]= display.newimage("sample5.png")  
--table with objects in it :)  
  
object = {}  
--last line did NOT destroy/remove those objects  

The last line (object = {})did NOT destroy/remove those objects nor did it free any memory used by them, it only destroyed the index that allowed you to referance them… you can now reuse this index(table) but if you do this repeatedly without removing the objects your app will run out of memory and crash… sooner or later. [import]uid: 40100 topic_id: 16732 reply_id: 62714[/import]