Page 1 of 1

[Solved] Need little help for a item filter

Posted: Tue May 05, 2015 12:18 pm
by DOSorDIE
A simple question:
What must i change the line that it takes only "Coal" or "Wood"?
I konw this line is wrong ... if item = "coal" or "wood" then

Code: Select all

if #items > 0 then
				inventory = collector.getinventory(1)
				for _,item in pairs(items) do
					if item = "coal" or "wood" then
						if not item.isitemonbelt() then
							if inventory.caninsert(item.stack) then
								inventory.insert(item.stack)
								item.destroy()
								break
							end
						end
					end
				end
			end
Thank You!

Re: Need little help for a item filter

Posted: Tue May 05, 2015 12:25 pm
by orzelek
You can check name of the item - item.name.
It will be coal and raw-wood for those two - you might need to check names for others in data files.

Actual if needs to look like this:

Code: Select all

if item.name = "coal" or item.name = "raw-wood" then

Re: Need little help for a item filter

Posted: Tue May 05, 2015 12:31 pm
by DOSorDIE
Great!
I will test it now

Thanks!

Re: Need little help for a item filter

Posted: Wed May 06, 2015 8:09 am
by DOSorDIE
The right one was

Code: Select all

if item.stack.name == "coal" or item.stack.name == "raw-wood" then 
because we must filter it from "item-on-ground"

But you help us to get to the right direction.

Re: Need little help for a item filter

Posted: Wed May 06, 2015 8:28 am
by rk84
Even though solved. I want to post additional tips.
If you have alot of item names to filter you can use table.

Code: Select all

filter = {
  coal = true,
  ["raw-wood"] = true,
  ["solid-fuel"] = true
}

--

if filter[item.stack.name] then
or if you want fuel items you can do

Code: Select all

if game.itemprototypes[item.stack.name].fuelvalue > 0 then

Re: [Solved] Need little help for a item filter

Posted: Wed May 06, 2015 2:06 pm
by DOSorDIE
Thats Great ... and usefull for me.
Is there more?
For Ore as example?

Thanks!

Re: [Solved] Need little help for a item filter

Posted: Wed May 06, 2015 8:26 pm
by rk84
hmm this one relies on naming. Name of object needs to be same in both item and entity form, which usually is the case.

Code: Select all

if game.entityprototypes[item.stack.name] and game.entityprototypes[item.stack.name].type == "resource" then
edit: little fix