Lua Scripting Tutorial (Beginner 3) introduced more programming concepts:

  • functions
  • variables
  • if-then-else statements

Here is some more technical background on these important topics, highlighting some of the differences between Lua and other languages.

As before, not everything is explained completely. For more detail, check out the Lua 5.3 reference manual.


All of the tutorials so far involved calling functions, which include the Lua function print, and the Foldit Lua Interface functions like selection.SelectRange and structure.RebuildSelected. Lua Scripting Tutorial (Beginner 3) introduced creating a new function, a utility function called "round".

The main difference between functions in Lua and languages like the C programming language is that Lua functions can return multiple values. Also, other languages require a function to declare up front whether they return a value. In Lua, a function can decide what it wants to return, then return as many values as needed.

Here's an example:

function xyz ()  
   return 1, 2, 3 
xx, yy, zz = xyz ()
print ( "xx = " .. xx .. ", yy = " .. yy .. ", zz = " .. zz )

You can copy and paste this example into the Foldit recipe editor (click "New (ScriptV2)" first), then run it to see what happens.


Lua Scripting Tutorial (Beginner 3) introduced the use of variables. Variables are used to store numbers, strings of characters, and other values. Many programming languages require you to declare the type of each variable before you can use the variable. The type can't change, and trying to put the wrong type of value into a variable causes and error.

Compared to other languages, Lua has a limited selection of types. Also, Lua is dynamically typed, meaning that variables don't have a specific type assigned. You can store a number in a variable, then turn around and put a string in the same variable. (This may not be a good idea in general, but Lua allows it.)

The main types so far are:

  • numbers - Lua numbers include both integers (1, 2, 3) and reals (123.475, -39.32, 3.14)
  • strings - Lua strings can be double- or single-quoted and any length, so "ABC", 'ABC', "A", and 'B' are strings
  • boolean - Lua "boolean" values can be true or false

Lua has some built-in functions that can be helpful in dealing with types:

  • type ( vv ) returns a string representing the type of variable "vv". The type function may be useful in cases where a caller needs to pass different types to a function.
  • tostring ( vv ) converts the variable "vv" to a string. Lua automatically converts numbers to strings, but it for some reason, it doesn't handle booleans the same way. Use tostring to print the value of a variable containing a boolean.

If-Then-Else StatementsEdit

Lua Scripting Tutorial (Beginner 3) included an if-then-else statement. Most programming languages have something similar, but the syntax in Lua is different than the syntax in related languages.

In Lua, there's no need to enclose the conditional expression of an if statement in parenthesis. In general, Lua relies on keywords instead of punctuation.

The Lua code used in this tutorial:

if postScore > preScore then
    print ( "gain is " .. round ( postScore - preScore ) ) 
    print ( "loss is " .. round ( preScore - postScore ) .. ", resetting to recentbest pose" ) 

could be written in the C programming language as:

if ( postScore > preScore)
    printf ( "gain is %.3\n", postScore - preScore );
    printf( "loss is %.3f, resetting to recentbest pose\n",  preScore - postScore );

Lua allows parentheses in conditional expressions, but they're only needed for complex expressions. Complex conditional expressions involve the Lua logical operators: and, or, and not.

Community content is available under CC-BY-SA unless otherwise noted.