This is a quick introduction to the Lua programming language, which is use in Foldit recipes. See Lua Scripting and Foldit Lua Interface for more on how Lua is used in Foldit.

-- - use double dash to immediately precede comments on any line.

--[ [ x ] ] - multi-line comments provided by enclosing comment body in double brackets

+ (addition) - (negation/subtraction) * (multiplication) / (division) ^ (exponent) % (modulo) .. (string concatenation)

> < >= <= == ~= - relational operators

= - assignment. Note that Lua allows multiple assignments like a,b = 1,2.

a={} - creates empty table

a[1]=3; a["apple"]=4; a[12]="red" - sets some values in table

#a - indexing upward from zero, this returns the index of the last non-nil value. For example, if a[1] is nil, #a returns zero.

nil - a non-value that indicates non-existent variables

true false - Boolean values. Note everything except false and nil evaluates as true (including zero and empty string!)

and or not - Boolean operators

if then elseif else end - keywords for structuring if/then statements

while do end - keywords for while statement

repeat until - keywords for repeat statement

for i=1,N,delta do end - numeric "for" statement

for i in iterator(table) do end - generic "for" statement

break - terminates a loop

local - limits scope of variable to the current block. Use this in functions to avoid corrupting global variables.

function name(arg) return end - keywords for structuring function.

Note that functions can return multiple values. Functions can also take variable numbers of arguments by including an ellipsis at the end of the argument list. The following example illustrates this:

function test(...)
  local args={...}
  for i=1,#args do  print(args[i])  end
  return args[1], args[2]
a,b = test(1,2,3)
Community content is available under CC-BY-SA unless otherwise noted.