how to know if item has inventory (ItemWithInventory ) ?

Place to get help with not working mods / modding interface.
Post Reply
Kruparker
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed May 25, 2016 6:16 pm
Contact:

how to know if item has inventory (ItemWithInventory ) ?

Post by Kruparker »

Everywhere on API doc for LuaItemStack,
members refer to "Can only be used if this is ItemWithInventory"

but there's no
ItemWithInventory :: Booleen [Read-only]
return true / false

get_inventory
Should
Return value
or nil if there is no inventory with the given index.
but Crash because " This is not an item-with-inventory."

Prototype / groups have nothing more.

So, please : How do I know if an item has an inventory or not (blueprint-book for example, or any new item defined in a mod ) ?

Helfima
Fast Inserter
Fast Inserter
Posts: 199
Joined: Tue Jun 28, 2016 11:40 am
Contact:

Re: how to know if item has inventory (ItemWithInventory ) ?

Post by Helfima »

u can catch the exception with pcall

Code: Select all

local ok , err = pcall(function()
		--the code to get_inventory
	end)
	if not ok then
		--code when error
	else
		--code when ok
	end

Kruparker
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed May 25, 2016 6:16 pm
Contact:

Re: how to know if item has inventory (ItemWithInventory ) ?

Post by Kruparker »

indeed, that's a way to do it, thank's

I'm not used to throw an exception to make an "if", but why not. =)

Post Reply

Return to “Modding help”