Cacheability of inventories

Place to get help with not working mods / modding interface.
Post Reply
PFQNiet
Fast Inserter
Fast Inserter
Posts: 235
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Cacheability of inventories

Post by PFQNiet »

I've noticed my code is actually doing "entity.get_inventory(...)" in a less-than-optimal manner.

Can I just save a reference to the LuaInventory in global and use that in future calls, instead of having to query the game engine for it?

Similarly, what about stacks within that inventory? If my inventory only has one slot, it would be better if I can just store "entity.get_inventory(...)[1]" once and use that local cache instead.

I already have a guard for "entity.valid". If the entity remains valid, can I just assume the entity's inventories and item stacks are also valid?

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 4942
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Cacheability of inventories

Post by Klonan »

PFQNiet wrote:
Mon Jul 19, 2021 8:51 am
I've noticed my code is actually doing "entity.get_inventory(...)" in a less-than-optimal manner.

Can I just save a reference to the LuaInventory in global and use that in future calls, instead of having to query the game engine for it?

Similarly, what about stacks within that inventory? If my inventory only has one slot, it would be better if I can just store "entity.get_inventory(...)[1]" once and use that local cache instead.

I already have a guard for "entity.valid". If the entity remains valid, can I just assume the entity's inventories and item stacks are also valid?
Should be fine,
Also the stack has its own `.valid` operator that you should check before accessing it

PFQNiet
Fast Inserter
Fast Inserter
Posts: 235
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Cacheability of inventories

Post by PFQNiet »

If I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:

Code: Select all

if entity.valid then
  local slot = entity.get_inventory(defines.inventory.chest)[1]
  -- slot IS valid here, right?
end
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...

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

Re: Cacheability of inventories

Post by DaveMcW »

If you are caching a single LuaItemStack, you can't use entity.valid or inventory.valid.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 4942
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Cacheability of inventories

Post by Klonan »

PFQNiet wrote:
Mon Jul 19, 2021 12:33 pm
If I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:

Code: Select all

if entity.valid then
  local slot = entity.get_inventory(defines.inventory.chest)[1]
  -- slot IS valid here, right?
end
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...
But I though the point was to not look it up from the entity each time?

PFQNiet
Fast Inserter
Fast Inserter
Posts: 235
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Cacheability of inventories

Post by PFQNiet »

Right. I'm not explaining this very well XD

I am storing a reference to the entity, so entity.valid will be available to me. I want to know if it is "safe" to store the entity's inventory's first slot as well, so that I don't have to query the game for it each time I want to access that slot. Basically, reducing the number of API calls and thus improving the code performance.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5201
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Cacheability of inventories

Post by eradicator »

Eradicator's Visible Stockpile stores thousands of LuaItemStack references (for update randomization), never had a problem with that. They do have valid and valid_for_read.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”