Guide: Optional dependencies

Introduction
Managing and declaring dependencies are a core part of modding for Minetest. However, sometimes it is more desireable to write mod code which is only optionally dependent on another mod, not mandatorily, epspecially if only a small part of the actual code is actually dependent on the other mod. One example are crafting recipes; often, they are not that important, since the core part of a mod, namely, the registered items, can still function without them.

This short guide describes how to convert any mandatory dependency to an optional one and briefly discusses this technique.

The “standard” technique
One way to make a dependency optional is to insert the code which causes the mandatory dependency into a simple  block and optionally insert fallback code into the

With minetest.get_modpath is can be checked whether a mod is present, it will return nil if the mod is not present.

Additionally, the file depends.txt needs to be adjusted accordingly; it must contain the line “ ” instead of “ ”.

Examples
The following example code has a mandatory dependency on the default mod, because the crafting recipe requires items from the default mod:

By putting above code into the template, we get the following code, which is now optionally dependent on the default mod.

Note that the  has been left out because we don't intend to use any fallback option here.

Discussion
This technique is useful when you have small and simple chunks of code causing a mandatory dependency. This technique is especially useful for crafting recipes, as they are often not that neccessary; other mods may be more interested in the registered items and may even register their own crafting recipes.

In theory, all dependencies can be made optional using this technique. But sometimes, it more practical to keep a mandatory dependency, especially if large portions of code are dependent on another mod and writing a fallback option would be unreasonable.

If your almost your entire mod code is dependent on another code, writing a fallback option would likely be highly redundant, so you should keep the dependency optional.