get a list of valid inventories for an entity

Post Reply
sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

get a list of valid inventories for an entity

Post by sparr »

I don't currently have a way to figure out which indexes I can pass to entity.get_inventory() to get its inventories. I could do a bunch of digging and trial and error to produce a lookup table from entity type, probably, but that seems like a bad approach.

maybe entity.has_inventory(x)->bool or entity.inventories()->LuaInventory[]

Sean Mirrsen
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Wed Apr 27, 2016 6:30 pm
Contact:

Re: get a list of valid inventories for an entity

Post by Sean Mirrsen »

sparr wrote:I don't currently have a way to figure out which indexes I can pass to entity.get_inventory() to get its inventories. I could do a bunch of digging and trial and error to produce a lookup table from entity type, probably, but that seems like a bad approach.

maybe entity.has_inventory(x)->bool or entity.inventories()->LuaInventory[]
You need the list of defines.inventory - http://lua-api.factorio.com/0.13.0-prev ... .inventory

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

Sean Mirrsen wrote:You need the list of defines.inventory - http://lua-api.factorio.com/0.13.0-prev ... .inventory
I already have that. What I need is to know which of those things go with which entities.

Sean Mirrsen
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Wed Apr 27, 2016 6:30 pm
Contact:

Re: get a list of valid inventories for an entity

Post by Sean Mirrsen »

sparr wrote:
Sean Mirrsen wrote:You need the list of defines.inventory - http://lua-api.factorio.com/0.13.0-prev ... .inventory
I already have that. What I need is to know which of those things go with which entities.
Uh, well most of the things on the list have pretty descriptive names. inventory_fuel goes for anything that has fuel, etc.

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

Yes, but how do I tell for an arbitrary entity whether it has fuel or not?

How do I tell which entities have a module inventory?

Etc.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: get a list of valid inventories for an entity

Post by DaveMcW »

Code: Select all

/c game.player.print(game.entity_prototypes["steel-furnace"].energy_usage.type)
Error: LuaEntityPrototype doesn't contain key energy_usage.

Code: Select all

/c game.player.print(game.entity_prototypes["electric-furnace"].module_specification.module_slots)
Error: LuaEntityPrototype doesn't contain key module_specification.

Sean Mirrsen
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Wed Apr 27, 2016 6:30 pm
Contact:

Re: get a list of valid inventories for an entity

Post by Sean Mirrsen »

DaveMcW wrote:

Code: Select all

/c game.player.print(game.entity_prototypes["steel-furnace"].energy_usage.type)
Error: LuaEntityPrototype doesn't contain key energy_usage.

Code: Select all

/c game.player.print(game.entity_prototypes["electric-furnace"].module_specification.module_slots)
Error: LuaEntityPrototype doesn't contain key module_specification.
I'm still not quite certain why you would need to programmatically determine whether an entity has a fuel or module inventory. You could just check what entity it is and work from that, unless you're trying to add something that dynamically affects anything of a particular category. And yes, a lot of the raw data is not accessible from the scope of the Lua code run during the game.

Depending on what you're trying to do, you might make do with checking via can_insert, or just running through the whole table of inventory indices and seeing what returns nil.

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

So I should make the assumption that things with no energy_usage don't have a fuel inventory?

After I query the module_slots, how do I tell whether it's defines.inventory.furnace.modules or defines.inventory.lab.modules or some other modules inventory? (yes, those seem "obvious", but they really aren't when you get into the details of all the different entity types)

Basically you're asking me to trial-and-error a lookup table and function to get the inventory list for an entity. That should be part of the API.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: get a list of valid inventories for an entity

Post by Adil »

The inventories are bound to type of entity, you can get type directly from entity.
Thus you only need to make a dictionary, maybe even programmatically, at the beginning of the mod, it'd be a dozen or two of entries but not completely out of possible.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

It's not quite a dictionary; it would have to be a function with some logic. Furnaces all have input and output, but only some of them have fuel (see above re energy_usage).

I could write that function. It would probably be 95-99% accurate. That's not accurate enough.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: get a list of valid inventories for an entity

Post by Adil »

Well, function would be 100% percent accurate if you add the relevant checks.

Also, where did you get that 'energy_usage'? Neither current nor next version expose that to runtime.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

Sean Mirrsen wrote:Depending on what you're trying to do, you might make do with checking via can_insert, or just running through the whole table of inventory indices and seeing what returns nil.
I filed a bug report alongside this request. Trying to access a nonexistent inventory index is currently a crash condition rather than returning nil. Someone on IRC pointed me towards pcall() to catch the error in lua and discard it, but that's really bad practice. Fortunately the bug report is already marked fixe din 0.13, so iterating through all indices and checking for nil is a viable approach next version.
Adil wrote:Well, function would be 100% percent accurate if you add the relevant checks.
I do not trust that anyone, including myself, is able to list every one of the relevant checks. Together a few of us can probably get 99%, missing some corner cases that will show up in random situations with weird mods.
Adil wrote:Also, where did you get that 'energy_usage'? Neither current nor next version expose that to runtime.
Just copied it from a post higher in this thread. This further proves my point. Coming up with the list of "relevant checks" is non-trivial.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: get a list of valid inventories for an entity

Post by DaveMcW »

Adil wrote:Also, where did you get that 'energy_usage'? Neither current nor next version expose that to runtime.
That's why this thread is in Modding interface requests.

slindenau
Long Handed Inserter
Long Handed Inserter
Posts: 73
Joined: Fri Mar 25, 2016 1:46 pm
Contact:

Re: get a list of valid inventories for an entity

Post by slindenau »

+1 for this request. Don't just throw an error if you're trying to access an inventory index that doesnt exist.
Either add a function to check if the inventory exists, or just return nil perhaps?

It is VERY bad practice that the end users have to determine based on the type of the entity what inventories it supports.
100% unmaintainable.
My mod: "Auto Deploy Destroyers" (follower robots) viewtopic.php?f=97&t=24545

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: get a list of valid inventories for an entity

Post by sparr »

slindenau wrote:+1 for this request. Don't just throw an error if you're trying to access an inventory index that doesnt exist.
Either add a function to check if the inventory exists, or just return nil perhaps?

It is VERY bad practice that the end users have to determine based on the type of the entity what inventories it supports.
100% unmaintainable.
Already confirmed in the bug report forum that get_inventory() will return nil instead of error in 0.13

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: get a list of valid inventories for an entity

Post by Rseding91 »

sparr wrote:
slindenau wrote:+1 for this request. Don't just throw an error if you're trying to access an inventory index that doesnt exist.
Either add a function to check if the inventory exists, or just return nil perhaps?

It is VERY bad practice that the end users have to determine based on the type of the entity what inventories it supports.
100% unmaintainable.
Already confirmed in the bug report forum that get_inventory() will return nil instead of error in 0.13
Indeed. It made no sense to throw an error since multiple entities might have or won't have an inventory and there was no way to know if it did before calling "get".

if anyone comes across other methods that throw errors when it doesn't make sense feel free to make bug reports about them. Specifically something is meant to throw an error when it's easily possible to tell if you should be calling that method to begin with. Something like trying to set a recipe on a rock for instance would be an error - you should only ever be calling that on an assembling machine type entity. Trying to get the electric energy of an entity however shouldn't generate an error because there's no good way to tell if an entity actually has electric power without just calling the get property.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Implemented mod requests”