Lesson 03 (Groups) updated Tue, 05/29/2018 - 14:24

     In this lesson we'll be learning about groups. I've briefly mentioned them in some of the previous lessons, and will be talking about them in future lessons as well, so now is as good a time as any to discuss them.

     We've used the oddly_breakably_by_hand group in some of our node registrations, so let's start by discussing what that group is and how it works, and then move on to other groups that are similar to it. Items in this group don't really make sense to be breakable by hand, but to allow for the player to get started they need to be. The group provides a rating of one through three, with higher values resulting in faster dig times. If the player is wielding a tool that can dig the node faster than these values the engine will use the tools speed. For example tree trunks are oddly_breakably_by_hand and choppy, so if the player is using an ax the engine will use the choppy speed from the ax. Tools define what the digging speed will be for specific groups and ratings, and we'll cover that in detail in the lesson on tools. Higher values result in faster dig times. Minetest_game uses the following groups to define what tools should be used to break the nodes.

Crumbly nodes are those that should be dug with shovels, things such as dirt, sand, gravel, silt, etc.
Cracky nodes are breakable with a pickax, things like stone and metal blocks.
Snappy nodes are best broken with a sword, in game these will be items like leaves, grasses, and other small plants.
Choppy nodes should be broken with an ax. Most wooden items will be in this group, trees, wood blocks, slabs stairs, and even wooden furniture, chests and shelves.
Dig_immediate nodes can be broken without any wear to a tool, set to two it will take about half a second to break the node, set to three it will be removed instantly.


     All of these groups can have a rating from one to three with higher values resulting in faster break times. If you are making custom tools you can use ratings higher than three, but none of the tools from Minetest_game will be able to break the nodes.

     Tools use an elaborate system of defining uses and dig times for different groups, and even have an option to make certain tools not able to break specific ratings in their group. For example a wooden pickax can't dig a stone with diamond block. We'll cover that in detail in the lesson on tools though.

     When registering a node you can have multiple of these groups, which will allow a player to use different tools to break the node. In the lesson on tools we'll look at how you can make a tool that is suited for breaking items of multiple groups. For right now we'll just look at making nodes that have multiple groups.

     If we create a node and give it just one of these tool related groups, say choppy=3 we'll only be able to break the node with an ax.
minetest.register_node("mod_1:fake_diamond", {
   description = "break with an ax",
   tiles = {"default_diamond_block.png"},
   groups = {choppy=3},
})

     Maybe we want to let the player break the node with other tools, but make it take a long time we can do that by adding another group with a lower rating, like crumbly=1. When using multiple groups they need to be separated with a comma. White space is ignored here, so you can put spaces between the group name and rating or not, it won't matter, though I strongly suggest you decide upon one way, and stick with that to keep your code all consistent.
minetest.register_node("mod_1:fake_diamond", {
   description = "break with an ax",
   tiles = {"default_diamond_block.png"},
   groups = {choppy=3, crumbly=1},
})

     The fake_diamond can now by broken with an ax or shovel. If both tools are the same tier, wood, stone, steel, etc the ax will break the node faster, however if you have a diamond shovel it will break the node faster than a wooden ax.

     Not all groups need ratings but you still need to give it a value of one for it to work. As mentioned in the last lesson on crafts the wood group lets players use the wood from any tree in a craft, which simplifies the code for a modder so they don't have to create a bunch of different recipes to allow the player to make an item from each type of wood.

     There are many groups used for crafting purposes, and you can create your own custom groups as well with zero effort. Let's take a look at some of the crafting groups from Minetest_game and then cover how to make our own groups.

Sand is for sand nodes, Minetest game had silver sand, desert sand, and beach sand, if your mod adds another type of sand you should probably add the sand group to it so people can use it in recipes that need sand.
Stone is for stone nodes, this also includes cobblestone, stone block and stone brick and the desert variants.
Wood is used for wood planks, but not tree trunks, stairs or slabs.
Tree is used for tree trunks.
Wool is used for wool, this is due to the fact that wool can by dyed, and the colored wools can all be used in crafts that need wool, even though the resulting object might not have colored wool in it.
Leaves are leaves from trees or bushes. Some recipes let you create oil from leaves, or sticks from leaves.
Water_bucket is used for buckets that are full of water. This is useful if you want to create a recipe that needs water, but don't care what type of water is used. Just be sure to use a replacement to give the player the empty bucket back.

     Using any of these groups is completely optional. If you are making a mod that adds sand, and you don't want to add the sand group you don't have to, these are just used to make things slightly more compatible between mods and games. You can use these groups in your recipes even if your mod doesn't add anything that is in that group, and any other mods that have that group will be usable in your mod. You don't even have to list them as optional dependencies.

     If you want to add your own custom group just add it in the node registration, that's literally all there is to it, then use that group in your recipes. Groups can be used for other things as well, but most of them are out of the scope of this lesson, so we'll cover them in the future.

     Let's go ahead and add a new group to both of our nodes
minetest.register_node("mod_1:first", {
   description = "Our very first node ever!",
   tiles = {"mod_1_first.png"},
   groups = {oddly_breakable_by_hand = 2, mod_1=1},
})

minetest.register_node("mod_1:fake_diamond", {
   description = "Break with an axe",
   tiles = {"default_diamond_block.png"},
   groups = {choppy=3, crumbly=1, mod_1=1},
})

     we can now create a craft recipe that uses group:mod_1 as an recipe item
minetest.register_craft({
   output = "mod_1:fake_diamond 2",
   recipe = {
      {"group:mod_1"},
      {"default:dirt"},
      {"group:mod_1"}
   }
})

     We can make two fake diamonds using two first nodes and dirt, or two fake diamonds and dirt, or a first node, a fake diamond node, and dirt. Because we're using groups we have more options as to how the item is crafted all without needing to create a bunch of recipes, which really saves us a lot of time.

     In the future when we get to making dynamic worlds and mods that do stuff we'll come back to groups and see how they can be used for things other than craft recipes and defining how quickly they can be broken with tools.

     That concludes this lesson, now on to the challenge. Using what you've learned in this lesson, and the previous lessons create two new nodes, and give them some groups to control how they can be broken, and to use them in crafts. Create two craft recipes that use some of the groups you just used in the node registrations.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.