If you did your homework you should have a mod that registers four new nodes, along with recipes for them. In this video I'll be demonstrating a better way to register multiple nodes that all only have slight differences.
Consider the four nodes we created, the only real differences are the texture, name, and description, they all have the same groups, and all drop dirt. We can simplify by creating a table that holds those differences and pull that data and use it to bulk register the nodes with a looping system.
Before we go on a quick explanation of tables. In lua a table is a data structure that can hold many different types of data; strings, numbers, or even other tables. To create a table we just put something between an opening and closing curly brace "{}". To use the data we also need a variable that points to the table, such as "local a = {}".
Lua variable are symbolic names that point to some data. This data can be read, written and modified. The names can be a mix of letters, numbers, and underscores. They are case sensitive, so mydata would be different from MyData, and MYDATA. Variables can also be local or global. For the time being we are going to make all our variables local. We'll talk more about this in a later lesson.
Let's make the table and populate it with our data.
The table needs a name, let's call it grass_table. I like to replace spaces with underscores, but some people use CamelCase where you capitalize the first letter of each word in multi-word variables. It doesn't really matter what you choose to use, just stay consistent with it. Use care to not make variable names too long, as you have to type them out, and longer names are harder to remember and easier to mistype, on the flip side don't make them too short or ambiguous that you can't remember what data they reference.
Back to our code. We need to populate this table, we've already discussed how we only have three differing aspect to these four nodes, the name, description, and texture. I usually like to leave a comment in my code saying what each entry in a table is, so here I'll just do "--name, desc, texture"
In lua anything after two hyphens is a comment. This text is then ignored and we can put anything we want there. Use comments to remind yourself what code does, or to let other's know what the code does.
Remember how tables can contain tables, that's exactly what we're going to do here, make a table for each of our nodes inside the grass_table table. The name will be the name of the node, but we can use some shortcuts and only use grass here, and put the grass: bit in with our node registration code. It will make sense in a minute.
We can do the same thing with the description, we only need the part that changes between the nodes in this table. Same with the texture.
In this case we are pulling things from the default mod, so we can ignore default_ in these strings. If we were using textures from multiple different mods we'd need to include the full texture name.
Our completed table has four tables with the name for each node, the description, and texture. So lets put this data to work for us.
Let me introduce loops. Loops do what the name says, they loop over and over, forever if you don't ever tell them to stop. Usually you don't want this as it will usually cause Luanti to crash or hang. There are different types of loops and you can do many different things with loops this will just be a simple example.
We are going to use a for loop, which looks like this. "for k in pairs (grass_table) do whatever code end" This loop will run once for every instance of k, which in our example would be four times. The K here holds no special value, and can be replaced with any variable name you'd like.
To pull the data from the nested tables we are going to create local variables named name, desc, and texture, and assign them data from the table. To do that we use the table name with k in square brackets, and a number in a second sent of square brackets. The k is the reference to the specific nested table we are processing, and the number is the location of the string within that table.
With these three variables we can finally start registering our nodes.
We will use the same node registration as before, and plug our variables in as needed.
Remember how I said we could omit parts of the strings in the table if they are the same across all the nodes. Well to make that work we need to use the concatenate operator ".." This joins two strings into one. So we can change the first line of the node registration to "core.register_node('grass:'..name, {"
The same holds true for the description. Each description starts with 'all faces ' so we can combine the description from the table in the same way. Adding to strings doesn't include a space, so we need to be sure to leave an extra space in the end of the first half of the description string.
Moving on to the tiles, here all the texture names start with 'default_' and also end with '.png' So we will put the variable in the middle and concatenate both to yield the full filename.
The groups and drop stay the same for all the nodes so we don't need to touch them.
At this point we can delete all the old node registrations as our new code is doing all the same work, in about 1/3 of the lines.
If we launch our world we'll find that all our nodes still exist, and look exactly the same as before. Adding or removing nodes is as easy as adding or removing tables from the grass_table table. Keep in mind that supporting other mods will require changing the tiles line and tiles entry in the tables so as to have the correct texture names.
So we have the nodes registered in this loop, let's add the recipes as well. They all follow the exact same pattern, so there is no reason not to.
We'll add one new entry to each table, which will be the ingredient needed to craft the node. I'm not sure what you chose for the coniferous litter node, but I went with the pine_needles.
We'll add one more variable which we'll call ingredient, and pull that from the grass_table tables as before, using the number four, to pull the fourth string.
The craft recipe registration will now output 'grass:'..name, which of course is the name of the node as registered a few lines above. We will put ingredient above, left, below, and to the right of 'default:dirt' in the recipe.
As before we can now remove all old instances of register_craft as all of this is handled in the loop.
Launching our world we can see that all the nodes can still be crafted.
We will find we can't craft various dirt with grasses nodes anymore, as they aren't part of our table and loop. We already have the ingredient, but we don't have the output names. I think you know where this is going.
Homework! Bet you didn't see that coming. Add the output to the grass_table tables and add a second craft registration that uses the new output string for the craft. As usual there will be a completed mod linked on the associated webpage.
- Home
- About
- Blog
- Hire Me
- Luanti
- Let's Plays
- Live Streams
- Minetest 101
- Modding
- Mod Reviews
- Servers
- Tutorials
- Request a Video
- Luanti Curriculum
- Portfolio
- Recipes