Page 1 of 1

Help with this code

Posted: Thu May 02, 2019 10:40 pm
by nosports
I want to filter some recipes, here for the recipe engine-unit.
And following all ohter recipes which uses the engine-unit.

For now i do only print to log, but if it will work i want to add something to the engine unit recipe and following all recipes also
But the code fails me, it even prints to log the copper-plate recipe :oops:
After much trying i don't have a grip what is the error in this

Here is my code for the moment :

Code: Select all

recipetodo = "engine-unit"	

if data.raw.recipe[recipetodo] then
	log("found " ..recipetodo.. " recipe")
end

for i, recipe in pairs (data.raw.recipe) do
	if data.raw["recipe"]  then
		for i, ingredient in pairs(data.raw["recipe"]) do
			if ingredient.name == recipetodo then
				log("found " ..ingredient.name.. " in recipe " ..recipe.name)
			end
                end
	end
end

Anyone can help me and show me my error in this.....
Thanks a lot

Re: Help with this code

Posted: Fri May 03, 2019 9:27 pm
by 321freddy
You are looping over all recipes twice instead of the actual ingredients. This is how a recipe is saved:

Code: Select all

   data.raw.recipe["engine-unit"] = {
      category = "advanced-crafting",
      enabled = false,
      energy_required = 10,
      ingredients = {
        {
          "steel-plate",
          1
        },
        {
          "iron-gear-wheel",
          1
        },
        {
          "pipe",
          2
        }
      },
      name = "engine-unit",
      result = "engine-unit",
      type = "recipe"
    },
So what you need to do is:

Code: Select all

recipetodo = "engine-unit"	

if data.raw.recipe[recipetodo] then
	log("found " ..recipetodo.. " recipe")
end

for i, recipe in pairs(data.raw.recipe) do
	for j, ingredient in pairs(recipe.ingredients) do
		if ingredient[1] == recipetodo then
			log("found " ..ingredient.name.. " in recipe " ..recipe.name)
		end
	end
end
(Also wrong forum I think)

Re: Help with this code

Posted: Fri May 03, 2019 10:25 pm
by AmatorPhasma
Not only:

a recipe can also look like:

Code: Select all

recipe.normal.ingredients=...
or

Code: Select all

recipe.normal.ingredients=..
recipe.expensive.ingredients=..
and the ingredients can also specified as:

Code: Select all

ingredients = {{type="item", name="engine-unit", amount=1}}
or a funny combination of both:

Code: Select all

ingredients = {
  {type="item", name="engine-unit", amount=1}, 
  {"steel-plate", 1}
}
I use this code to check for an ingredient: https://gitlab.com/AmatorPhasma/apm_fac ... e.lua#L100

Re: Help with this code

Posted: Fri May 03, 2019 10:50 pm
by nosports
Thanks, that was very helpfull..

Which forum would be then the correct ? :oops:

Re: Help with this code

Posted: Sat May 04, 2019 1:21 pm
by Therenas
viewforum.php?f=25 would be appropriate