Moving between inventories

Place to get help with not working mods / modding interface.
Atria
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sat Jul 09, 2016 10:25 am
Contact:

Moving between inventories

Post 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?
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Moving between inventories

Post 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.
Atria
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sat Jul 09, 2016 10:25 am
Contact:

Re: Moving between inventories

Post 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.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Moving between inventories

Post 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)
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.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5411
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Moving between inventories

Post 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
Atria
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sat Jul 09, 2016 10:25 am
Contact:

Re: Moving between inventories

Post 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).
Atria
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sat Jul 09, 2016 10:25 am
Contact:

Re: Moving between inventories

Post 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).
Post Reply

Return to “Modding help”