Page 1 of 1

How to insert all the items in several chests?

Posted: Tue Mar 27, 2018 4:16 pm
by WIZ4
I can not insert on all items in chests. Items that do not fit in the chest, disappear.How to solve this problem?

Code: Select all

  global.items = {}
  global.items.items_list =
  {
	defence =
    {
      ["stone-wall"] = 1000,
      ["gun-turret"] = 500,
      ["uranium-rounds-magazine"] = 1000
    }}

Code: Select all

function advanced_chest()
local chest = game.surfaces.nauvis.create_entity{name = "wooden-chest", position= {game.player.position.x, game.player.position.y+1},force = game.player.force}
for k,list in pairs(global.items.items_list.defence) do
	if chest.can_insert{name=k, count=list} == false then 
	chest = game.surfaces.nauvis.create_entity{name = "wooden-chest", position= {chest.position.x, chest.position.y+1},force = game.player.force}
	end
	chest.insert{name=k, count=list}	
end
end	
Screenshot_8.png
Screenshot_8.png (230.55 KiB) Viewed 1214 times
Screenshot_9.png
Screenshot_9.png (242.68 KiB) Viewed 1214 times

Re: How to insert all the items in several chests?

Posted: Tue Mar 27, 2018 7:29 pm
by eradicator
http://lua-api.factorio.com/latest/LuaI ... ory.insert
Return value
Number of items actually inserted.
So just substract that number from the list you have. And loop over it for every chest. At the end all the entries in the list should end up being == 0.