LuaInventory.insert/can_insert return inserted items

Post Reply
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

LuaInventory.insert/can_insert return inserted items

Post by Choumiko »

From the wiki https://forums.factorio.com/wiki/inde ... ork#insert
Returns Types/uint the count of items actually inserted
Having this for LuaInventory (and maybe even LuaPlayer) too would be really great, right now inserting more than 1 at a time is a bit tedious unless i'm missing something?

Code: Select all

local stack = {name=item.name, count=1}
local count = player.get_item_count(item.name)
local desired = requests[item.name] and requests[item.name] + item.count or item.count
local diff = count - desired
  if diff > 0 then
    local trash = player.get_inventory(defines.inventory.player_trash)
    for j=1,diff do
      if trash.can_insert(stack) then
        player.remove_item(stack)
        trash.insert(stack)
      end
    end
  end
could be

Code: Select all

local stack = {name=item.name, count=1}
local count = player.get_item_count(item.name)
local desired = requests[item.name] and requests[item.name] + item.count or item.count
local diff = count - desired
if diff > 0 then
  local trash = player.get_inventory(defines.inventory.player_trash)
  player.remove_item{name=item.name, count = trash.insert{name=item.name, count=diff}}
end
Also, having can_insert return the count of items that could be inserted would be nice

Post Reply

Return to “Implemented mod requests”