|
Lua is a lightweight programming language. It is used on MediaWiki wikis, including Fandom wikis, and also in other projects (e.g. game development).[1][2] Fandom wikis have Lua enabled by default.[3]
On wikis, Lua is often used in templates. The Scribunto extension enables it. It's more powerful than parser functions, giving you more options.[1]
|
What Lua looks like
Lua source code goes into pages called modules. Each module returns a Lua table of functions.
Modules are called by {{#invoke:}}. When calling a specific function, it might look like {{#invoke:Module|Function name|parameter 1 (if applicable)|parameter 2 (if applicable, etc.)}}
Here's an example of Lua code for saying "Hello, world!"[1]
local p = {} -- This local variable holds our functions. We usually name it "p"
p.sayHi = function( frame ) -- Add a function named "sayHi" to "p". "frame" keeps our parameters
local message = "Hello, world!" -- Let's make a local variable
return message -- The result of the function is our "message" variable
end -- end of the function "sayHi"
return p -- Returning "p" makes our functions accessible.
On Fandom, you create modules by making pages named Module:ModuleName. Beneath your code is an area called the "Debug console." There, you can run print() functions, like print("Hello, world"). After hitting the enter key, the output should display.
How Lua works
Here are things to know:[1][4]
- Concatenate strings with two periods (..)
- Example:
return "My name is " .. myName
- Example:
- Comments begin with two hyphens (--). For multi-line comments, start with --[[ and end with ]]--.
- Assign values with an equals sign.
- Curly braces are not a thing. Instead, use indentation.
- Use
nilfor null.
Lua will convert string and number types for calculations when possible. For example, 1 + "2" will give you 3, even though "2" is a string.[1]
Boolean logic
- See also: Boolean logic
Lua includes boolean logic.[4]
- Test equality with 2 equals signs (==). For example, (1 == 1) will return true.
- Test inequality with a tilde and an equals sign (~=). For example, (true ~= false) will return true.
- Use the written words "and," "or," and "not" for "and," "or," and "not."
Only nil and false are falsy. Things like 0 and "" (empty string) will be read as true![4]
If, for, etc.
Lua uses English words as operators. If you typically use languages like JavaScript, it may take time to get used to the lack of parentheses and brackets.
Here's an example of an if/else flow:[4]
-- if, elseif, and else
if chocolateCount > 0 then
print("I have chocolate!")
elseif chocolateCount == 0 then
print ("I do not have chocolate.")
else
print ("Something has gone very wrong here.")
You also have for loops.[4]
-- By default, your counter will increment by 1 each time.
for i = 0, 5 do -- Inclusive
print("I will get another piece of chocolate.")
chocolateCount++
-- You can add a third parameter to specify how to change your counter each time.
for i = 5, 0, -1
print("I will eat a piece of chocolate.")
chocolateCount--
Functions
Functions are similar to JavaScript functions: they take in parameters and return values. Rather than a closing curly brace, you write "end" at the end.[4]
local function hello(name)
return "Hello, " .. name .. "!"
end
If you don't write "local" before your function name, it'll be global.
Recursion is allowed.
Tables
Lua calls its arrays "tables."[4] They involve key-value pairs.
myTable = {greeting = "Hello", pi = 3.14, [123] = "counting"}
print(myTable.greeting) -- Prints "Hello"
print(myTable.[123]) -- Prints "counting". Any non-string key needs to be enclosed in brackets
You can build tables to function similarly to classes, including inheritance. You can iterate through a table:[4]
for key, val in pairs(myTable) do
print(key, val)
end
See also
- Data types
- JavaScript
- Parser functions
- Templates
- Template data
- Template parameters
External links
- Learn Lua in 15 Minutes at Tyler Neylon (assumes you have good programming language familiarity)
- Lua templating at Fandom Developers Wiki
- Lua/Tutorial at MediaWiki