Creating a item collector based on entity_died problems

Place to get help with not working mods / modding interface.
kasandraen
Long Handed Inserter
Long Handed Inserter
Posts: 51
Joined: Wed May 27, 2015 11:00 pm
Contact:

Creating a item collector based on entity_died problems

Post by kasandraen »

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:

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)
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Creating a item collector based on entity_died problems

Post by Nexela »

area :: BoundingBox (optional): Search area if not defined and position is not defined the entire surface is searched
position :: RealPosition (optional): Search position if not defined and area is not defined the entire surface is searched

It however doesn't say what happens when area is defined but position is not. Try adding a position to check.

but you are also looking for items not entities, do these show up when using find_entities? I think you need item-entity

Code: Select all

for _, item in ipairs(event.entity.surface.find_entities_filtered{area={{POS.x-RANGE, POS.y-RANGE},{POS.x+RANGE, POS.y+RANGE}}, position=POS, type="item-entity"})

edit: for refrence https://github.com/kyranf/robotarmyfact ... d.lua#L402
kasandraen
Long Handed Inserter
Long Handed Inserter
Posts: 51
Joined: Wed May 27, 2015 11:00 pm
Contact:

Re: Creating a item collector based on entity_died problems

Post by kasandraen »

Okay, now it works. buuuuuut
The loot drops from the unit after the events.on_entity_died, so a unit needs to die right next to it for it to move the items :<
Guess I'll have to find another way...
Post Reply

Return to “Modding help”