Skip to player
Skip to main content
Search
Connect
Watch fullscreen
Like
Bookmark
Share
More
Add to Playlist
Report
Luanti Curriculum: Loops (Lesson 03)
Minetest Videos
Follow
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
Category
📚
Learning
Transcript
Display full video transcript
00:00
If you did your homework, you should have a mod that registers 4 new nodes, along with
00:04
recipes for them.
00:06
In this video, I'll be demonstrating a better way to register multiple nodes that all have
00:10
only slight differences.
00:12
Consider the 4 nodes we created.
00:15
The only real differences are the texture, name, and description.
00:19
They all have the same groups, and all drop dirt.
00:22
We can simplify by creating a table that holds those differences, and pull that data and
00:27
use it to bulk register the nodes with a looping system.
00:30
Before we go on, a quick explanation of tables.
00:33
In Lua, a table is a data structure that can hold many different types of data, strings,
00:38
numbers, or even other tables.
00:41
To create a table, we just put something between an opening and closing curly brace.
00:45
To use the data, we also need a variable that points to the table, such as local A equals
00:52
opening and closing curly braces.
00:54
A table of variables are symbolic names that point to some data.
00:58
This data can be read, written, and modified.
01:01
The names can be a mix of letters, numbers, and underscores.
01:04
They are case sensitive, so my data would be different from my data and my data.
01:11
Variables can also be local or global.
01:15
For the time being, we're going to make all of our variables local.
01:18
We'll talk about this more in a later lesson.
01:20
Let's make the table and populate it with our data.
01:23
If this table needs a name, let's call it grass underscore table.
01:26
I like to replace spaces with underscores, but some people use camel case, where you capitalize
01:31
the first letter of each word in a multi-word variable.
01:34
It doesn't really matter what you choose to use, just stay consistent with it.
01:39
Use care to not make variable names too long, as you have to type them out, and longer names
01:44
are harder to remember and easier to mistype.
01:46
On the flip side, don't make them too short or ambiguous, that you can't remember what
01:50
data they reference.
01:52
Back to our code.
01:54
We need to populate this table.
01:55
We've already discussed how we only have three different aspects to these four nodes.
01:59
The name, description, and texture.
02:01
I usually like to leave a comment in my code saying what each entry in a table is.
02:06
So here I'll just do dash dash name description texture.
02:11
In Lua, anything after two hyphens is a comment.
02:15
This text is then ignored, and we can put anything we want there.
02:18
Use comments to remind yourself what code does, or to let others know what the code does.
02:24
Remember how tables can contain tables?
02:26
Well, that's exactly what we're going to do here.
02:29
We're going to make a table for each of our nodes inside of the grass underscore table table.
02:34
The name will be the name of the node, but we can use some shortcuts and only use grass
02:38
here, and put the grass colon bit in with our node registration code.
02:42
It'll make sense in a minute.
02:44
We can do the same thing with the description.
02:46
We only need the part that changes between the nodes in this table.
02:49
Same with textures.
02:50
In this case, we are pulling things from the default mod, so we can ignore default underscore
02:55
in these strings.
02:57
If we were using textures from multiple different mods, we'd need to include the full texture
03:01
name.
03:02
Our completed table has four tables, with the name for each node, the description, and texture.
03:07
So let's put this data to work for us.
03:10
Let me introduce loops.
03:11
Loops do what the name says.
03:13
They loop over and over, forever, if you don't ever tell them to stop.
03:17
Usually you don't want this, as it will cause the launch to crash or hang.
03:21
There are different types of loops, and you can do many, many different things with loops.
03:26
This will just be a simple example.
03:28
We are going to use a for loop, which looks like this.
03:32
For k in pairs, grass table, do, whatever code, end.
03:39
This loop will run once for every instance of k, which in our example would be four times.
03:45
The k here holds no special value, and can be replaced with any variable name you'd like.
03:50
To pull the data from the nested tables, we're going to create local variables named name,
03:56
description, and texture, and assign them data from the table.
04:01
To do that, we use the table name with the k in square braces, and a number in the second
04:07
set of square braces.
04:09
The k is the reference to the specific nested table we are processing, and the number is the
04:14
location of the string within that table.
04:16
With these three variables, we can finally start registering our nodes.
04:20
We'll use the same node registration as before, and plug our variables in as needed.
04:26
Remember how I said we could omit parts of the string in the table, if they're the same across
04:30
all the nodes?
04:31
Well, to make that work, we need to use the concatenate operator, two periods.
04:37
This joins two strings into one.
04:40
So we can change the first line of all the node registrations to core.register underscore
04:44
node parentheses quote grass colon quote dot dot name comma opening curly brace.
04:52
The same holds true for the description.
04:55
Each description starts with all faces.
04:57
So we can combine the descriptions from the table in the same way.
05:01
Adding to strings doesn't include a space.
05:04
So we need to be sure to leave an extra space in the end of the first half of the description
05:09
string.
05:10
Moving on to the tiles.
05:11
Here, all the texture names start with default underscore, and also end with dot png.
05:17
So we'll put the variable in the middle, and concatenate both to yield the full file name.
05:23
The groups and drops stay the same for all the nodes, so we don't need to touch them.
05:27
At this point, we can delete all the old node registrations, as our new code is doing all
05:31
the same work, in about a third of the lines.
05:34
If we launch our world, we'll find that all our nodes still exist, and look exactly the
05:39
same as before.
05:41
Adding or removing nodes is as easy as adding or removing tables from the graphs underscore
05:45
table table.
05:46
Keep in mind that supporting other mods will require changing the tile lines and tile entry
05:51
in the tables so as to have the correct texture names.
05:55
So we have the nodes registered in this loop, let's add the recipes as well.
06:00
They all follow the exact same pattern, so there's no reason not to.
06:03
We'll add one new entry to each table, which will be the ingredient needed to craft the
06:07
node.
06:08
I'm not sure what you chose for the kind of furious litter node, but I went with pine needles.
06:14
We'll add one more variable, which we'll call ingredients, and pull that from the graphs
06:18
underscore table tables as before, using the number 4 to pull the fourth string.
06:23
The craft recipe registration will now output grass colon name, which of course is the name
06:30
of the node as registered a few lines above.
06:32
We'll put ingredient above, left, below, and to the right of default dirt in the recipe.
06:39
As before, we can now remove all old instances of register craft, as all of this is handled
06:44
in the loop.
06:45
Launching our world, we'll see that all the nodes can still be crafted.
06:49
We will find that we can't craft various dirt with grasses node anymore, as they aren't
06:54
part of our table in the loop.
06:55
We already have the ingredient, but we don't have the output names.
07:00
I think you see where this is going.
07:02
Homework.
07:03
Bet you didn't see that coming.
07:05
Add the output to the grass underscore table tables, and add a second craft registration that
07:11
uses the new output string for the craft.
07:15
As usual, there will be a completed mod linked on the associated web page.
Be the first to comment
Add your comment
Recommended
10:10
|
Up next
Luanti Mod Review: Painting Gallery
Minetest Videos
5 months ago
10:42
Luanti Modding Tutorial: Wear Bar Color
Minetest Videos
7 months ago
13:39
Luanti Mod Review: Bonified
Minetest Videos
7 months ago
7:13
Luanti Mod Review: Placement Preview
Minetest Videos
7 months ago
12:41
Luanti Mod Review: Astralcraft
Minetest Videos
7 months ago
6:28
Oxidizing Copper | Luanti Mod Review
Minetest Videos
5 days ago
5:52
OK Bloomer | Luanti Mod review
Minetest Videos
2 weeks ago
14:26
Luanti Mod Review: Item Replicator
Minetest Videos
2 months ago
6:32
Luanti Mod Review: Skinsdb
Minetest Videos
3 months ago
12:54
Luanti Mod Review: LWRoad track
Minetest Videos
3 months ago
10:42
Luanti Mod Review: Best Lobster
Minetest Videos
3 months ago
8:45
Luanti Mod Review: Blackhole Containment Box
Minetest Videos
3 months ago
20:42
Luanti Mod Review: Wilhelmines Marinara
Minetest Videos
3 months ago
7:43
Luanti Mod Review: Basket
Minetest Videos
4 months ago
10:57
Luanti Mod Review: Elly's Flowers
Minetest Videos
4 months ago
4:53
Luanti Curriculum: Our First Mod (Lesson 02)
Minetest Videos
4 months ago
17:49
Luanti Mod Review: Gooey Mess
Minetest Videos
5 months ago
4:06
Luanti Modding: 01 Intro
Minetest Videos
5 months ago
14:12
Sudoku: A Luanti Game
Minetest Videos
5 months ago
9:44
Luanti Mod Review: Trowel
Minetest Videos
5 months ago
32:08
Luanti Mod Review: Infinity Gauntlet
Minetest Videos
6 months ago
9:13
Luanti Mod Review: Ore Deposits
Minetest Videos
6 months ago
10:28
Luanti Mod Review: Dungeons Plus
Minetest Videos
7 months ago
0:51
Former Aide Claims She Was Asked to Make a ‘Hit List’ For Trump
Veuer
2 years ago
1:08
Musk’s X Is ‘the Platform With the Largest Ratio of Misinformation or Disinformation’ Amongst All Social Media Platforms
Veuer
2 years ago
Be the first to comment