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?
Cacheability of inventories
Re: Cacheability of inventories
Should be fine,PFQNiet wrote: ↑Mon Jul 19, 2021 8:51 amI'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?
Also the stack has its own `.valid` operator that you should check before accessing it
Re: Cacheability of inventories
If I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...
Code: Select all
if entity.valid then
local slot = entity.get_inventory(defines.inventory.chest)[1]
-- slot IS valid here, right?
end
Re: Cacheability of inventories
If you are caching a single LuaItemStack, you can't use entity.valid or inventory.valid.
Re: Cacheability of inventories
But I though the point was to not look it up from the entity each time?PFQNiet wrote: ↑Mon Jul 19, 2021 12:33 pmIf I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...Code: Select all
if entity.valid then local slot = entity.get_inventory(defines.inventory.chest)[1] -- slot IS valid here, right? end
Re: Cacheability of inventories
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.
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.
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Cacheability of inventories
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.