Is it possible to move items from one inventory and then insert them into other inventory using mods?
Using Factorio API I can get all items in inventory as LuaItemStack using [] access operator of LuaInventory. Problem is inserting them into different inventory because LuaInventory.insert() takes SimpleItemStack and not LuaItemStack. Information like what equipment that is in Power armor or item durability would be lost.
Also I have problem because new inventory is created after old one is destroyed. If I simply save reference to LuaItemStack, destroying of inventory will invalidate these references. Is there way to hold LuaItemStack in aether while old inventory is destroyed without reference being invalidated?
Moving between inventories
Re: Moving between inventories
#1
get_contents() → dictionary string → uint Get counts of all items in this inventory. Return value The counts, indexed by item names.
simple item to item inventory transfer
#2 same principle just needs to be handled a little differently probably using equipment api to store all the values. If I get some time I will try and mock something for you.
#3 won't affect #1 as inv_to_transfer is not tied to any objects so you could save it for later use.
get_contents() → dictionary string → uint Get counts of all items in this inventory. Return value The counts, indexed by item names.
simple item to item inventory transfer
Code: Select all
inv_to_transfer = inv1.get_contents()
for name, count in pairs(inv_to_transfer) do
local notinserted = count - inv2.insert({name=name, count=count})
if count > 0 then
player.surface.spill_item_stack(player.position, {name=name, count=notinserted})
end
end
#3 won't affect #1 as inv_to_transfer is not tied to any objects so you could save it for later use.
Re: Moving between inventories
Yeah, I have something like this and I dont even have to check insert, because is always enough space. But the main problem is those non trivial items. I will probably need to deep clone LuaItemStack. I don't know how to do deep clone in Lua. Plus there probably will be some problem with C objects in the background. Then insert new item using LuaInventory.insert() and copy its non trivial properties from that clone.Nexela wrote:#1
get_contents() → dictionary string → uint Get counts of all items in this inventory. Return value The counts, indexed by item names.
simple item to item inventory transfer
#2 same principle just needs to be handled a little differently probably using equipment api to store all the values. If I get some time I will try and mock something for you.Code: Select all
inv_to_transfer = inv1.get_contents() for name, count in pairs(inv_to_transfer) do local notinserted = count - inv2.insert({name=name, count=count}) if count > 0 then player.surface.spill_item_stack(player.position, {name=name, count=notinserted}) end end
#3 won't affect #1 as inv_to_transfer is not tied to any objects so you could save it for later use.
Thank you so much for your help.
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Moving between inventories
Code: Select all
local copy = table.deepcopy( tbl )
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Re: Moving between inventories
Atria wrote: Using Factorio API I can get all items in inventory as LuaItemStack using [] access operator of LuaInventory. Problem is inserting them into different inventory because LuaInventory.insert() takes SimpleItemStack and not LuaItemStack. Information like what equipment that is in Power armor or item durability would be lost.
?
You can use set_stack instead of insert, which you use on a item stack in the target inventory, and it supports LuaItemStack and SimpleItemStack:
http://lua-api.factorio.com/latest/LuaI ... .set_stack
Re: Moving between inventories
Unfortunatly it doesn't work. Even when I do deep copy, when entity holding inventory is destroyed, underlaying C objects are destoryed and all references in lua are invalidated. Or I'm doing something wrong.aubergine18 wrote:If table.deepcopy isn't defined, you might need to `require "util"` (although I'm pretty sure util lib is included as standard since factorio 0.13)Code: Select all
local copy = table.deepcopy( tbl )
Maybe LuaItemStack constructor with set_stack idea by Klokan could work (that is if constructors are accesable from mods).
Re: Moving between inventories
Oh I just now realized I can create two entities on top of each other. So I don't have to destroy old entities before placing new one so with set_stack() everything is working.
Thank a lot to everybody who helped. I will post resulting mod in a minute (Merging Chests).
Thank a lot to everybody who helped. I will post resulting mod in a minute (Merging Chests).