Lua Scripts: started
This commit is contained in:
parent
beabaa8c30
commit
77fc1401cb
@ -12,8 +12,9 @@
|
|||||||
<li><a href="{{ page.root }}chapters/folders.html">1 - Folder Structure</a></li>
|
<li><a href="{{ page.root }}chapters/folders.html">1 - Folder Structure</a></li>
|
||||||
<li><a href="{{ page.root }}chapters/nodes_items_crafting.html">2 - Nodes, Items and Crafting</a></li>
|
<li><a href="{{ page.root }}chapters/nodes_items_crafting.html">2 - Nodes, Items and Crafting</a></li>
|
||||||
<li><a href="{{ page.root }}chapters/node_drawtypes.html">3 - Node Drawtypes</a></li>
|
<li><a href="{{ page.root }}chapters/node_drawtypes.html">3 - Node Drawtypes</a></li>
|
||||||
<li><a href="{{ page.root }}chapters/abms.html">4 - Active Block Modifiers</a></li>
|
<li><a href="{{ page.root }}chapters/lua.html">4 - Lua Scripts</a></li>
|
||||||
<li><a href="{{ page.root }}chapters/formspecs.html">5 - Formspecs</a></li>
|
<li><a href="{{ page.root }}chapters/abms.html">5 - Active Block Modifiers</a></li>
|
||||||
|
<li><a href="{{ page.root }}chapters/formspecs.html">6 - Formspecs</a></li>
|
||||||
<li><hr></li>
|
<li><hr></li>
|
||||||
<li><a href="{{ page.root }}book_index.html">Index</a></li>
|
<li><a href="{{ page.root }}book_index.html">Index</a></li>
|
||||||
<li><a href="{{ page.root }}lua_api.html">Webpage version of lua_api.txt</a></li>
|
<li><a href="{{ page.root }}lua_api.html">Webpage version of lua_api.txt</a></li>
|
||||||
|
132
chapters/lua.md
Normal file
132
chapters/lua.md
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
---
|
||||||
|
title: Lua Scripts
|
||||||
|
layout: default
|
||||||
|
root: ../
|
||||||
|
---
|
||||||
|
|
||||||
|
<div class="notice">
|
||||||
|
<h2>This chapter is incomplete</h2>
|
||||||
|
|
||||||
|
The wording or phrasing may be hard to understand.
|
||||||
|
Don't worry, we're working on it.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
------------
|
||||||
|
|
||||||
|
In this chapter we will talk about scripting in Lua, the tools required,
|
||||||
|
and go over some techniques which you will probably find useful.
|
||||||
|
|
||||||
|
This chapter will assume that you have had some programming experience before,
|
||||||
|
even Scratch level is acceptable.
|
||||||
|
|
||||||
|
Tools
|
||||||
|
-----
|
||||||
|
|
||||||
|
A text editor with code highlighting is sufficient for writing scripts in Lua.
|
||||||
|
Code highlighting gives different words and characters different colors in order to
|
||||||
|
make it easier to read the code and spot any mistakes.
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
function ctf.post(team,msg)
|
||||||
|
if not ctf.team(team) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
if not ctf.team(team).log then
|
||||||
|
ctf.team(team).log = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(ctf.team(team).log,1,msg)
|
||||||
|
ctf.save()
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
For example, keywords in the above snippet are highlighted, such as if, then, end, return.
|
||||||
|
table.insert is a function which comes with Lua by default.
|
||||||
|
|
||||||
|
### Integrated Programming Environments
|
||||||
|
|
||||||
|
IDEs allow you to debug code like a native application.
|
||||||
|
These are harder to set up than just a text editor.
|
||||||
|
|
||||||
|
One such IDE is Eclipse with the Koneki Lua plugin:
|
||||||
|
|
||||||
|
* Install Eclipse + Koneki.
|
||||||
|
* Create a new Lua project from existing source (specify Minetest's base directory).
|
||||||
|
* Follow instructions from Koneki wiki how to do "Attach to remote Application" debugging (just a few steps).
|
||||||
|
* It is suggested to add those lines from wiki at beginning of builtin.lua.
|
||||||
|
* Start the debugger (set "Break on first line" in debugger configuration to see if it is working).
|
||||||
|
* Start Minetest.
|
||||||
|
* Enter the game to startup Lua.
|
||||||
|
|
||||||
|
Local and Global
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Local should be used as much as possible.
|
||||||
|
Lua is global by default, which means that variables declared in a function
|
||||||
|
could be read by other functions.
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
function one()
|
||||||
|
foo = "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
function two()
|
||||||
|
print(dump(foo)) -- Output: "bar"
|
||||||
|
end
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
This is sloppy coding, and Minetest will in fact warn you about this.
|
||||||
|
To correct this, use "local":
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
function one()
|
||||||
|
local foo = "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
function two()
|
||||||
|
print(dump(foo)) -- Output: nil
|
||||||
|
end
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
The same goes for functions, you should make functions as local as much as possible,
|
||||||
|
as other mods could have functions of the same name.
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
local function foo(bar)
|
||||||
|
return bar * 2
|
||||||
|
end
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
If you want your functions to be accessible from other scripts or mods, it is recommended that
|
||||||
|
you add them all into a table with the same name as the mod:
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
mymod = {}
|
||||||
|
|
||||||
|
function mymod.foo(bar)
|
||||||
|
return foo .. "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
-- In another mod, or script:
|
||||||
|
mymod.foo("foobar")
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
Including other Lua Scripts
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
You can include Lua scripts from your mod, or another mod like this:
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
dofile(minetest.get_modpath("modname") .. "/script.lua")
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
"local" variables declared outside of any functions in a script file will be local to that script.
|
||||||
|
You won't be able to access them from any other scripts.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{% highlight lua %}
|
||||||
|
{% endhighlight %}
|
@ -80,7 +80,20 @@ figure {
|
|||||||
background: #444;
|
background: #444;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.notice {
|
||||||
|
margin: 10px;
|
||||||
|
display: block;
|
||||||
|
padding: 5px;
|
||||||
|
border: 1px solid orange;
|
||||||
|
border-radius: 5px;
|
||||||
|
background: #FFEEDD;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.notice h2 {
|
||||||
|
margin: 0 0 5px 0;
|
||||||
|
padding: 0 0 2px 0;
|
||||||
|
}
|
||||||
#navbar li a.title {
|
#navbar li a.title {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
font-size: 120%;
|
font-size: 120%;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user