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

Place to get help with not working mods / modding interface.
23johnw
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sat Oct 08, 2016 5:35 pm
Contact:

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

Post 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.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

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

Post 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
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
23johnw
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sat Oct 08, 2016 5:35 pm
Contact:

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

Post by 23johnw »

Thank you people for your help
Post Reply

Return to “Modding help”