LuaInventory.insert/can_insert return inserted items
Posted: Wed Sep 09, 2015 7:43 pm
From the wiki https://forums.factorio.com/wiki/inde ... ork#insert
could be
Also, having can_insert return the count of items that could be inserted would be nice
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?Returns Types/uint the count of items actually inserted
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
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