So first thing I need to do is add newly created thumpers to a table each time it's created. If I'm correct, this is needed for the "set_multi_command" that needs a target to be a table.
I'm still having problems with tables, adding I got, but not removing.
Here is my current code.
The below goes in my "function On_Built(event)" code
On-Built:
Code: Select all
--- Add to Table
if event.created_entity.name == "Thumper" then
table.insert(global.ThumperTable,event.created_entity)
end
The below goes in my "function On_Removed(event)" code
Code: Select all
--- On Remove
for k,T in ipairs(global.ThumperTable) do
if not T.valid then
table.remove(global.ThumperTable,k)
if #global.ThumperTable == 0 then
global.ThumperTable = nil
end
end
end
Code: Select all
game.on_event(defines.events.on_sector_scanned, function(event)
--- Each time a Thumper "Scans", it will attract biters in the area
if event.radar.name == "Thumper" then
game.get_surface(1).set_multi_command({type=defines.command.attack,target=ThumperTable[#ThumperTable-1],distraction=defines.distraction.none},10)
end
end)
- I don't think my table removal code is correct, but don't know how to fix it.
- For my attack commend, I need to specify a table. So here is where I'll give it the thumber table. But my code is not 100% correct here.
- Lastly, there will be a problem when you have multiple thumpers. Each time a thumper completes a scan, it should trigger the attack, but how do I tell what thumper did the scan and have the biters attach that thumper?
I was thinking of maybe a counter, and each time a scan is completed to attach the next thumper. That way everyone will get a turn, it might not be the one that completes the scan, but each one will have a turn. Not very clean, since ideally the one completing the scan should be attached.
So I'm not sure how to complete/fix this code and was hoping I could get some help!
Thanks.