Skip to content

Commit

Permalink
Lua Scripts: update
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Dec 30, 2014
1 parent 77fc140 commit 7d2308a
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions chapters/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ 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
* Integrated Programming Environments
* Local and Global
* Including other Lua Scripts

Tools
-----

Expand Down Expand Up @@ -64,9 +69,40 @@ One such IDE is Eclipse with the Koneki Lua plugin:
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.
Whether a variable is local or global determines where it can be written to or read to.
A local variable is only accessible from where it is defined. Here are some examples:

{% highlight lua %}
-- Accessible from within this script file
local one = 1

function myfunc()
-- Accessible from within this function
local two = one + one

if two == one then
-- Accessible from within this if statement
local three = one + two
end
end
{% endhighlight %}

Whereas global variables can be accessed from anywhere in the script file, and from any other mod.

{% highlight lua %}
my_global_variable = "blah"

function one()
my_global_variable_2 = "blah"
end

{% endhighlight %}


### Locals should be used as much as possible

Lua is global by default (unlike most other programming languages).
Local variables must be identified as such.

{% highlight lua %}
function one()
Expand All @@ -78,7 +114,10 @@ function two()
end
{% endhighlight %}

This is sloppy coding, and Minetest will in fact warn you about this.
This is sloppy coding, and Minetest will in fact warn you about this:

[WARNING] Assigment to undeclared global 'foo' inside function at init.lua:2

To correct this, use "local":

{% highlight lua %}
Expand All @@ -91,7 +130,8 @@ function two()
end
{% endhighlight %}

The same goes for functions, you should make functions as local as much as possible,
The same goes for functions. Functions are variables of a special type.
You should make functions as local as much as possible,
as other mods could have functions of the same name.

{% highlight lua %}
Expand Down

0 comments on commit 7d2308a

Please sign in to comment.