From ad04ccd95112ea5e5ce6810dca7de0eb3cce748a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 24 Feb 2015 09:53:25 +0000 Subject: [PATCH] Lua Scripts: Correct syntax errors and explain dump and nil --- chapters/lua.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/chapters/lua.md b/chapters/lua.md index 9485d3f..be84988 100644 --- a/chapters/lua.md +++ b/chapters/lua.md @@ -227,9 +227,12 @@ Whereas global variables can be accessed from anywhere in the script file, and f my_global_variable = "blah" function one() - my_global_variable_2 = "blah" + my_global_variable = "three" end +print(my_global_variable) -- Output: "blah" +one() +print(my_global_variable) -- Output: "three" {% endhighlight %} @@ -246,8 +249,15 @@ end function two() print(dump(foo)) -- Output: "bar" end + +one() +two() {% endhighlight %} +dump() is a function that can turn any variable into a string so the programmer can +see what it is. The foo variable will be printed as "bar", including the quotes +which show it is a string. + 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 @@ -262,8 +272,14 @@ end function two() print(dump(foo)) -- Output: nil end + +one() +two() {% endhighlight %} +Nil means **not initalised**. The variable hasn't been assigned a variable yet, +doesn't exist or has been uninitalised (ie: set to nil) + 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. @@ -281,7 +297,7 @@ you add them all into a table with the same name as the mod: mymod = {} function mymod.foo(bar) - return foo .. "bar" + return "foo" .. bar end -- In another mod, or script: