Tabletop Simulator Guide

A Short Lua Tutorial for Tabletop Simulator

A Short Lua Tutorial

Overview

A very basic, but clear guide to the use and syntax of the Lua programming language. An essential reference for those new to Lua or scripting.

Introduction

Taken from:
https://web.archive.org/web/20110811101759/http://wiki.vodpod.com/w/page/12352674/A%C2%A0Short-Lua-Tutorial
(edited for minor corrections and additional examples)

When I saw this guide, I found it to be a very clear and organized reference to the basic syntax and use of operators, control structures, functions, etc. of the Lua language. There is obviously more, but anyone programming in Lua either knows and uses everything here, or could use this as a reference.

If you’re completely new to Lua, this information is an essential reference from which to start.

(I was attempting to check if there were any very basic guides such as this already on Steam, but Guides are currently completely unavailable to search or browse – go Steam.)

Literal values

Lua literals are very standard:

5.5 – defines a number. All numbers in Lua are floats. true – defines the boolean value true false – defines the boolean value false nil – defines null, the lack of any value

Lua has a single datatype called simple “table” which is used to implement both arrays and associative arrays.

Expressions

Lua arithmetic expressions are basically the same as Javascript.

Mathematic: +, -, /, * String concat: .. (like “foo” .. “bar” yields “foobar”) Comparison: ==, ~= (not equal), <, >, <=, >= Boolean: <expr> or <expr>, <expr> and <expr>, not <expr> Assignment: myvar = <expr> Function call: function(..arguments..)

Identifiers

Identifiers must be alphanumeric, include underscore, and cannot start with a number.

Valid Examples:

table1 List_of_Names _temp_value_ a1x2

INVALID Examples:

row&column 1name important_number!

Separators

Generally Lua can use either newlines or ‘;’ as statement separators. However, statements do not actually need to be delimited, and some control statements like “if” and “for” can use newlines or just a space, but cannot use ‘;’ as a delimiter.

Note: It is usually recommended to use newlines between statements for clarity, readability, and maintenance (when you or others go back to modify the code at a later date). I’d suggest only formatting as the shown in some of the compact examples below for special circumstances, or if the statements are very short.

Examples:

local var1 = 1 local var2 = 0 local var1 = 1 local var2 = 0 if var1 == 0 then var2 = 1 end if var1 == 0 then var2 = 1 else var2 / var1 end if var1==0 then var2=1 else var2/var1 end for k,v in pairs(table1) do print(k, v) end for k,v in pairs(table1) do print(k,v) end

Note: If using a compressed control strucure, I’d reccommend putting the ‘end’ statement on the next line (possibly also ‘else’ statements for clarity), as it can aid in debugging when an ‘end’ statement is missing elsewhere in your script. Example:

if var1==0 then var2=1 else var2/var1 end

Control Statements

if-then-elseif-else statement

if <expr> then — do stuff elseif <expr> then — do different stuff else — if nothing else, do this stuff end

A for loop running “i” from 1 to 10.

for i=1,10 do — stuff to do 10 times end

A for loop running “j” from 90 to 0 in steps down of 30.

for j = 90, 0, -30 do — something to do once with each number: 90, 60, 30, 0 end

A for loop setting “val” to each entry in the table “array” in turn, and running “index” from 1 to the length of “array”.

for index,val in ipairs(array) do array[index] = val end

A loop over the key,value pairs in a hash table (Lua just calls it a ‘table’).

for key,val in pairs(table) do — do stuff with key,value pairs in the table end

while-do statement

while <expr> do — keep doing this stuff as long as <expr> is true — this stuff won’t happen if <expr> is already true end

repeat-until statement

repeat — keep doing this stuff until <expr> is true — this stuff will happen at least once until <expr>

Functions

Functions in Lua must be called using parentheses (like Javascript, but unlike Ruby). Arguments are passed by value and by position. You can omit arguments to function calls and their values will be set to nil. Basically Lua function calls work just like Javascript functions, right down to closures. To return a value from a function, you must use the “return” operator (unlike Ruby).

function calculator(op, a, b) if op == ‘+’ then return a+b elseif op == ‘*’ then return a*b end return 0 end > print( calculator() ) 0 > print( calculator(‘+’, 3, 6) ) 9.0 > print( calculator(‘*’, 3, 6) ) 18.0

Tables and Functions

The Lua “table” datatype is used much like the Javascript associative array. Tables can be used like integer-indexed arrays or like hash tables with arbitrary keys.

When using a table like an array, indexes start at 1 (unlike most other languages).

Array example

3.0 print(i .. “: ” .. color) end 1: red 2: white 3: blue 4: green

Associative array example

print(key .. “: ” .. val) end name: scott age: 39 eyes: blue children: #tablex0393 (tables don’t print by themselves)

Using tables as function namespaces
A common technique in Lua is to use tables as namespaces for functions. Laminate uses this extensively.

> util = {} > util.print_table = function(atable) { for key,val in pairs(atable) do print(key .. “: ” .. val) end } > > wine = {color: ‘red’, price:35.0} > util.print_table(wine) color: red price: 35.0

Conclusion

Please comment with any questions, corrections, or suggestions for additional information.

For any additions however, I may want to keep this guide as a very basic introduction and not have it become too expansive.

SteamSolo.com