Page 1 of 1

Moving between inventories

Posted: Tue Sep 20, 2016 6:30 am
by Atria
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?

Re: Moving between inventories

Posted: Tue Sep 20, 2016 8:53 am
by Nexela
#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

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
#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.

Re: Moving between inventories

Posted: Tue Sep 20, 2016 9:28 am
by Atria
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

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
#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.
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.

Thank you so much for your help.

Re: Moving between inventories

Posted: Tue Sep 20, 2016 11:53 am
by aubergine18

Code: Select all

local copy = table.deepcopy( tbl )
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)

Re: Moving between inventories

Posted: Tue Sep 20, 2016 11:57 am
by Klonan
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

Posted: Tue Sep 20, 2016 12:14 pm
by Atria
aubergine18 wrote:

Code: Select all

local copy = table.deepcopy( tbl )
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)
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.

Maybe LuaItemStack constructor with set_stack idea by Klokan could work (that is if constructors are accesable from mods).

Re: Moving between inventories

Posted: Tue Sep 20, 2016 12:35 pm
by Atria
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).