Technology research disables a recipe

Place to get help with not working mods / modding interface.
Post Reply
User avatar
DiegoPro77
Fast Inserter
Fast Inserter
Posts: 169
Joined: Tue Feb 20, 2018 1:40 pm
Contact:

Technology research disables a recipe

Post by DiegoPro77 »

Hi guys, I have a question.
I've been absent from the game and therefore from programming Lua for quite a period, and I've never been good with this language, so I'd like to know if you're kind enough to tell me if there's a way to do a thing like this.
(Maybe it's faster with an example.)

Code: Select all

		{
		  type = "recipe",
		  name = "iron-ingot-v1",
		  category = "smelting",
		  energy_required = 3.2,
		  ingredients =
		  {
			{ "iron-mineral", 1}
		  },
		  result ="iron-ingot"
		}
This is the first recipe, enabled from the beginning of the game let's suppose.
Now i research this new recipe with the "steel-processing" technology:

Code: Select all

		{
		  type = "recipe",
		  name = "iron-ingot-v2",
		  category = "smelting",
		  energy_required = 3.2,
		  enabled=false,
		  ingredients =
		  {
			{ "iron-mineral", 1}
		  },
		  result ="iron-ingot",
		  result_count=2
		}
When i research steel processing can I disable the first recipe, so that I don't see 10 different iron ingot recipes in the crafting interface? :?
(And, but maybe this is too much, if an assembling machine runs the first recipe - not this obv its category is smelting - is there a way to instantly change it to do the new recipe or will I have to do it manually in-game?)
Aka Playmaker. My mods: https://mods.factorio.com/user/77playmaker -|- My Discord Server: https://discord.gg/6NGYQdX
"One day you'll live this word behind, so live a life you will remember."
Expanding your game experience since 2018.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1659
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Technology research disables a recipe

Post by Pi-C »

DiegoPro77 wrote:
Sat May 27, 2023 1:56 pm
When i research steel processing can I disable the first recipe, so that I don't see 10 different iron ingot recipes in the crafting interface? :?
This would require control scripting. You want to listen to the on_research_finished event, which will give you event.research (a LuaTechnology), from which you can get the effects of the research as well as the force that researched the tech. If the research unlocks your second recipe, set force.recipes[first_recipe].enabled = false to hide it from the GUIs of all players on that force.
(And, but maybe this is too much, if an assembling machine runs the first recipe - not this obv its category is smelting - is there a way to instantly change it to do the new recipe or will I have to do it manually in-game?)
You've still got the force that researched the tech:

Code: Select all

local furnaces, recipe
for s, surface in pairs(game.surfaces) do
  furnaces = surface.find_entities_filtered{
    type = "furnace", 
    force = event.research.force}
   
  for f, furnace in ipairs(furnaces) do
    recipe = furnace.get_recipe()
    if recipe and recipe.name == old_recipe.name then
      furnace.set_recipe(new_recipe)
    end
  end
end
You can skip searching the surfaces if you also listen to on_built_entity, on_robot_built_entity, and script_raised_built to store all buildings that can craft your recipe in your global table when they are built, and listen to on_player_minded_entity, on_robot_mined_entity, on_entity_died, on_entity_destroyed, and script_raised_destroy to clean up your list.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1659
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Technology research disables a recipe

Post by Pi-C »

Wait, I've made a mistake! Both "assembling-machine" and "furnace" are Prototype/CraftingMachine, so get_recipe() will work for both. But set_recipe() only works for "assembling-machine", not for furnaces. Replacing furnaces that use the old recipe should work, however:

Code: Select all

local furnaces, furnace, recipe
local remove = {}

for s, surface in pairs(game.surfaces) do
  furnaces = surface.find_entities_filtered{
    type = "furnace", 
    force = event.research.force}
   
  for f = #furnaces, 1, -1 do
    furnace = furnaces[f]
    recipe = furnace.get_recipe()
    if recipe and recipe.name == old_recipe.name then
      furnace.clone{position = furnace.position, surface = surface, force = furnace.force}
      furnace.destroy({raise_destroy=true})
    end
  end
end
EDIT: Added raise_destroy flag to furnace.destroy(), so other mods can update their data.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DiegoPro77
Fast Inserter
Fast Inserter
Posts: 169
Joined: Tue Feb 20, 2018 1:40 pm
Contact:

Re: Technology research disables a recipe

Post by DiegoPro77 »

Hi!
Thank you for the codes and the explanations: they're all really appreciated.
I succeded in disabling some "deprecated" recipes the mod I'm making, and this will become really handly in the future, since I inted to modify and enhance many recipes in the making.

When I'll implement the second piece of code I'll reply if something goes wrong, but for now everything is ok here. :D
Aka Playmaker. My mods: https://mods.factorio.com/user/77playmaker -|- My Discord Server: https://discord.gg/6NGYQdX
"One day you'll live this word behind, so live a life you will remember."
Expanding your game experience since 2018.

Post Reply

Return to “Modding help”