LuaItemStack#split_stack

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
PFQNiet
Fast Inserter
Fast Inserter
Posts: 234
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

LuaItemStack#split_stack

Post by PFQNiet »

If I want to transfer some portion of an item stack then I have to manipulate the stack's count. If that item stack could have durability, then this needs additional handling. I created this function in Lua:

Code: Select all

---@param source LuaItemStack
---@param destination LuaItemStack
---@param count uint
function split_stack(source, destination, count)
  if count >= source.count then
    return destination.transfer_stack(source)
  end
  
  source.count = source.count - count
  return destination.transfer_stack{name=source.name, count=count}
end
This seems to work. For example, a stack of 2 firearm magazines with an ammo count of 4/10 will allow me to split 1 (full) magazine away, then the next call will transfer the remaining 4/10 ammo magazine. Although I'm not confident this will work properly on items with health values saved. I actually don't know how those work at all...

I'm hoping to have this simplified into a LuaItemStack#split_stack function that works like transfer_stack but allows a quantity to be specified. Alternatively, just add a "count" parameter to LuaItemStack#transfer_stack that defaults to "the entire stack" if not passed.

Post Reply

Return to “Modding interface requests”