From 77fc1401cb7121a003d626fac31d13bcd4fc8898 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 30 Dec 2014 18:50:46 +0000 Subject: [PATCH] Lua Scripts: started --- _includes/header.html | 5 +- chapters/lua.md | 132 ++++++++++++++++++++++++++++++++++++++++++ static/style.css | 13 +++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 chapters/lua.md diff --git a/_includes/header.html b/_includes/header.html index 0952354..51d5e5b 100644 --- a/_includes/header.html +++ b/_includes/header.html @@ -12,8 +12,9 @@
  • 1 - Folder Structure
  • 2 - Nodes, Items and Crafting
  • 3 - Node Drawtypes
  • -
  • 4 - Active Block Modifiers
  • -
  • 5 - Formspecs
  • +
  • 4 - Lua Scripts
  • +
  • 5 - Active Block Modifiers
  • +
  • 6 - Formspecs

  • Index
  • Webpage version of lua_api.txt
  • diff --git a/chapters/lua.md b/chapters/lua.md new file mode 100644 index 0000000..7fc32b4 --- /dev/null +++ b/chapters/lua.md @@ -0,0 +1,132 @@ +--- +title: Lua Scripts +layout: default +root: ../ +--- + +
    +

    This chapter is incomplete

    + + The wording or phrasing may be hard to understand. + Don't worry, we're working on it. +
    + +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 %} diff --git a/static/style.css b/static/style.css index 2a99931..d61f96c 100644 --- a/static/style.css +++ b/static/style.css @@ -80,7 +80,20 @@ figure { 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 { text-align: center; font-size: 120%;