How to know what items lie on a belt?

Place to get help with not working mods / modding interface.
Post Reply
SigmatroN
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Sat Apr 26, 2014 8:31 am
Contact:

How to know what items lie on a belt?

Post by SigmatroN »

Apparently, due to optimisation, command of looks: surface.find_entities_filtered({area = area, name = "item-on-ground"}) returns only stacks, that actually on the ground and not the belt. How can I get item stacks from belts?

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: How to know what items lie on a belt?

Post by Choumiko »

New object LuaTransportLine, accessible from entity as read method get_transport_line(index) - an interface to the items on transport belts.
Sounds like what you need, haven't tried it.

SigmatroN
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Sat Apr 26, 2014 8:31 am
Contact:

Re: How to know what items lie on a belt?

Post by SigmatroN »

Thanks. Did it the other way.

Code: Select all

--counts items with particular name up to max_count 
function cyberchest.get_count_on_ground(self, item_name, max_count)
	if not self.ground_collection_allowed then return 0 end
	local area = {{self.entity.position.x - 5,self.entity.position.y - 5}, {self.entity.position.x + 5, self.entity.position.y + 5}}
	local belts = surface.find_entities_filtered({area = area, type = "transport-belt"})
	local count = 0
	for _, belt in pairs(belts) do
		if count >= max_count then --stop when enough
			return count
		end
		count = count + belt.get_item_count(item_name)
	end
	return count
end
--removes items with particular name
function cyberchest.remove_from_ground(self, item_name, count)
	local area = {{self.entity.position.x - 5,self.entity.position.y - 5}, {self.entity.position.x + 5, self.entity.position.y + 5}}
	local belts = surface.find_entities_filtered({area = area, type = "transport-belt"})
	for _, belt in pairs(belts) do
		if count == 0 then return end
		local b_count = belt.get_item_count(item_name)
		if b_count > 0 then
			belt.remove_item({name = item_name, count = math.min(b_count, count)})
			count = count - b_count
		end
	end
end

Post Reply

Return to “Modding help”