Page 1 of 1

Accessing data from control.lua

Posted: Sun May 24, 2020 6:20 am
by sherlockholmes221b
Hi, I work on mod with scenario. The main idea of the mod is creating a factory producing food and serving it. I have an entity "orderer", which should choose randomly a recipe, set it to itself and if it got what it wanted, choose next recipe and so on. The problem is, I can't access game data to randomly choose a recipe. Is there any way of getting recipes available for LuaRecipeCategory or LuaEntity?

Re: Accessing data from control.lua

Posted: Sun May 24, 2020 7:16 am
by DaveMcW
Recipe category:

Code: Select all

local category = "smelting"
local recipes = {}
for _, recipe in pairs(game.recipe_prototypes) do
  if recipe.category == category then
    table.insert(recipes, recipe.name)
  end
end
Entity:

Code: Select all

local entity = game.surfaces[1].find_entities_filtered{type="furnace", limit=1}[1]
local categories = entity.prototype.crafting_categories
local recipes = {}
for _, recipe in pairs(game.recipe_prototypes) do
  for category, _ in pairs(categories) do
    if recipe.category == category then
      table.insert(recipes, recipe.name)
    end
  end
end