Creating a item collector based on entity_died problems
Posted: Sat Jul 30, 2016 7:08 am
Hi
I'm trying to make a item collector based on entity_died event.
When a unit dies, it scan around the corpse for item (to find its own loot), thenit scans a 64x64 area for a chest named "loot-chest". If it finds a chest it insert the items into the inventoy.
My problem is that the
"for _, item in ipairs(event.entity.surface.find_entities_filtered{area={{POS.x-RANGE, POS.y-RANGE},{POS.x+RANGE, POS.y+RANGE}},type="item"}) do" never works, it never runs, and therefor no loot table is made and nothing works...
This is way over my skill level in lua, but I thought I'd give it a go to see if I can learn but so far it only proving to be a headache... nothing works as exspected...
here's the code:
I'm trying to make a item collector based on entity_died event.
When a unit dies, it scan around the corpse for item (to find its own loot), thenit scans a 64x64 area for a chest named "loot-chest". If it finds a chest it insert the items into the inventoy.
My problem is that the
"for _, item in ipairs(event.entity.surface.find_entities_filtered{area={{POS.x-RANGE, POS.y-RANGE},{POS.x+RANGE, POS.y+RANGE}},type="item"}) do" never works, it never runs, and therefor no loot table is made and nothing works...
This is way over my skill level in lua, but I thought I'd give it a go to see if I can learn but so far it only proving to be a headache... nothing works as exspected...
here's the code:
Code: Select all
script.on_event(defines.events.on_entity_died, function(event)
if event.entity.type == "unit" then
local POS = event.entity.position
writeDebug(event.entity.name)
writeDebug(POS.x)
writeDebug(POS.y)
local Chests = 0
local RANGE = 64
local LOOT = {}
for _, item in ipairs(event.entity.surface.find_entities_filtered{area={{POS.x-RANGE, POS.y-RANGE},{POS.x+RANGE, POS.y+RANGE}},type="item"}) do
writeDebug(item)
if item.to_be_looted then
table.insert(LOOT, item)
writeDebug(LOOT)
end
end
if LOOT ~= nil then
for _, chest in ipairs(event.entity.surface.find_entities_filtered{area={{POS.x-RANGE, POS.y-RANGE},{POS.x+RANGE, POS.y+RANGE}},name="loot-chest"}) do
writeDebug(chest.name)
if Chests == 0 then
if chest.can_insert(LOOT) then
local Chests = Chests + 1
local Inventory = {}
chest.get_inventory(Inventory)
chest.insert(LOOT)
end
end
end
end
end
end)