What I also wanted to say is that this mod can be shortened considerably, no need for extra text in the init.lua or all those textures.
I'll try to explain it here, it shouldn't be too hard to understand if i make it step by step.
Firstly all of your nodes are registered by the same way, every node has exactly the same definition.
- Code: Select all
minetest.register_node("btw:red", {
description = "RED BLOCK",
tiles = {"red.png"},
is_ground_content = true,
groups = {cracky=3},
sounds = default.node_sound_glass_defaults(),
light_source = 6,
paramtype = "light",
use_texture_alpha = false,
drawtype = "glasslike",
sunlight_propagates = true,
})
Notice how e.g. group is always cracky=3, sounds always glass etc.
To shorten this you could use a for loop, repeating this same part of the code, just every time with different variables.
To do that we first need a table of our desired colours.
- Code: Select all
local colors = {
{"red", "Red", "FF0000"},
{"orange", "Orange", "FF8000"},
{"yellow", "Yellow", "FFFF00"},
{"green", "Green", "00FF00"},
{"darkgreen", "Dark Green", "008000"},
{"blue", "Blue", "0000FF"},
{"violet", "Violet", "8000FF"},
{"pink", "Pink", "FF00FF"},
{"magenta", "Magenta", "FF0080"},
{"brown", "Brown", "400C00"},
{"white", "White", "FFFFFF"},
{"grey", "Grey", "808080"},
{"darkgrey", "Dark Grey", "404040"},
{"black", "Black", "000000"},
}
This is one of the ways to do it, basically first variable in every row is "coding name", the we'll use it to create nodes coding name (e.g. "default:sandstone" is a coding name).
The second one in the row is always the so called "real name", basically same thing, just starts with a capital letter since we use this one to create node's description.
Third one might be harder to understand, it is hex code of a colour. Every colour has a hex code (the amount of red, green and blue colour in the colour in hexadecimal system). Since we don't use hexadecimal system in real life too much you probably didn't know of it, you don't need to since you can easily convert every colour to it
here.
Now we need to implement this table.
Firstly this table need to be in the same file and above the for loop, which we are now creating.
To start the for loop type the line
- Code: Select all
for _, rowc in ipairs(colors) do
You'll need to define which column of the table does what, like this:
- Code: Select all
local coding_name = rowc[1]
local real_name = rowc[2]
local hex_color = rowc[3]
And now that we have defined the columns, we can use these variables in the node definition.
To use the certain variable in the node definition, first we need to check which variable does what.
As I said before, first column consists of coding names, we'll use this one to create node's coding name.
- Code: Select all
minetest.register_node("btw:" .. coding_name,
This will give us output such as "btw:red" for the red colour for example.
But this is only the first variable, we will also use real name to create descriptions.
- Code: Select all
description = real_name .. " Node",
You can make the description to your liking, this one makes red node's description be "Red Node".
Also notice that i created an intentional space before the "Node" part, or the name would be all in one word.
The third variable is our colour, this is why registering like this is much easier.
Before we start this one, you'll need a texture of whichever size you prefer, coloured into gray colour.
e.g. btw_gray.png will be used in this example.
- Code: Select all
tiles = {"btw_gray.png^[colorize:#" .. hex_color .. "80"},
Here you can see that I colorized the gray texture into specific colour which is defined by the hex_color variable.
You can find out more about texture modifiers
here.
The number 80 in the last part of this table is the intensity of the colour, the alpha channel.
You can also call it saturnation in this case, cause we are using the gray texture as the base.
Now, we have all the variables inserted in the code, it should look like this:
- Code: Select all
for _, rowc in ipairs(colours) do
local coding_name = rowc[1]
local real_name = rowc[2]
local hex_color = rowc[3]
minetest.register_node("btw:" .. coding_name, {
description = real_name .. " Block",
tiles = {"btw_gray.png^[colorize:#" .. hex_color .. "80"},
is_ground_content = true,
groups = {cracky=3},
sounds = default.node_sound_glass_defaults(),
light_source = 6,
paramtype = "light",
use_texture_alpha = false,
drawtype = "glasslike",
sunlight_propagates = true,
})
end
This is how you make the node definitions, but you'll also want to register crafting recipes.
This is easy now.
Now you can use these variables in any part of the code you want that is
inside the for loop, before that "end" that is.
So, registering cooking recipes? Like this:
- Code: Select all
minetest.register_craft({
type = "cooking",
recipe = "wool:" .. coding_name,
output = "btw:" .. coding_name,
})
And place that before the end, i say this again since this part is useless if placed anywhere else.
Now, this will register cooking recipes for e.g. red nodes, but if you make a colour that doesn't exist in wool mod, you won't get a cooking recipe.
You'll also require the opposite recipe if you want nodes back to wool.
Now that you have the basic understanding of how for loop works, you can experiment a bit, that is why I didn't make the rest of the code for you.
I shall also mention the use of the if/else loop.
For example if you need all of the nodes except for the gray one colorized (cause it is gray already), you'll exclude the gray node from colorizing.
- Code: Select all
if hex_color == "808080" then
(definition of the gray node)
end
This should be placed at the beginning of the for loop, also leading to the node definition with "else".
- Code: Select all
else
(the definition of the rest of the nodes)
end
This way the gray nodes will not be colorized, but the rest will.
If/else loops can also be applied when making crafting recipes, cooking recipes and whatever rest you might think of.