<p>Minetest has a ScriptAPI (Applictation Programming Interface), which is used to program Mods (Modifications) for the game, extending its features and adding new items.</p>
<p>This ScriptAPI is accessed using an easy-to-use programming langauge called Lua.</p>
<h3>Requirements</h3>
<ul>
<li>Basic Programming Knowledge, ideally in the Lua Language (<ahref="#learntocode">learn</a>)</li>
<divid="step">2) Copy the file 'tutorial_decowood.png' supplied with this Document to the textures folder in the mod.</div>
<br/>
Try it) Launch the game now, and notice that the mods are automatically loaded and compiled. This means when changing the code you simply have to 'Exit to Menu' and 'Start Game/Connect' again to try out the changes.
<br/>
Let's try out our first mod! Open the chat window ingame (press t) and enter "/giveme tutorial:decowood 99" (Without "" of course). This will add 99 blocks of the decorative wood to your inventory!
<p><tablebgcolor="#FFFFCC">
<tr><td><center>
The "give" privilage is required for the /giveme command to work<br/><br/>
To grant yourself the "give" privilage, go to worlds/gamename/auth.txt and open it.<Br/>
Add ",give" after "shout,interact" to make it "shout,interact,give"<br/>
</center></td></tr>
</table></p>
<h3>Let's have a look at the source code:</h3>
<p>The function minetest.register_node(name, table) is responsible for adding new blocks to the game (node=block, but also torches, rails, ...)<br/>
It takes 2 Parameters: The name of the new block ("tutorial:decowood", the string before : MUST be the name of the mod folder) and a table with several properties of the block.</p>
<p>In this case we use 2 properties:</p>
<ul>
<li>tile_images: Sets the texture of the block; You can use only 1 texture or multiple textures,
seperated by commas {"tex1.png", "tex2.png", ...}. The game checks for the texture files in ALL textures folders of the game.</li>
<li>group: This sets the time it takes to destroy the block, and the tool required <ahref="http://minetest.net/wiki/doku.php?id=roadmap:digging_time_groups">See more</a></li>
<p>Crafting does not only play an important role in Minecraft, Minetest also uses different crafting recipes. Therefore it is important to know what crafting means and how to code it!<br/>
<p>Crafting means to creating Tools, Blocks and Other Objects.<br/>
In minetest you have a 3x3 crafting area by default with a 1x1 output field.</p>
<p>For example, a stone pickaxe can be made out of 2 Sticks and 3 Cobblestones:<br/>
<p><imgsrc="3.png"alt="The craft recipe for a stone pick-axe"/></p>
ABMs stands for "Active Block Modifiers" and they add actions to blocks. For instance, the tutorial-wood could become normal wood after a few seconds.<br/>
<divid="step">Append this code to your init.lua:</div>
<p><code><pre>
minetest.register_abm(
{nodenames = {"tutorial:decowood"},
interval = 30,
chance = 1,
action = function(pos)
minetest.env:add_node(pos, {name="default:wood"})
end,
})
</pre></code></p>
Try it out! It's really annoying to see all your decowood creations destroyed after 30 seconds, they simply become normal wood.<br/>
<p>But how does this work?<br/>
The function minetest.register_abm registers an action for each block of the same type.</p>
<p>nodenames = {"tutorial:decowood'} means that the action is processed for each decowood block.</p>
<p>You could also try "default:stone" instead of that to turn all stone blocks into wood.</p>
<p>interval = 30 means that the action is performed every 30 seconds. It starts counting at the beginning of the game. After 30 seconds all actions are processed, it doesn't matter when the block was placed.</p>
<p>This is not a per-block timer!</p>
<p>chance = 1 means that the probability of the action is 1:1, it happens in every case.<br/>
A higher value means that it's less probable.</p>
<p>action = function(pos) is the function that is actually performed.</p>
<p>It contains the command minetest.env:add_node. More about it in the next section<br/>
<h3>minetest.env:add_node</h3>
<p>This is a function that is used to add/replace a node the given place</p>
<p>minetest.env:add_node has 2 parameters.
<uL>
<li>The position parameter (more information later)<br/>
<li>A table which defines the properties of the block, e.g. the name, the direction it faces, ...<br/><br/>
</ul>
In this case the name is enough to define what block you can see.
</p>
So let's assume we want to create a mod that makes junglegrass grow above every dirt-with-grass block. This should be a slow process, one dirt-with-grass block after the other should be grown over.
But there is a solution! <code>dofile(minetest.get_modpath("tutorial").."/anotherfile.lua")</code> will tell Minetest to look for anotherfile.lua in the same folder as init.lua, and load its contents.
As with most programming, when you develop you tend to get what are called "bugs" and errors, which are basically human mistakes.<br/>
There are three types of bugs/errors
<p><ul>
<li>Compile Time Errors - These occur when Minetest is loading the mods, and are caused by syntax errors (a simple mistake like leaving a "}" out)</li>
<li>Runtime Errors - These occur while the game is being played, and often crash the game. (eg: mod "modname:blockname" is not defined)</li>
<li>Runtime Bugs - These bugs cause the mod not to work as planned</li>
</ul></p>
<h3>Avoiding Syntax Mistakes</h3>
To help avoid syntax mistakes, make sure your code is easy to read.
Also you should check your work and put comments in
<p>
<code>
pos.y=pos.y+1 --This line increases the position's y axis by 1
</code>
</p>
<h3>Avoiding Runtime Mistakes</h3>
<tablebgcolor="#FFFFCC">
<tr><td><p>
The Console is the black window with writing in that appears when Minetest runs.</p>
</td></tr>
</table>
<p>LUA has a function called "print" and it displays a message to the console.</p>
<p>
<code>
print("message to send")
</code>
</p>
You should the print function so you know how far Minetest gets in a program.<br/>
<p>
<tablebgcolor="#FFFFCC">
<tr><th>Why use <u>print</u></th></tr><tr><td>
For example, you have a mistake in the code:
<p>
<code>
<pre>
check_something(1)
function check_something(value)
if value=0 then
print("it ends up here")
else
print("but you are certern that value=1")
end
end
</pre>
</code>
</p>
The aboves Runtime bug was caused by a single "=" instead of double "==", and so instead of checking if value was equal to 0, it set it to 0 resulting in true<br/>
And so having print helps point out your mistake.
</td></tr>
</table>
</p>
<h1>Apendix</h1>
<h2><aname="learntocode">Learn to code</a></h2>
Don't have any knowledge? Use the following to learn:
<ul>
<li><ahref="http://codecademy.com">Codecademy</a> -Learn the basics of programming (it is Javascript (not Java) but still helps)</li>