[SOLVED] Recipes Not Registering Properly with For Loop

Post Reply
User avatar
octacian
Member
Posts: 597
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian
Location: Canada

[SOLVED] Recipes Not Registering Properly with For Loop

by octacian » Post

I am currentlt working on a mod called genesis, which includes 48 different types of glass. To register the different glass, I have a series of 4 for loops each registering 12 glass. (There are 4 different types of glass, each with 12 colours). I also have two tables, one for the different hues, and one for the dyes from the default dyes mod. The only difference between the entries in each table, is that all entries in the dyes table are lowercase.

When trying to register the recipe using a nested for loop, I find that no matter the colour of wool I use, I am given White Glass. Here is the code for my tables and one for loop (the one in which I am testing the recipe loop):

Tables:

Code: Select all

-- HUES: FILES / NAMES
HUES = {
  "Black",
  "Blue",
  "Brown",
  "Cyan",
  "Green",
  "Magenta",
  "Orange",
  "Pink",
  "Red",
  "Voilet",
  "Yellow",
  "White"
}

-- DYES: RECIPE DYE
DYES = {
  "black",
  "blue",
  "brown",
  "cyan",
  "green",
  "magenta",
  "orange",
  "pink",
  "red",
  "voilet",
  "yellow",
  "white"
}
For Loop:

Code: Select all

-- For to Register Stained Glass
for h = 1, 12 do
  hues = HUES[h]

  for d = 1, 12 do
    dyes = DYES[d]

    -- Recipe Register
    minetest.register_craft({
      output = "genesis:stained_glass_" .. (h),
      recipe = {
        {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"},
        {"genesis:glass_molten", "dye:" .. dyes, "genesis:glass_molten"},
        {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"}
      }
    })

    -- Stained Glass Node Register
    minetest.register_node("genesis:stained_glass_" .. (h), {
      description = hues .. " Stained Glass",
      drawtype = "glasslike_framed_optional",

      tiles = {hues .. "_Glass.png", hues .. "_Glass_detail.png"},
      inventory_image = minetest.inventorycube(hues .. "_Glass.png"),

      paramtype = "light",
      sunlight_propagates = true,
      is_ground_content = false,
      use_texture_alpha = true,

      groups = {cracky = 3, oddly_breakable_by_hand = 3},
      sounds = default.node_sound_glass_defaults()
    })
  end
end
Thanks, any help would be greatly appreciated. I have tried for over an hour, and this is where I am.
Last edited by octacian on Fri Apr 22, 2016 01:16, edited 1 time in total.
MicroExpansion, Working Computers, All Projects - Check out my YouTube channel! (octacian)
I'm currently inactive in the Minetest community! So if I don't respond, that's why.

User avatar
oleastre
Member
Posts: 81
Joined: Wed Aug 13, 2014 21:39
GitHub: oleastre
In-game: oleastre

Re: Recipes Not Registering Properly with For Loop

by oleastre » Post

As Naj said, your current code loop both on hues and dyes and since the recipe does not depend on the hue, you register 12 times the same recipe; and at the end you get is 12 different recipes to generate white glass.

To make it a more simple:
- I would first register the node, then the craft recipe (maybe it does not change anything).
- I would use local variables when possible.
- I'd prefer to use the lua ipairs function instead of writing a hard coded numeric for loop and assigning the hue and dye manually.

Should be something like (completely untested):

Code: Select all

local STAINED_GLASS = {
  { hue = "Black",   dye = "black" },
  { hue = "Blue",    dye = "blue" },
  { hue = "Brown",   dye = "brown" },
  { hue = "Cyan",    dye = "cyan" },
  { hue = "Green",   dye = "green" },
  { hue = "Magenta", dye = "magenta" },
  { hue = "Orange",  dye = "orange" },
  { hue = "Pink",    dye = "pink" },
  { hue = "Red",     dye = "red" },
  { hue = "Violet",  dye = "violet" },
  { hue = "Yellow",  dye = "yellow" },
  { hue = "White",   dye = "white" }
}

-- For to Register Stained Glass
for i, glass in ipairs(STAINED_GLASS) do
  -- Stained Glass Node Register
  minetest.register_node("genesis:stained_glass_" .. (i), {
    description = glass.hue .. " Stained Glass",
    drawtype = "glasslike_framed_optional",

    tiles = {glass.hue .. "_Glass.png", glass.hue .. "_Glass_detail.png"},
    inventory_image = minetest.inventorycube(glass.hue) .. "_Glass.png"),

    paramtype = "light",
    sunlight_propagates = true,
    is_ground_content = false,
    use_texture_alpha = true,

    groups = {cracky = 3, oddly_breakable_by_hand = 3},
    sounds = default.node_sound_glass_defaults()
  })

  -- Recipe Register
  minetest.register_craft({
    output = "genesis:stained_glass_" .. (i),
    recipe = {
      {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"},
      {"genesis:glass_molten", "dye:" .. glass.dye, "genesis:glass_molten"},
      {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"}
    }
  })
end

User avatar
octacian
Member
Posts: 597
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian
Location: Canada

Re: [SOLVED] Recipes Not Registering Properly with For Loop

by octacian » Post

Thanks for all your help. With this information put together, here is what I did.

Single Table Specifying hue and dye:

Code: Select all

local GLASS_COLOURS = {
  { hue = "Black",   dye = "black" },
  { hue = "Blue",    dye = "blue" },
  { hue = "Brown",   dye = "brown" },
  { hue = "Cyan",    dye = "cyan" },
  { hue = "Green",   dye = "green" },
  { hue = "Magenta", dye = "magenta" },
  { hue = "Orange",  dye = "orange" },
  { hue = "Pink",    dye = "pink" },
  { hue = "Red",     dye = "red" },
  { hue = "Violet",  dye = "violet" },
  { hue = "Yellow",  dye = "yellow" },
  { hue = "White",   dye = "white" }
}
For Loop:

Code: Select all

-- For to Register Stained Glass
for i, glass in ipairs(GLASS_COLOURS) do
  -- Stained Glass Node Register
  minetest.register_node("genesis:stained_glass_" .. glass.dye, {
    description = glass.hue .. " Stained Glass",
    drawtype = "glasslike_framed_optional",

    tiles = {glass.hue .. "_Glass.png", glass.hue .. "_Glass_detail.png"},
    inventory_image = minetest.inventorycube(glass.hue .. "_Glass.png"),

    paramtype = "light",
    sunlight_propagates = true,
    is_ground_content = false,
    use_texture_alpha = true,

    groups = {cracky = 3, oddly_breakable_by_hand = 3},
    sounds = default.node_sound_glass_defaults()
  })

  -- Recipe Register
  minetest.register_craft({
    output = "genesis:stained_glass_" .. glass.dye,
    recipe = {
      {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"},
      {"genesis:glass_molten", "dye:" .. glass.dye, "genesis:glass_molten"},
      {"genesis:glass_molten", "genesis:glass_molten", "genesis:glass_molten"}
    }
  })
end
With all this put together, I not only get everything functioning in less code, but the item strings are genesis:stained_glass_black and so on.

Thnx again!
MicroExpansion, Working Computers, All Projects - Check out my YouTube channel! (octacian)
I'm currently inactive in the Minetest community! So if I don't respond, that's why.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests