SOLVED: Table madness - removing object and rows - Help!

Hi, This is driving me nuts - I can’t get it to work and when I do smaller test in simple project, it works…

I am having a table with an object reference and an name - so that I can find it.

So this is how I am filling up the table:

  
myLoader[loadCount] = {}  
myLoader[loadCount].obj = SpriteHelperLoader:initWithContentOfFileFromSubfolder("stage/", row.sheet)  
myLoader[loadCount].name = animalID  
  

And this is the code where I am trying to delete from.

for j = 1,table.getn( myLoader ) ,1 do  
 print(myLoader[j].name, id, #myLoader)  
end  
  
for i = 1, table.getn( myLoader ) ,1 do  
  
 if myLoader[i].name== id then  
 print("Deleting : " ..myLoader[i].name, i)  
  
 -- Remove loader from memory  
 myLoader[i].obj:removeSelf();  
  
 --Remove line from table  
 table.remove(myLoader, i)  
 break;  
 end  
  
end   

Before I delete a item, i am adding a new.

The first time it comes in to the delete function, everything seems fine. It is stating by the first loop that we have 5 entries. The second time I am trying to delete another entry i am getting errors within the first print loop, even that it claims that there are 6 entries.

Help, kneedeep in code shit now…
[import]uid: 81188 topic_id: 23194 reply_id: 323194[/import]

I think you have to loop through the array from the end instead. That will hopefully solve your problems.

[lua]for i=#myLoader,1,-1 do
– remove table value
end[/lua] [import]uid: 103182 topic_id: 23194 reply_id: 92801[/import]

Nopp, same problems even if I run the loop backwards…any other suggestions?

Joakim [import]uid: 81188 topic_id: 23194 reply_id: 92808[/import]

The next time I run the delete sequence, i am getting error at:

if myLoader[i].name== id then

A classic one, attempt to index field ‘?’ (a nil value)

What the heck is going on here…
[import]uid: 81188 topic_id: 23194 reply_id: 92809[/import]

I don’t know if the function “table.getn” is the same as #table. Have you tried both?
[import]uid: 103182 topic_id: 23194 reply_id: 92815[/import]

use 2 array but i will tried to fix you code later.

But tell me bit more what you want to do with in your code?

This line seems to be the problem:-- Remove loader from memory
myLoader[i].obj:removeSelf(); [import]uid: 86417 topic_id: 23194 reply_id: 92816[/import]

@ marceloblanco, table.getn is supposed to be a safer way to get the actual record count.

@martin.edmaier, That line removes the object that holds my sprite sheet. Its a reference that holds the image just.

Purpose: I am staging five big animals on the screen, each one from a separate sprite sheet.

The game contains more animals, but I just want five on the stage at the time. So when the child is scrolling through the landscape, I am removing the animal to the left, and adding a new at 2 steps further in. So there are always five animals on stage.

If I do this in a simple project everything works fine, but here something happens.

Joakim [import]uid: 81188 topic_id: 23194 reply_id: 92824[/import]

Even if I comment that line, I getting the same error - so there something strange going on here… [import]uid: 81188 topic_id: 23194 reply_id: 92825[/import]

There is something strange with,

table.remove(myLoader[i], i)

Joakim [import]uid: 81188 topic_id: 23194 reply_id: 92829[/import]

what is the id? [import]uid: 86417 topic_id: 23194 reply_id: 92830[/import]

The id is the parameter from my function that holds the code, e.g. name in the lua table.

Sorry

[code]
local freeLoader = function (id)

for j =table.getn( myLoader ) ,1,-1 do
print(myLoader[j].name, id, #myLoader)
end

for i = table.getn( myLoader ) ,1, -1 do

if myLoader[i].name == id then
print("Deleting : " …myLoader[i].name, i)

– Remove loader from memory
myLoader[i].obj:removeSelf();

–Remove line from table
table.remove(myLoader, i)

break;
end

end
print(“Total rows:” … table.getn( myLoader ) )
end
master.freeLoader = freeLoader
[/code] [import]uid: 81188 topic_id: 23194 reply_id: 92835[/import]

I just put togheter a sample and this doesn’t work as well. I must be doing something stupid here.

[code]
local myTable = {};
local counter = 1
local mainGroup = display.newGroup();
for i = 1, 10, 1 do

myTable[counter] = {}
myTable[counter].img = display.newImage(mainGroup,“head.png”,0,0)
myTable[counter].id = i
counter = counter + 1
end
local del = function(event)
if event.phase == “ended” then
–Add a new entry
myTable[counter] = {}
myTable[counter].img = display.newImage(mainGroup,“head.png”,0,0)
myTable[counter].id = i
counter = counter + 1

local i = math.random(10)

for j = #myTable,1,-1 do
print(j)
if myTable[j].id==i then
table.remove(myTable,j)
break
end
end

for j = 1,#myTable,1 do
print(myTable[i].img)
end
end
end

Runtime:addEventListener(“touch”, del)

[/code] [import]uid: 81188 topic_id: 23194 reply_id: 92858[/import]

OK, i manage to get it to work and a correct delete function for a table should look something like this.

  
for i,val in pairs(myLoader) do   
 if val.name==id then   
 print("delete", val.name);   
 myLoader[i].obj:removeSelf()  
 table.remove(myLoader, i)   
 end   
end  
  

Thanks, Joakim [import]uid: 81188 topic_id: 23194 reply_id: 92864[/import]