I'm having a little trouble with adding/removing from a table I'm using. What I'm doing is adding Rocket Silo's to a table when built, having biters attack it.
It works to a certain extent, but when I build and then remove a rocket silo and re-build it, I get an error. "Item does not exist"
I think my issue is with the
Here is my code:
Initialization:
Code: Select all
if not global.RocketSilos then
global.RocketSilos = {}
end
Code: Select all
if event.created_entity.name == "rocket-silo" then
--- Add to Table
table.insert(global.RocketSilos,event.created_entity)
--- Biters will attack the newly built Rocket Silo
game.get_surface(1).set_multi_command({type=defines.command.attack,target=global.RocketSilos[1],distraction=defines.distraction.none},2000)
end
Remove from Table:
--- On Remove
Code: Select all
if event.entity.name == "AlienControlStation" then
RS_Remove() -- Call function
end
Code: Select all
function RS_Remove(index)
if index then
if global.RocketSilos[index] and not global.RocketSilos[index].valid then
table.remove(global.RocketSilos, index)
return -- if an index was found and it's entity was not valid return after removing it
end
end
-- if no index was provided, or an inappropriate index was provided then loop through the table
for k,RSilo in ipairs(global.RocketSilos) do
if not RSilo.valid then
table.remove(global.RocketSilos,k)
end
end
end
I think my problem is with "RocketSilos[1]" in the set_multi_command function, but not sure how to update.
Thanks.