Page 1 of 1

How to know what items lie on a belt?

Posted: Sun Jul 26, 2015 9:55 am
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?

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

Posted: Sun Jul 26, 2015 11:37 am
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.

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

Posted: Sun Jul 26, 2015 5:38 pm
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