Page 1 of 1

Copying contents of chest

Posted: Tue Mar 08, 2016 9:05 pm
by daniel34
I'm trying to copy the contents of a steel chest to another steel chest.

I used get_contents() on the inventory of the source chest and insert(item) on the inventory of the target chest, which worked. This approach copies the chest item-by-item, meaning it "sorts" the contents into the target chest.

I would like to keep the order of item stacks in the chest. so I tried using the [] operator to access the slots directly and copy the chest slot-by-slot but this fails, although the [] operator is marked Read-Write in the API documentation.

Getting the contents of slot 1

Code: Select all

/c game.player.print(game.player.selected.get_inventory(defines.inventory.chest)[1].name.." "..game.player.selected.get_inventory(defines.inventory.chest)[1].count)
gives me as output: iron-plate 58

but setting the contents of slot 1 on the same chest

Code: Select all

/c game.player.selected.get_inventory(defines.inventory.chest)[1] = {name="steel-plate", count=10}
gives me this error:
Cannot execute command. Error: [string "game.player.selected.get_inventory(defines.in..."]:1: LuaInventory doesn't contain key 1.

What am I doing wrong here?

Re: Copying contents of chest

Posted: Tue Mar 08, 2016 9:22 pm
by Klonan
daniel34 wrote:I'm trying to copy the contents of a steel chest to another steel chest.

I used get_contents() on the inventory of the source chest and insert(item) on the inventory of the target chest, which worked. This approach copies the chest item-by-item, meaning it "sorts" the contents into the target chest.

I would like to keep the order of item stacks in the chest. so I tried using the [] operator to access the slots directly and copy the chest slot-by-slot but this fails, although the [] operator is marked Read-Write in the API documentation.

Getting the contents of slot 1

Code: Select all

/c game.player.print(game.player.selected.get_inventory(defines.inventory.chest)[1].name.." "..game.player.selected.get_inventory(defines.inventory.chest)[1].count)
gives me as output: iron-plate 58

but setting the contents of slot 1 on the same chest

Code: Select all

/c game.player.selected.get_inventory(defines.inventory.chest)[1] = {name="steel-plate", count=10}
gives me this error:
Cannot execute command. Error: [string "game.player.selected.get_inventory(defines.in..."]:1: LuaInventory doesn't contain key 1.

What am I doing wrong here?
I don't think you can write inventories like that,
To set the contents you'll need to use the insert() method to put things back in.

Im not 100% sure, but you might be able to do something like ths

Code: Select all

function copy_chest(original, copy)
  for item_name, item_count  in pairs (original.get_inventory(1)) do
    copy.insert(name = item_name, count = item_count)
  end
end

Im not really sure though, but im pretty sure the only way you can add or remove items from inventories is with the insert, remove or clear methods

EDIT:

if you want to do it with console commands you could define things using your cursor in the global.table

Code: Select all

/c global.orginal = game.local_player.selected
/c global.copy = game.local_player.selected 
    for item_name, item_count  in pairs (global.original.get_inventory(1)) do
      global.copy.insert(name = item_name, count = item_count)
    end


Re: Copying contents of chest

Posted: Tue Mar 08, 2016 9:43 pm
by daniel34
Like I said, my goal is to have a slot-by-slot copy of the inventory, to be done in control.lua for a mod, not on the console. That was just to test if I could reproduce it on the console.

Code: Select all

	-- copying with insert()
	local contents = inv.get_contents()
	for item, amount in pairs(contents) do
		entity.get_inventory(defines.inventory.chest).insert{name = item, count = amount}
	end

	-- copying slot-by-slot
	for i = 1, #inv do
		entity.get_inventory(defines.inventory.chest)[i] = inv[i]
	end
The code at the top actually does what you suggest, that I had already working.
The code at the bottom shows what I am trying to achieve.

What you are saying is that it is not possible with the Lua API to fill a chest like that when the stack size is 100:
Image
because this is not possible using only insert() and remove().
EDIT: or insert items into the red slots of a chest limited with X.

Re: Copying contents of chest

Posted: Tue Mar 08, 2016 11:01 pm
by Klonan
You can use set_stack(),

I haven't used it before, but something like this should work,

Code: Select all

   local contents = inv.get_contents()
     local s = 1
   for item, amount in pairs(contents) do
      entity.get_inventory(defines.inventory[s].set_stack(name = item, count = amount)
      s = s+1
   end
not sure if thats the best way, but the set_stack should do what you're asking

Re: Copying contents of chest

Posted: Tue Mar 08, 2016 11:08 pm
by daniel34
Klonan wrote:You can use set_stack(),
I haven't used it before, but something like this should work,
not sure if thats the best way, but the set_stack should do what you're asking
Thank you, that was exactly what I was looking for. Working without problems.
Although shouldn't the operator [] for LuaInventory then be marked Read-Only, as you can't actually write to it?

EDIT: here is the working code, in case someone else needs it:
inv - the old inventory (sourceChest.get_inventory(defines.inventory.chest))
entity - the entity to fill

Code: Select all

	for i = 1, #inv do
		entity.get_inventory(defines.inventory.chest)[i].set_stack(inv[i]);
	end

Re: Copying contents of chest

Posted: Tue Mar 08, 2016 11:20 pm
by Klonan
daniel34 wrote:
Thank you, that was exactly what I was looking for. Working without problems.
Although shouldn't the operator [] for LuaInventory then be marked Read-Only, as you can't actually write to it?
On the wiki its correct saying 'read only'
Image

So i guess the API docs are just slightly incorrect