[SOLVED]replacing ingredients in recipes

Place to get help with not working mods / modding interface.
Villfuk02
Long Handed Inserter
Long Handed Inserter
Posts: 79
Joined: Mon Apr 30, 2018 7:23 am
Contact:

[SOLVED]replacing ingredients in recipes

Post by Villfuk02 »

I'm making my own modules to replace the vanilla ones, but because of compatibility i just removed the crafting recipes for the normal modules. Now i want to replace all "speed-module" in ingredients of all recipes with "module-B0" but I can't seem to find a way to do it.
Last edited by Villfuk02 on Sun May 13, 2018 8:52 am, edited 1 time in total.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: replacing ingredients in recipes

Post by eradicator »

Imho for compatibility it's best to just edit the original module properties/recipes. Recipe disabling is non-trivial especially if your mod is added/removed in an ongoing game (i.e. not newly started). But if you have some good reason to not want to you'll have to:

Loop through data.recipe
Loop through default/normal/expensive variants of ingredients subtable (not all of those are guaranteed to exist)
Loop through subtable to see if it has the old module and replace with new (string compare), be aware that ingredients table may have one of two different formats, either {type='item',name='thing',amount=1} or just {'thing',1}.
Villfuk02
Long Handed Inserter
Long Handed Inserter
Posts: 79
Joined: Mon Apr 30, 2018 7:23 am
Contact:

Re: replacing ingredients in recipes

Post by Villfuk02 »

After lots of trial and erro i created this:

Code: Select all

for i, r in pairs(data.raw.recipe) do
	if r.ingredients then
		for j, n in pairs(r.ingredients) do
			if n.name and string.find(n.name, "speed-module") then
				n.name = "module-B0"				
			end	
			if n[1] and string.find(n[1], "speed-module") then
				n[1] = "module-B0"
			end
			if n.name and string.find(n.name, "effectivity-module") then
				n.name = "module-G0"				
			end	
			if n[1] and string.find(n[1], "effectivity-module") then
				n[1] = "module-G0"
			end
			if n.name and string.find(n.name, "productivity-module") then
				n.name = "module-R0"				
			end	
			if n[1] and string.find(n[1], "productivity-module") then
				n[1] = "module-R0"
			end
		end
	end	
end
And it works!
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: replacing ingredients in recipes

Post by darkfrei »

Villfuk02 wrote:After lots of trial and erro i created this:
code
And it works!
How about normal and expensive recipes?
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [SOLVED]replacing ingredients in recipes

Post by eradicator »

There's a fair bit of redundancy in there which could be shortened by using a lookup table. I.e. something like

Code: Select all

--replacement definition and function
local replacement = {
  ['speed-module-1'] = "module-B1"
  ['speed-module-2'] = "module-B2"
  }
local function replace(module_name)
  --keeps the old name if no replacement was found
  return replacement[module_name] or module_name
  end
...further down

Code: Select all

  if n.name then
   n.name = replace(n.name)
  else
   n[1] = replace(n[1])
   end
Recipe difficulty should be hacked in too, something like:

Code: Select all

--for i, r in pairs(data.raw.recipe) do
--   if r.ingredients then
for _, x in pairs(data.raw.recipe) do
  for _, r in pairs{x, x.normal, x.expensive} do
   if r.ingredients then
     --....
Also your current version replaces all speed modules (i.e. 1,2,3,etc) with the same module, is that on purpose?
Post Reply

Return to “Modding help”