[SOLVED] Recipes Not Registering Properly with For Loop

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

[SOLVED] Recipes Not Registering Properly with For Loop

by octacian » Tue Apr 19, 2016 03:46

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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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.
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 

User avatar
Naj
Member
 
Posts: 170
Joined: Sat Sep 19, 2015 21:14
GitHub: pyrollo
In-game: naj

Re: Recipes Not Registering Properly with For Loop

by Naj » Tue Apr 19, 2016 09:21

I dont know if it is the cause but I would call register_node before register_craft.

And for the texture, I guess it is better to prefix png file names with mod name.
 

User avatar
Naj
Member
 
Posts: 170
Joined: Sat Sep 19, 2015 21:14
GitHub: pyrollo
In-game: naj

Re: Recipes Not Registering Properly with For Loop

by Naj » Tue Apr 19, 2016 09:27

And I don't understand why you use nested loops for dyes and hues. The code in nested loops will be run for each 144 combination of hues and dyes (Black/black, Black/blue, Black/brown and so on).

May be try that instead :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- For to Register Stained Glass
for h = 1, 12 do
    hues = HUES[h]
    dyes = DYES[h]

    -- 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()
    })

    -- 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"}
      }
    })
end
 

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 » Tue Apr 19, 2016 12:49

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):

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
Naj
Member
 
Posts: 170
Joined: Sat Sep 19, 2015 21:14
GitHub: pyrollo
In-game: naj

Re: Recipes Not Registering Properly with For Loop

by Naj » Tue Apr 19, 2016 13:42

That's even better :)

I'd change also node names, replacing :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
"genesis:stained_glass_" .. (i)

with:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
"genesis:stained_glass_" .. glass.dye


so, instead of genesis:stained_glass_1, genesis:stained_glass_2, ... you get genesis:stained_glass_black, genesis:stained_glass_blue... which, IMO is better.
 

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

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

by octacian » Fri Apr 22, 2016 01:19

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

Single Table Specifying hue and dye:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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!
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 10 guests