I have a piece of code that runs through a table to see if anything in that table matches. More specifically, the code checks if the current floor matches any ".floor" value in the table. The problem is, once it's checked, I'd like to delete that value from the table altogether, but I don't know how to do that. My current method doesn't work because if you run 'function selectPeople' twice, it throws and error because "persontable[i]:removeSelf()" appears to try to remove a nil value. What is the proper way to delete a value in a table completely?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | local function selectPeople(floor) local i for i = 1,table.maxn(persontable) do if ( persontable[i].floor == floor ) then persontable[i]:removeSelf() table.remove (persontable[i]) end end |
is persontable[i].floor a display object?
If all you are trying to do is remove the table point than
1 | table.remove(persontable[i]) |
If this code:
1 | table.remove(persontable[i]) |
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | local function selectPeople(floor) local i for i = 1,table.maxn(persontable) do if persontable[i] ~= nil then if ( persontable[i].floor == floor ) then persontable[i]:removeSelf() persontable[i] = nil collectgarbage("collect") table.remove (persontable[i]) end end end end |
Personally I would do :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | local function selectPeople(floor) local i for i = 1,table.maxn(persontable) do if ( persontable[i].floor == floor ) then display.remove(persontable[i]) persontable[i] = nil end end |
The problem with that Danny is two fold.
First, as I found out recently, #tablename which returns the length of the table will return a number too small if there is a nil entry in the table. I'm assuming that table.maxn() has the same behavior.
In other words:
t = { 1, 2, 3, 4, 5}
print(#t) -- 5
t = { 1, 2, nil, 4, 5}
print(#t) -- 2
If table.maxn() does the same, then the loop will never reach 4 and 5.
To work around this problem, you have two choices:
1. Don't use #tablename or table.maxn() and just have a maxSize variable and then as you loop through the table, test for tablename[i] being nil and only process it if its not.
2. Remove the empty table entry completely to avoid the nil entries. But depending on how fast this runs, removing the table entry before garbage collection runs could cause a memory leak. I could be wrong about this, but this is what it seems to be.
Please correct me if I'm wrong on this stuff. Even though I've been programming in Corona for about a year, there is still a lot of the magic under the hood I don't understand with all of this memory management stuff.
Thanks for the responses everyone!
Before posting, I had tried everything suggested except:
1 | collectgarbage("collect") |
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Purchasing Corona PRO broken??
Image Z order, Groups, and Director
Forum - Quote post still not there - Signatures
Linux support
Clear all objects from screen
tACKY - The Animals Can Kill You Music blog RSS App is now available!
[Physics] Applying Force Constantly - Gravity?
Video display object
Corona Cant Keep a Smooth 30fps - Animation Useless