Page 1 of 1

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

Posted: Thu Jan 14, 2021 7:10 pm
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