Skip to playerSkip to main content
  • 22 hours ago
When we deal with bulk node registrations we can sometimes save a lot of typing by using a table and a loop to register all the nodes.

Download Luanti: https://www.luanti.org/downloads/

https://www.nathansalapat.com/LMC/03-loops
https://nathansalapat.com/SocialMedia
https://nathansalapat.com/support-me
Transcript
00:00If you did your homework, you should have a mod that registers 4 new nodes, along with
00:04recipes for them.
00:06In this video, I'll be demonstrating a better way to register multiple nodes that all have
00:10only slight differences.
00:12Consider the 4 nodes we created.
00:15The only real differences are the texture, name, and description.
00:19They all have the same groups, and all drop dirt.
00:22We can simplify by creating a table that holds those differences, and pull that data and
00:27use it to bulk register the nodes with a looping system.
00:30Before we go on, a quick explanation of tables.
00:33In Lua, a table is a data structure that can hold many different types of data, strings,
00:38numbers, or even other tables.
00:41To create a table, we just put something between an opening and closing curly brace.
00:45To use the data, we also need a variable that points to the table, such as local A equals
00:52opening and closing curly braces.
00:54A table of variables are symbolic names that point to some data.
00:58This data can be read, written, and modified.
01:01The names can be a mix of letters, numbers, and underscores.
01:04They are case sensitive, so my data would be different from my data and my data.
01:11Variables can also be local or global.
01:15For the time being, we're going to make all of our variables local.
01:18We'll talk about this more in a later lesson.
01:20Let's make the table and populate it with our data.
01:23If this table needs a name, let's call it grass underscore table.
01:26I like to replace spaces with underscores, but some people use camel case, where you capitalize
01:31the first letter of each word in a multi-word variable.
01:34It doesn't really matter what you choose to use, just stay consistent with it.
01:39Use care to not make variable names too long, as you have to type them out, and longer names
01:44are harder to remember and easier to mistype.
01:46On the flip side, don't make them too short or ambiguous, that you can't remember what
01:50data they reference.
01:52Back to our code.
01:54We need to populate this table.
01:55We've already discussed how we only have three different aspects to these four nodes.
01:59The name, description, and texture.
02:01I usually like to leave a comment in my code saying what each entry in a table is.
02:06So here I'll just do dash dash name description texture.
02:11In Lua, anything after two hyphens is a comment.
02:15This text is then ignored, and we can put anything we want there.
02:18Use comments to remind yourself what code does, or to let others know what the code does.
02:24Remember how tables can contain tables?
02:26Well, that's exactly what we're going to do here.
02:29We're going to make a table for each of our nodes inside of the grass underscore table table.
02:34The name will be the name of the node, but we can use some shortcuts and only use grass
02:38here, and put the grass colon bit in with our node registration code.
02:42It'll make sense in a minute.
02:44We can do the same thing with the description.
02:46We only need the part that changes between the nodes in this table.
02:49Same with textures.
02:50In this case, we are pulling things from the default mod, so we can ignore default underscore
02:55in these strings.
02:57If we were using textures from multiple different mods, we'd need to include the full texture
03:01name.
03:02Our completed table has four tables, with the name for each node, the description, and texture.
03:07So let's put this data to work for us.
03:10Let me introduce loops.
03:11Loops do what the name says.
03:13They loop over and over, forever, if you don't ever tell them to stop.
03:17Usually you don't want this, as it will cause the launch to crash or hang.
03:21There are different types of loops, and you can do many, many different things with loops.
03:26This will just be a simple example.
03:28We are going to use a for loop, which looks like this.
03:32For k in pairs, grass table, do, whatever code, end.
03:39This loop will run once for every instance of k, which in our example would be four times.
03:45The k here holds no special value, and can be replaced with any variable name you'd like.
03:50To pull the data from the nested tables, we're going to create local variables named name,
03:56description, and texture, and assign them data from the table.
04:01To do that, we use the table name with the k in square braces, and a number in the second
04:07set of square braces.
04:09The k is the reference to the specific nested table we are processing, and the number is the
04:14location of the string within that table.
04:16With these three variables, we can finally start registering our nodes.
04:20We'll use the same node registration as before, and plug our variables in as needed.
04:26Remember how I said we could omit parts of the string in the table, if they're the same across
04:30all the nodes?
04:31Well, to make that work, we need to use the concatenate operator, two periods.
04:37This joins two strings into one.
04:40So we can change the first line of all the node registrations to core.register underscore
04:44node parentheses quote grass colon quote dot dot name comma opening curly brace.
04:52The same holds true for the description.
04:55Each description starts with all faces.
04:57So we can combine the descriptions from the table in the same way.
05:01Adding to strings doesn't include a space.
05:04So we need to be sure to leave an extra space in the end of the first half of the description
05:09string.
05:10Moving on to the tiles.
05:11Here, all the texture names start with default underscore, and also end with dot png.
05:17So we'll put the variable in the middle, and concatenate both to yield the full file name.
05:23The groups and drops stay the same for all the nodes, so we don't need to touch them.
05:27At this point, we can delete all the old node registrations, as our new code is doing all
05:31the same work, in about a third of the lines.
05:34If we launch our world, we'll find that all our nodes still exist, and look exactly the
05:39same as before.
05:41Adding or removing nodes is as easy as adding or removing tables from the graphs underscore
05:45table table.
05:46Keep in mind that supporting other mods will require changing the tile lines and tile entry
05:51in the tables so as to have the correct texture names.
05:55So we have the nodes registered in this loop, let's add the recipes as well.
06:00They all follow the exact same pattern, so there's no reason not to.
06:03We'll add one new entry to each table, which will be the ingredient needed to craft the
06:07node.
06:08I'm not sure what you chose for the kind of furious litter node, but I went with pine needles.
06:14We'll add one more variable, which we'll call ingredients, and pull that from the graphs
06:18underscore table tables as before, using the number 4 to pull the fourth string.
06:23The craft recipe registration will now output grass colon name, which of course is the name
06:30of the node as registered a few lines above.
06:32We'll put ingredient above, left, below, and to the right of default dirt in the recipe.
06:39As before, we can now remove all old instances of register craft, as all of this is handled
06:44in the loop.
06:45Launching our world, we'll see that all the nodes can still be crafted.
06:49We will find that we can't craft various dirt with grasses node anymore, as they aren't
06:54part of our table in the loop.
06:55We already have the ingredient, but we don't have the output names.
07:00I think you see where this is going.
07:02Homework.
07:03Bet you didn't see that coming.
07:05Add the output to the grass underscore table tables, and add a second craft registration that
07:11uses the new output string for the craft.
07:15As usual, there will be a completed mod linked on the associated web page.
Be the first to comment
Add your comment

Recommended