Our previous mod has taught us something interesting about minetest events. As an example we added a custom Hello world! message to the player HUD when they join our world.

The problem is: this is everything but fun.

But I want to amend myself and now I want to teach you how to destroy blocks, oh this will be fun.

Obviously we will write a mod in lua to break things, maybe a tool that destroy more than one block.

To create a new tool we have to register it with the register_tool API.

minetest.register_tool("pewpew", {
        descritpion = "pewpew",
})

This snippet of code will be run at startup and it will register a new tool, like a shovel or an axe, called “pewpew”.

This is still nothing but fun.

So let’s add something that we can do with this new tool.

minetest.register_tool("pewpew:pewpew", {
        descritpion = "pewpew",
        -- activate the object on left click
        on_use = function (itemstack, user, pointed_thing)
                -- if you are pointing at something this retrieve it's position
                local position = minetest.get_pointed_thing_position(pointed_thing)
                -- this tool only works on nodes, not objects or players
                if pointed_thing.type == "node" then
                        -- if the position exists
                        if position then
                                -- dig a node as you were there
                                minetest.dig_node(position)
                        end
                end
                -- do not change the object used
                return nil
        end
})

And now when we use the tool pewpew it destroy nodes from distance, a fancy tool.

Still this is very like a normal tool, perhaps we can extend it’s range.

minetest.register_tool("pewpew:pewpew", {
        descritpion = "pewpew",
        range = 25.0,
        -- activate the object on left click
        on_use = function (itemstack, user, pointed_thing)
                -- if you are pointing at something this retrieve it's position
                local position = minetest.get_pointed_thing_position(pointed_thing)
                -- this tool only works on nodes, not objects or players
                if pointed_thing.type == "node" then
                        -- if the position exists
                        if position then
                                -- dig a node as you were there
                                minetest.dig_node(position)
                        end
                end
                -- do not change the object used
                return nil
        end
})

But increasing the tool range too much can slow down your game as the computer take advantage of untouched farther block. Now we can change blocks furter than ever and so the game have to pick our pace rendering and updating more blocks than before.

Maybe a shovel will do better.

Key points

We can create tools to fit our needs.

All tool are alike, their on_use attribute is different.

Extending tools has a tradeoff in game.

We can peek at default tools in the minetest game repository