Skip to content

Commit 77fc140

Browse files
committed
Lua Scripts: started
1 parent beabaa8 commit 77fc140

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

_includes/header.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
<li><a href="{{ page.root }}chapters/folders.html">1 - Folder Structure</a></li>
1313
<li><a href="{{ page.root }}chapters/nodes_items_crafting.html">2 - Nodes, Items and Crafting</a></li>
1414
<li><a href="{{ page.root }}chapters/node_drawtypes.html">3 - Node Drawtypes</a></li>
15-
<li><a href="{{ page.root }}chapters/abms.html">4 - Active Block Modifiers</a></li>
16-
<li><a href="{{ page.root }}chapters/formspecs.html">5 - Formspecs</a></li>
15+
<li><a href="{{ page.root }}chapters/lua.html">4 - Lua Scripts</a></li>
16+
<li><a href="{{ page.root }}chapters/abms.html">5 - Active Block Modifiers</a></li>
17+
<li><a href="{{ page.root }}chapters/formspecs.html">6 - Formspecs</a></li>
1718
<li><hr></li>
1819
<li><a href="{{ page.root }}book_index.html">Index</a></li>
1920
<li><a href="{{ page.root }}lua_api.html">Webpage version of lua_api.txt</a></li>

chapters/lua.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Lua Scripts
3+
layout: default
4+
root: ../
5+
---
6+
7+
<div class="notice">
8+
<h2>This chapter is incomplete</h2>
9+
10+
The wording or phrasing may be hard to understand.
11+
Don't worry, we're working on it.
12+
</div>
13+
14+
Introduction
15+
------------
16+
17+
In this chapter we will talk about scripting in Lua, the tools required,
18+
and go over some techniques which you will probably find useful.
19+
20+
This chapter will assume that you have had some programming experience before,
21+
even Scratch level is acceptable.
22+
23+
Tools
24+
-----
25+
26+
A text editor with code highlighting is sufficient for writing scripts in Lua.
27+
Code highlighting gives different words and characters different colors in order to
28+
make it easier to read the code and spot any mistakes.
29+
30+
{% highlight lua %}
31+
function ctf.post(team,msg)
32+
if not ctf.team(team) then
33+
return false
34+
end
35+
if not ctf.team(team).log then
36+
ctf.team(team).log = {}
37+
end
38+
39+
table.insert(ctf.team(team).log,1,msg)
40+
ctf.save()
41+
42+
return true
43+
end
44+
{% endhighlight %}
45+
46+
For example, keywords in the above snippet are highlighted, such as if, then, end, return.
47+
table.insert is a function which comes with Lua by default.
48+
49+
### Integrated Programming Environments
50+
51+
IDEs allow you to debug code like a native application.
52+
These are harder to set up than just a text editor.
53+
54+
One such IDE is Eclipse with the Koneki Lua plugin:
55+
56+
* Install Eclipse + Koneki.
57+
* Create a new Lua project from existing source (specify Minetest's base directory).
58+
* Follow instructions from Koneki wiki how to do "Attach to remote Application" debugging (just a few steps).
59+
* It is suggested to add those lines from wiki at beginning of builtin.lua.
60+
* Start the debugger (set "Break on first line" in debugger configuration to see if it is working).
61+
* Start Minetest.
62+
* Enter the game to startup Lua.
63+
64+
Local and Global
65+
----------------
66+
67+
Local should be used as much as possible.
68+
Lua is global by default, which means that variables declared in a function
69+
could be read by other functions.
70+
71+
{% highlight lua %}
72+
function one()
73+
foo = "bar"
74+
end
75+
76+
function two()
77+
print(dump(foo)) -- Output: "bar"
78+
end
79+
{% endhighlight %}
80+
81+
This is sloppy coding, and Minetest will in fact warn you about this.
82+
To correct this, use "local":
83+
84+
{% highlight lua %}
85+
function one()
86+
local foo = "bar"
87+
end
88+
89+
function two()
90+
print(dump(foo)) -- Output: nil
91+
end
92+
{% endhighlight %}
93+
94+
The same goes for functions, you should make functions as local as much as possible,
95+
as other mods could have functions of the same name.
96+
97+
{% highlight lua %}
98+
local function foo(bar)
99+
return bar * 2
100+
end
101+
{% endhighlight %}
102+
103+
If you want your functions to be accessible from other scripts or mods, it is recommended that
104+
you add them all into a table with the same name as the mod:
105+
106+
{% highlight lua %}
107+
mymod = {}
108+
109+
function mymod.foo(bar)
110+
return foo .. "bar"
111+
end
112+
113+
-- In another mod, or script:
114+
mymod.foo("foobar")
115+
{% endhighlight %}
116+
117+
Including other Lua Scripts
118+
---------------------------
119+
120+
You can include Lua scripts from your mod, or another mod like this:
121+
122+
{% highlight lua %}
123+
dofile(minetest.get_modpath("modname") .. "/script.lua")
124+
{% endhighlight %}
125+
126+
"local" variables declared outside of any functions in a script file will be local to that script.
127+
You won't be able to access them from any other scripts.
128+
129+
130+
131+
{% highlight lua %}
132+
{% endhighlight %}

static/style.css

+13
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,20 @@ figure {
8080
background: #444;
8181
}
8282

83+
.notice {
84+
margin: 10px;
85+
display: block;
86+
padding: 5px;
87+
border: 1px solid orange;
88+
border-radius: 5px;
89+
background: #FFEEDD;
8390

91+
}
92+
93+
.notice h2 {
94+
margin: 0 0 5px 0;
95+
padding: 0 0 2px 0;
96+
}
8497
#navbar li a.title {
8598
text-align: center;
8699
font-size: 120%;

0 commit comments

Comments
 (0)