diff --git a/_en/basics/lua.md b/_en/basics/lua.md index 6ae2f3f..47bf68d 100644 --- a/_en/basics/lua.md +++ b/_en/basics/lua.md @@ -13,14 +13,10 @@ In this chapter we'll talk about scripting in Lua, the tools required to assist with this, and some techniques which you may find useful. - [Programming](#programming) + - [Coding in Lua](#coding-in-lua) - [Code Editors](#code-editors) -- [Coding in Lua](#coding-in-lua) - - [Program Flow](#program-flow) - - [Variable Types](#variable-types) - - [Arithmetic Operators](#arithmetic-operators) - - [Selection](#selection) - - [Logical Operators](#logical-operators) - [Local and Global Scope](#local-and-global-scope) + - [Locals should be used as much as possible](#locals-should-be-used-as-much-as-possible) - [Including other Lua Scripts](#including-other-lua-scripts) @@ -41,6 +37,13 @@ however, the following websites are quite useful in developing this: * [Programming with Mosh](https://www.youtube.com/user/programmingwithmosh) is a good YouTube series to learn programming. +### Coding in Lua + +It's also beyond the scope of this book to teach Lua coding. +The [Programming in Lua](https://www.lua.org/pil/contents.html) book is an +excellent introduction to Lua programming. + + ## Code Editors A code editor with code highlighting is sufficient for writing scripts in Lua. @@ -79,128 +82,6 @@ Commonly used editors which are well-suited for Lua include: Other suitable editors are also available. -## Coding in Lua - -### Program Flow - -Programs are a series of commands that run one after another. We call these -commands "statements." Program flow is how these statements are executed, and -different types of flow allow you to skip or jump over sets of commands. - -There are three main types of flow: - -* Sequence: runs one statement after another, with no skipping. -* Selection: skips over sequences depending on conditions. -* Iteration: repeats the same statements until a condition is met. - -So, what do statements in Lua look like? - -```lua -local a = 2 -- Set 'a' to 2 -local b = 2 -- Set 'b' to 2 -local result = a + b -- Set 'result' to a + b, which is 4 -a = a + 10 -print("Sum is "..result) -``` - -In this example, `a`, `b`, and `result` are *variables*. Local variables are -declared by using the `local` keyword, and then given an initial value. Local -will be discussed later, because it's part of a very important concept called -*scope*. - -The `=` sign means *assignment*, so `result = a + b` means set the value of -`result` to the value of `a + b`. Variable names can be longer than one -character, as seen with the `result` variable. It's also worth noting that, like -most languages, Lua is *case-sensitive*; `A` is a different variable to `a`. - - -### Variable Types - -A variable will be only one of the following types and can change type after an -assignment. -It's good practice to make sure a variable is only ever nil or a single non-nil type. - -| Type | Description | Example | -|----------|---------------------------------|----------------| -| Nil | Not initialised. The variable is empty, it has no value | `local A`, `D = nil` | -| Number | A whole or decimal number. | `local A = 4` | -| String | A piece of text. | `local D = "one two three"` | -| Boolean | True or False. | `local is_true = false`, `local E = (1 == 1)` | -| Table | Lists. | Explained below. | -| Function | Can run. May require inputs and may return a value. | `local result = func(1, 2, 3)` | - -### Arithmetic Operators - -Operators in Lua include: - -| Symbol | Purpose | Example | -|--------|----------------|---------------------------| -| A + B | Addition | 2 + 2 = 4 | -| A - B | Subtraction | 2 - 10 = -8 | -| A * B | Multiplication | 2 * 2 = 4 | -| A / B | Division | 100 / 50 = 2 | -| A ^ B | Powers | 2 ^ 2 = 22 = 4 | -| A .. B | Join strings | "foo" .. "bar" = "foobar" | - -Please note that this is not an exhaustive list; it doesn't contain every -possible operator. - -### Selection - -The most basic method of selection is the if statement. For example: - -```lua -local random_number = math.random(1, 100) -- Between 1 and 100. -if random_number > 50 then - print("Woohoo!") -else - print("No!") -end -``` - -This generates a random number between 1 and 100. It then prints "Woohoo!" if -that number is bigger than 50, and otherwise prints "No!". - - -### Logical Operators - -Logical operators in Lua include: - -| Symbol | Purpose | Example | -|---------|--------------------------------------|-------------------------------------------------------------| -| A == B | Equals | 1 == 1 (true), 1 == 2 (false) | -| A ~= B | Doesn't equal | 1 ~= 1 (false), 1 ~= 2 (true) | -| A > B | Greater than | 5 > 2 (true), 1 > 2 (false), 1 > 1 (false) | -| A < B | Less than | 1 < 3 (true), 3 < 1 (false), 1 < 1 (false) | -| A >= B | Greater than or equals | 5 >= 5 (true), 5 >= 3 (true), 5 >= 6 (false) | -| A <= B | Less than or equals | 3 <= 6 (true), 3 <= 3 (true) | -| A and B | And (both must be correct) | (2 > 1) and (1 == 1) (true), (2 > 3) and (1 == 1) (false) | -| A or B | either or. One or both must be true. | (2 > 1) or (1 == 2) (true), (2 > 4) or (1 == 3) (false) | -| not A | not true | not (1 == 2) (true), not (1 == 1) (false) | - -Please note that this doesn't contain every possible operator. - -It is also possible to combine operators. For example: - -```lua -if not A and B then - print("Yay!") -end -``` - -This prints "Yay!" if A is false and B is true. - -Logical and arithmetic operators work the same way; they both accept inputs and -return a value which can be stored. For example: - -```lua -local A = 5 -local is_equal = (A == 5) -if is_equal then - print("Is equal!") -end -``` - ## Local and Global Scope @@ -239,6 +120,9 @@ one() two() ``` + +### Locals should be used as much as possible + Local variables should be used whenever possible. Mods should only create one global at most, with the same name as the mod. Creating other globals is sloppy coding, and Minetest will warn about this: