How to return an integer of empty slots from chest.

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 return an integer of empty slots from chest.

Post by 23johnw »

Hi very new to Factorio modding, in lua how can I return how many empty inventory slots are in a chest. Thanks
Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: How to return an integer of empty slots from chest.

Post by Alex33 »

Hi,
I'm new too, but I would try that:

Code: Select all

local nbrSlots=#chest.get_inventory(defines.inventory.chest)
local nbrEmptySlots=0
for i=1,nbrSlots,1 
do 
   if chest.get_inventory()[i]==nil then
	nbrEmptySlots=nbrEmptySlots+1
   end
end
https://lua-api.factorio.com/latest/Lua ... perator%20#
https://lua-api.factorio.com/latest/def ... tory.chest
Last edited by Alex33 on Mon Oct 28, 2019 8:55 pm, edited 1 time in total.
Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: How to return an integer of empty slots from chest.

Post by Alex33 »

to get the chest variable, there are some example functions in Factorio\data\base\lualib\

file "check.lua" :

Code: Select all

check.chests_emptied = function(chest_list, goal, update_quest_gui)
  if update_quest_gui == nil then
    update_quest_gui = true
  end
  goal = goal or #chest_list

  -- Check each entity by tag in the list
  local result = {total=0,current=0}
  for _, tag in ipairs(chest_list) do
    local chest = locations.get_surface_ent_from_tag(locations.get_main_surface(),tag)
    if chest then
      local inventory = chest.get_inventory(defines.inventory.chest)
      result.total = result.total + 1
      if inventory and inventory.is_empty() then
        result.current = result.current +1
      end
    end
  end

  if update_quest_gui then
    quest_gui.update_count('empty',result.current,goal)
  end

  return result.current >= goal
end
file "player_tracker.lua":

Code: Select all

tracker.check_chest_for_items = function(item_list,quant)
  local chests = locations.get_main_surface().find_entities_filtered({name='wooden-chest'})
  for _, chest in pairs(chests) do
    local inv = chest.get_inventory(defines.inventory.chest)
    for _, item_name in pairs(item_list) do
      if inv.get_item_count(item_name) > quant then
        print("found chest of iron")
        tracker.start_timer('full_item_chest')
        return chest
      end
    end
  end
  return nil
end
Post Reply

Return to “Modding help”