<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://olddev.minetest.org/index.php?action=history&amp;feed=atom&amp;title=Guide%3A_Optional_dependencies</id>
	<title>Guide: Optional dependencies - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://olddev.minetest.org/index.php?action=history&amp;feed=atom&amp;title=Guide%3A_Optional_dependencies"/>
	<link rel="alternate" type="text/html" href="https://olddev.minetest.org/index.php?title=Guide:_Optional_dependencies&amp;action=history"/>
	<updated>2026-04-15T04:02:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.7</generator>
	<entry>
		<id>https://olddev.minetest.org/index.php?title=Guide:_Optional_dependencies&amp;diff=56&amp;oldid=prev</id>
		<title>&gt;ROllerozxa at 12:14, 17 December 2022</title>
		<link rel="alternate" type="text/html" href="https://olddev.minetest.org/index.php?title=Guide:_Optional_dependencies&amp;diff=56&amp;oldid=prev"/>
		<updated>2022-12-17T12:14:02Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{LuaTips}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This short guide describes how to convert any mandatory dependency to an optional one and briefly discusses this technique.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The “standard” technique ==&lt;br /&gt;
One way to make a dependency optional is to insert the code which causes the mandatory dependency into a simple &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; block and optionally insert fallback code into the &amp;lt;code&amp;gt;branch&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With &amp;lt;code&amp;gt;minetest.get_modpath&amp;lt;/code&amp;gt; it can be checked whether a mod is present and loaded, as it will return nil if not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if minetest.get_modpath(&amp;quot;example&amp;quot;) then&lt;br /&gt;
	--[[ Insert code which depends on the example mod here ]]&lt;br /&gt;
else&lt;br /&gt;
	--[[ Optionally insert fallback code here when the mod is not available.&lt;br /&gt;
	     If you intend no fallback, you can leave this section empty. ]]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition, you need to add the depending mod into your mod's optional dependencies list in &amp;lt;code&amp;gt;mod.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Examples ===&lt;br /&gt;
The following example code has a mandatory dependency on the default mod, because the crafting recipe requires items from the default mod:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
minetest.register_craft({&lt;br /&gt;
	output = &amp;quot;example:awesome_item&amp;quot;,&lt;br /&gt;
	recipe = {&lt;br /&gt;
		{ &amp;quot;default:wood&amp;quot;, &amp;quot;default:stone&amp;quot;, &amp;quot;default:mese&amp;quot; },&lt;br /&gt;
		{ &amp;quot;&amp;quot;, &amp;quot;default:wood&amp;quot;, &amp;quot;&amp;quot; },&lt;br /&gt;
	}&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By putting above code into the template, we get the following code, which is now ''optionally'' dependent on the default mod.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if minetest.get_modpath(&amp;quot;default&amp;quot;) then&lt;br /&gt;
	minetest.register_craft({&lt;br /&gt;
		output = &amp;quot;example:awesome_item&amp;quot;,&lt;br /&gt;
		recipe = {&lt;br /&gt;
			{ &amp;quot;default:wood&amp;quot;, &amp;quot;default:stone&amp;quot;, &amp;quot;default:mese&amp;quot; },&lt;br /&gt;
			{ &amp;quot;&amp;quot;, &amp;quot;default:wood&amp;quot;, &amp;quot;&amp;quot; },&lt;br /&gt;
		}&lt;br /&gt;
	})&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; has been left out because we don't intend to use any fallback option here.&lt;br /&gt;
&lt;br /&gt;
== Discussion ==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;/div&gt;</summary>
		<author><name>&gt;ROllerozxa</name></author>
	</entry>
</feed>