index argument for LuaInventory.find_item_stack to start the search at a specific index

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
tiriscef
Long Handed Inserter
Long Handed Inserter
Posts: 98
Joined: Thu Mar 28, 2019 8:45 pm
Contact:

index argument for LuaInventory.find_item_stack to start the search at a specific index

Post by tiriscef »

Hello best developers in the world,

the LuaInventory object has a find_item_stack-function which returns the first stack and index with the given item-prototype-name.
This is very useful if you need just one stack, because then you don't have to iterate all slots of the inventory and check if they contain the item - which is pretty expensive, because it creates a lot of useless LuaObjects.

I came repeatedly to cases where I needed all item stacks of a given type - instead of just the first one.
In some I could use the find_item_stack-function, because I consumed the stack. That resulted in way better performance. In others I had to resort to 'manually' iterating.

Therefore the request: an optional 'index' argument to the find_item_stack-function, which makes it start at the given index instead of at the beginning, allowing you to skip the previous stacks. Making loops like this possible:

Code: Select all

local stack, index = inventory.find_item_stack(name)

while stack do
  -- something useful
  stack, index = inventory.find_item_stack(name, index)
end

Post Reply

Return to “Modding interface requests”