Page 1 of 1

recipe values

Posted: Wed Jun 01, 2016 8:24 am
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.

Re: recipe values

Posted: Wed Jun 01, 2016 10:08 am
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}},
  },
}

Re: recipe values

Posted: Wed Jun 01, 2016 11:47 am
by bobingabout
I think you want to be using...
data.raw.recipe[recipename]

Re: recipe values

Posted: Wed Jun 01, 2016 12:00 pm
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.

Re: recipe values

Posted: Wed Jun 01, 2016 3:19 pm
by de_fire
ArderBlackard, the code you made, worked out perfect, thank you :D

Re: recipe values

Posted: Wed Jun 01, 2016 3:38 pm
by ArderBlackard
You are welcome! :)