question regarding copying items from inventory to container

Place to get help with not working mods / modding interface.
User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 870
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

question regarding copying items from inventory to container

Post 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

Rseding91
Factorio Staff
Factorio Staff
Posts: 14107
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: question regarding copying items from inventory to container

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 870
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

Re: question regarding copying items from inventory to container

Post 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

Post Reply

Return to “Modding help”