removing an item from an array

Forgive me if this is a question that’s been asked a gazillion times. I’ve searched and found ‘near-miss’ answers but nothing I’ve seen quite hits the spot.

I have TD game I’m coding. In this game the player can put down barricade squares to block enemy units. When an enemy unit hits a barricade, per game tick/loop some health is deducted until we hit zero then the barricade is removed. The following is my code for dealing with that…it is in the main game loop.


if (totalBarricades == 0) then
        print (“No barricades left”)
    else
        for i = 1, totalBarricades do        
                if (barricade[i].isAttacked == true) and (barricade[i].health > 0) then
                    barricade[i].health = barricade[i].health - 10
                    --and change graphic to show damage
                    print (“barricade”,i,“health is now”,barricade[i].health)
                end
        end
        for i = 1, totalBarricades do
                if (barricade[i].health <= 0) then
                    print (“REMOOOOOVING BARRICADE”, i)
                    --this function deletes target array item, and shunts down higher ones by 1
                    local deadBarricade = table.remove(barricade,i)
                    deadBarricade:removeSelf()
                    deadBarricade = nil
                    totalBarricades = totalBarricades - 1 --one less in the ‘total’!
                end    
        end
    --totalBarricades = totalBarricades - 1
    barricadeCount = totalBarricades


When I run my code I get the ol’ 'attempt to index field ‘?’ (a nil value). I’ve been struggling for the last few days to see whats going wrong here. Can anyone throw some light on this?

Many thanks.

Hi @alex.scarrow,

One main question is, how are you inserting barricade items/references into the table? Are you inserting them directly, or setting them as “dictionary” items (key-value pairs) within the table?

Note that when removing items from a Lua table, it’s usually best to loop through the table backwards. If you don’t, you run the risk of checking “every other” item in the table, because when one is removed, the item after then falls into the index slot of the one you just removed, but the loop iterator has increased, so then it never checks the item that just fell into the now-empty slot. Iterating through the table backwards circumvents this:

[lua]

for i = totalBarricades, 1, -1 do

   …

end

[/lua]

Hope this helps,

Brent

Thanks Brent. That’s sorted me out nicely.

Hi @alex.scarrow,

One main question is, how are you inserting barricade items/references into the table? Are you inserting them directly, or setting them as “dictionary” items (key-value pairs) within the table?

Note that when removing items from a Lua table, it’s usually best to loop through the table backwards. If you don’t, you run the risk of checking “every other” item in the table, because when one is removed, the item after then falls into the index slot of the one you just removed, but the loop iterator has increased, so then it never checks the item that just fell into the now-empty slot. Iterating through the table backwards circumvents this:

[lua]

for i = totalBarricades, 1, -1 do

   …

end

[/lua]

Hope this helps,

Brent

Thanks Brent. That’s sorted me out nicely.