|
| 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 %} |
0 commit comments