Help with this code

Place to get help with not working mods / modding interface.
nosports
Filter Inserter
Filter Inserter
Posts: 274
Joined: Fri Jan 19, 2018 5:44 pm
Contact:

Help with this code

Post 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
321freddy
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Sep 23, 2016 10:16 pm
Contact:

Re: Help with this code

Post 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)
User avatar
AmatorPhasma
Fast Inserter
Fast Inserter
Posts: 126
Joined: Sat Aug 05, 2017 8:20 pm
Contact:

Re: Help with this code

Post 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
nosports
Filter Inserter
Filter Inserter
Posts: 274
Joined: Fri Jan 19, 2018 5:44 pm
Contact:

Re: Help with this code

Post by nosports »

Thanks, that was very helpfull..

Which forum would be then the correct ? :oops:
User avatar
Therenas
Factorio Staff
Factorio Staff
Posts: 249
Joined: Tue Dec 11, 2018 2:10 pm
Contact:

Re: Help with this code

Post by Therenas »

viewforum.php?f=25 would be appropriate
Post Reply

Return to “Modding help”