Modding Minetest - Hello World
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.
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.
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
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
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.