Page 1 of 1

How to return an integer of empty slots from chest.

Posted: Sat Oct 26, 2019 12:48 am
by 23johnw
Hi very new to Factorio modding, in lua how can I return how many empty inventory slots are in a chest. Thanks

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

Posted: Sat Oct 26, 2019 1:35 am
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

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

Posted: Sat Oct 26, 2019 1:49 am
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