Page 1 of 1

question regarding copying items from inventory to container

Posted: Wed Aug 03, 2016 2:18 pm
by mexmer
i wanted to make copy of inventory to chest (something like deatchest does) and found strange issue.

since i wanted to make sure i can't go over containter capacity i used LuaInventory::insert(item) function
strangely it seems this function doesn't replicate item completely.

to be precise, let's say you have in your inventory tank, or buggy, or power armor - all these things have own inventories, but when you try to insert them to chest, you get "empty" copy instead.

therefore i checked difference and death chest use
LuaInventory[index]::set_stack(item) function and this one indeed properly works (eg. item is set to container, including it's own inventory)

so my question is rather to authors of API, does insert create new item from "template", instead of doing carbon copy (which i expect set_stack does, unless set_stack assigns reference)

i will give sample codes - player and savechest are obviously source and target entity, there is somewhat complex enumcode for that

this code copies inventory of vehicles, armors and so on (if you put them in your pocket)

Code: Select all

local playerinventory = player.get_inventory(defines.inventory.player_main)
local chestinventory = savechest.get_inventory(defines.inventory.chest)
local chestitems = 1
for j = 1, #playerinventory, 1 do
	if playerinventory[j].valid and playerinventory[j].valid_for_read then
		local item = playerinventory[j]

		local item = playerinventory[j]

		if chestinventory.can_insert(item) then
			chestitems = chestitems + 1
			chestinventory[chestitems].set_stack(item)
		end
	end
end
this code does not copy inventory of vehicles, armors and so on (if you put them in your pocket)

Code: Select all

local playerinventory = player.get_inventory(defines.inventory.player_main)
local chestinventory = savechest.get_inventory(defines.inventory.chest)
for j = 1, #playerinventory, 1 do
	if playerinventory[j].valid and playerinventory[j].valid_for_read then
		local item = playerinventory[j]

		local item = playerinventory[j]

		if chestinventory.can_insert(item) then
			chestinventory.insert(item)
		end
	end
end

Re: question regarding copying items from inventory to container

Posted: Thu Aug 04, 2016 11:37 pm
by Rseding91
inventory.insert makes a new item with the given name. set_stack clones the original item and makes an exact copy in the destination.

Re: question regarding copying items from inventory to container

Posted: Fri Aug 05, 2016 12:10 am
by mexmer
Rseding91 wrote:inventory.insert makes a new item with the given name. set_stack clones the original item and makes an exact copy in the destination.
thanks for confirmation