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.