Page 1 of 1

How to remove or change recipe unlock in technology?

Posted: Thu Aug 03, 2023 8:16 pm
by Keysivi
Good afternoon

There is a code for adding unlocking a recipe when researching a technology:

Code: Select all

if data.raw["technology"]["military-4"] then
table.insert(data.raw["technology"]["military-4"].effects, {type = "unlock-recipe",recipe = "dragons-breath-shotgun-shell"})
end
But I don't have enough knowledge to prescribe a similar code to remove vanilla recipe unlock from researched technology? Or replacing the vanilla recipe with another?

Something like this:

Code: Select all

if data.raw["technology"]["military-4"] then
 table.remove( ..., {type = "unlock-recipe",recipe = "piercing-shotgun-shell"})
end
Or:

Code: Select all

if data.raw["technology"]["military-4"] then
 table.replace( ... , {type = "unlock-recipe",recipe = "piercing-shotgun-shell"} to {type = "unlock-recipe",recipe = "dragons-breath-shotgun-shell"}) 
 end
 
Sorry for the bad syntax...

I would be very grateful for your help!

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 7:48 am
by Pi-C
Keysivi wrote: Thu Aug 03, 2023 8:16 pm Good afternoon

There is a code for adding unlocking a recipe when researching a technology:

Code: Select all

if data.raw["technology"]["military-4"] then
table.insert(data.raw["technology"]["military-4"].effects, {type = "unlock-recipe",recipe = "dragons-breath-shotgun-shell"})
end
Basically, this code is the equivalent of

Code: Select all

if data.raw["technology"]["military-4"] then
    local last_entry = #data.raw["technology"]["military-4"].effects
    data.raw["technology"]["military-4"].effects[last_entry + 1] =  { type = "unlock-recipe",recipe = "dragons-breath-shotgun-shell" }
end
This means that data.raw["technology"]["military-4"].effects is an array where the elements are indexed by consecutive numbers (i.e. 1, 2, 3, …, last_entry). If given without a position parameter, table.insert(array, new_element) will attach new_element directly to the end of array.
But I don't have enough knowledge to prescribe a similar code to remove vanilla recipe unlock from researched technology? Or replacing the vanilla recipe with another?
In order to manipulate an element in such an array, you must know the element's position within the array. You won't know the position because even if you try to change the effects of a vanilla technology, there may be other mods that have added/removed/replaced effects of this technology before your mod could act. So in order to remove or replace an element in the array of tech effects, you must find its position:

Code: Select all

local tech = data.raw["technology"]["military-4"]
if tech then
  local position

  for e, effect in ipairs(tech.effects) do
    if effect.type == "unlock-recipe" and effect.recipe = "piercing-shotgun-shell" then
    	position = e
    	break
    end
  end    
  
  -- Code to remove or change the element goes here
end


Now that you've found the position of your unlock-recipe in the array of technology effects, you can remove the effect:

Code: Select all

 table.remove(tech.effects, position)
or replace it:

Code: Select all

  tech.effects[position] = {type = "unlock-recipe", recipe = "dragons-breath-shotgun-shell"}

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 4:31 pm
by Keysivi
Pi-C - thank you very much!

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 4:53 pm
by Keysivi
Unfortunately something is wrong with your code.

Shows a syntax error:
2.237 Mods to disable:Не удалось загрузить следующие моды: __My_add_pack__/prototype/dragons-breath-shotgun-pellet.lua:4: ..._/prototype/dragons-breath-shotgun-pellet/technology.lua:6: 'then' expected near '='
stack traceback:
[C]: in function 'require'
__My_add_pack__/prototype/dragons-breath-shotgun-pellet.lua:4: in main chunk
[C]: in function 'require'
__My_add_pack__/data.lua:18: in main chunk
lua:6

Code: Select all

     if effect.type == "unlock-recipe" and effect.recipe = "piercing-shotgun-shell" then

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 5:20 pm
by Keysivi
I seem to have found an error:

You have it written like this:

Code: Select all

if effect.type == "unlock-recipe" and effect.recipe = "piercing-shotgun-shell" then
And I added a second '='

Code: Select all

if effect.type == "unlock-recipe" and effect.recipe == "piercing-shotgun-shell" then
It seems to have worked.

Was it the right decision?

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 5:56 pm
by Pi-C
Keysivi wrote: Fri Aug 04, 2023 5:20 pm I seem to have found an error:

You have it written like this:

Code: Select all

if effect.type == "unlock-recipe" and effect.recipe = "piercing-shotgun-shell" then
And I added a second '='

Was it the right decision?
Sorry for the typo! You handled this correctly. In the if-clause of an if-then-statement, you only want comparisons (x == y), assignments (=) can only be used after "then".

Re: How to remove or change recipe unlock in technology?

Posted: Fri Aug 04, 2023 7:36 pm
by Keysivi
I understood you. Thanks a lot for the tips! You helped me a lot!