recipe values

Place to get help with not working mods / modding interface.
Post Reply
User avatar
de_fire
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Jun 01, 2016 8:06 am
Contact:

recipe values

Post by de_fire »

I have tried to make a function that set some default values for some recipes, but it doesn't work. My guess is im doing something wrong, or it's something that cant be done the way i think.
Hope someone in here can help me solve the problem, if there is a way to do it.

my function:

function recValues(recipename)
recipe[recipename].icon = "__MyMod__/graphics/icon/recycling-progress.png"
recipe[recipename].hidden = "true"
recipe[recipename].category = "smeltning",
recipe[recipename].energy_required = 5,
end

The recipe:

{
type = "recipe",
name = "rec-iron-chest",
ingredients = {{"iron-chest", 1}},
results =
{{"iron-plate", 4}},
recValues("rec-iron-chest"),
},

The idea is to make a function that fill in the spots that won't change for all the recipes, im gonna make, spare a lot of lines of code.

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: recipe values

Post by ArderBlackard »

Actually your function returns nothing so it's call results to 'nil' value. As a consequence you are inserting 'nil' value into the table which is the same as not inserting at all.
You may consider using the code like this:

Code: Select all

function recValues( prototype ) 
  prototype.icon = "__MyMod__/graphics/icon/recycling-progress.png"
  prototype.hidden = "true" 
  prototype.category = "smelting"
  prototype.energy_required = 5
  return prototype
end

data:extend{
  recValues( {
    type = "recipe",
    name = "rec-iron-chest",
    ingredients = {{"iron-chest", 1}},
    results = {{"iron-plate", 4}},
  } ),
}
As a result we create a table (as a parameter of the recValues function inside the data:extend call), fill it with some unique data, then pass it inside the recValues function where it receives the common values and then is returned and added to the data:extend parameter table.

Also Lua allows you to omit method call parentheses if there is only one parameter and it is a string or a table, so you can remove them from the recValues call to make it a bit clearer:

Code: Select all

data:extend{
  recValues {
    type = "recipe",
    name = "rec-iron-chest",
    ingredients = {{"iron-chest", 1}},
    results = {{"iron-plate", 4}},
  },
  recValues {
    type = "recipe",
    name = "rec-iron-chest-1",
    ingredients = {{"iron-chest", 2}},
    results = {{"iron-plate", 5}},
  },
}
Gib dich hin bis du Glück bist

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: recipe values

Post by bobingabout »

I think you want to be using...
data.raw.recipe[recipename]
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: recipe values

Post by ArderBlackard »

bobingabout wrote:I think you want to be using...
data.raw.recipe[recipename]
Anyway in the provided code the function is called from the place where the recipe is not yet added to the data.raw table.
Gib dich hin bis du Glück bist

User avatar
de_fire
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Jun 01, 2016 8:06 am
Contact:

Re: recipe values

Post by de_fire »

ArderBlackard, the code you made, worked out perfect, thank you :D

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: recipe values

Post by ArderBlackard »

You are welcome! :)
Gib dich hin bis du Glück bist

Post Reply

Return to “Modding help”