Recently I became interested in creating a very simple minetest mod. what is minetest you say? It’s a glorious FOSS alternative to Minecraft, a succesful sandbox game (by the way, do you live under a rock?).

To begin with the simple things here is Hello world, a simple program that will print “Hello world” somewhere to be read by the user.

Minetest is divided into two parts, the core and the game. The core is written in C++ and the game is written in Lua, a scritping language.

To create a mod there is no need to learn C++ because we can change anything using Lua. Everything that is used in the game is written in Lua, every kind of block, tree, animal, player or object, we can always look at the game to have a reference to how we should do things.

To begin here is a silly program called Hello world!, put it into a file called init.lua and save it into the mod directory.

minetest.log("INFO", "Hello world!")

This code will run in the game on startup and it will print the message “Hello World” in the logs, such a shame no one will ever see it.

Obvioulsly every player should be greeted by a friendly message when entering a world.

There is another API we can use to show a message when a player join the game. The message will be written in a new element of the HUD and this element will be added when the player joins the game.

To do something when a player join the game, e.g. show a welcome message, we can use the minetest.register_on_player_join API by passing it a function.

This function recieves a reference to the new player and we can use that to show him a welcome message.

-- when a player join the game execute this
minetest.register_on_player_join(function(player)
        -- add an element to the user interface
        player:hud_add({
                -- display some text
                hud_elem_type = "text",
                -- in this position
                position = {x=0.7,y=0.3},
                -- how many pixel should
                -- the message be
                scale = {x=100, y=100},
                -- here is the text to display
                text = "Hello World!",
                -- this is the text colour
                number = 0x00FF00,
                -- we can align the text
                -- x=0, center horizontaly
                -- y=0, center vertically
                alignment = {x=0,y=0},
                -- display the text with an offset
                -- from position
                offset = {x=0, y=0},
        })
end)

This will add a green “Hello World!” message to every player interface in the game.

Because a mod is executed at startup we can’t use something like

local player = minetest.get_player("singleplayer")
player:hud_add({
        hud_elem_type = "text",
        position = {x=0.7,y=0.3},
        scale = {x=100, y=100},
        text = "Hello World!",
        number = 0x00FF00,
        alignment = {x=0,y=1},
        offset = {x=0, y=-32},
})

because at startup there are no players logged in so it will fail miserably and the simulation will crash.

To give a personal touch to we should greet the player by their name

minetest.register_on_player_join(function(player)
        player:hud_add({
        hud_elem_type = "text",
        position = {x=0.7,y=0.3},
        scale = {x=100, y=100},
        text = "Hello " .. player:get_player_name() .. "!",
        number = 0x00FF00,
        alignment = {x=0,y=1},
        offset = {x=0, y=-32},
})

Key points

You can’t greet the user with the logs, those are for the developers.

Targeting objects from the game works best when they exists, use the events provided by the API to react to objects joining, leaving or spawning in the world.