Page 1 of 1

How to Spawn chest full of items at start of map.

Posted: Tue Oct 22, 2019 10:27 pm
by 23johnw
Hi, i am very new to this modding Factorio, what I would like to do is at the start of a game, spawn a chest full of items in front of me, rather than in my inventory. How do I go about creating the chest and then adding items to it? thank for reading.

Re: How to Spawn chest full of items at start of map.

Posted: Wed Oct 23, 2019 5:33 am
by darkfrei

Code: Select all

local chest_inventory = entity.get_inventory(1)
local item = "coal"
--  chest_inventory.clear()
	
  for s = 1, 2 do -- only two stacks is enough
    chest_inventory[s].set_stack{name = item, count = game.item_prototypes[item].stack_size}
  end

Re: How to Spawn chest full of items at start of map.

Posted: Wed Oct 23, 2019 2:04 pm
by eradicator
Here's a practical example that spawns a bunch of infinity chests containing every possible item:

Code: Select all

/c
local s = game.players[1].surface
local slots = game.entity_prototypes['infinity-chest'].get_inventory_size(defines.inventory.chest)
local rows = math.ceil(math.sqrt(#game.item_prototypes/slots))

local function new_chest(x,y) return s.create_entity{name='infinity-chest',position={x=x,y=y},force=game.players[1].force} end
local function allowed(item) if item.type ~= 'mining-tool' then return true end end

local i,chest = 0
for k,v in pairs(game.item_prototypes) do 
 i = i+1
 if i%slots == 1 then
  chest = new_chest((math.floor((i-1)/slots)) % rows + 1, math.floor(i/(rows*slots)))
  chest.remove_unfiltered_items = true
  end
 if allowed(v) then
  local slot_index = (i-1)%slots+1
  local filters = chest.infinity_container_filters
  filters[slot_index] = {name=v.name,count=v.stack_size,mode='exactly',index=slot_index}
  chest.infinity_container_filters = filters
  chest.get_inventory(defines.inventory.chest)[slot_index].set_stack(filters[slot_index])
  end
 end

Re: How to Spawn chest full of items at start of map.

Posted: Sat Oct 26, 2019 12:43 am
by 23johnw
Thank you people for your help