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
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.)
i always declare all my variables that or not local to a function at the top of my code like this
1
2
3
4
5
6
7
8
9
10
11
12
| --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() |
If it's global what I usually do when I need to clear it run a loop that removes each value:
1
2
3
| for i = 1, #table do
table.remove(table, i)
end |
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...
1
2
3
4
5
6
7
8
9
10
11
| ...
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 |
views:1261 update:2011/10/25 9:10:48