Variables are an essential feature of every scripting language. They serve as containers that store specified information, be it text, numbers, or Userdata, and allow you to manipulate their contents in many scenarios.

In Lua, you can define local and global variables:

 

    • Local variables exist only within the code block in which they are defined.

    • Global variables exist script-wide and can be accessed from anywhere.

(Quick aside: the following is an example of a code block. Instructions belonging to the block are typically inset with a tab for ease of readability. A key detail to remember: in Lua, blocks are always closed with the keyword end at the bottom!)

Lua<span role=”button” tabindex=”0″ data-code=”i = 0
while (i

 

i = 0
while (i < 10) do
  local name = "TombEngine"
  print(name)
  i = i + 1
end

In the above example, the local variable name, defining the string of text "TombEngine", exists only within the block of this while loop (explained in section 5). This means it cannot be used anywhere except inside of it. Every local variable must have the local keyword before the declaration of its name.

Meanwhile, the global variable i, defining the integer 0, exists across the script. This means it can be used anywhere within it, inside and outside any block. A variable without a keyword before the declaration of its name is global by default.

Data Types

In Lua, there are seven types of data you may use in your scripts:

1. Strings

Strings are collections of characters storing text, and are particularly useful if you want to store words, phrases, or sentences for dialogue in a variable.

To declare and define a string, initialize the variable with whatever characters you wish, encased within double-quotes (“”) or single-quotes ():

Lua

local laraCuriousString = "Huh, what is this?"

local lernerAngryString = 'Lara, what are you doing?!'

2. Variables

Integers are whole numbers which can be positive or negative. They are useful for storing things such as health values and timers, and are ideal for dealing with dreaded magic numbers (more on them later).

To declare and define an integer, simply initialize the variable with a whole number:

Lua

local entityHealth = 100

local negativeNumber = -6

3. Floating-Point Number (Decimals)

Floating-point numbers are decimal point numbers, which, just like integers, can be positive or negative. They are useful for storing things that require more precision than what an integer provides.

To declare a and define the floating-point number, initialise the numeric variable with a decimal place:

Lua

local orientationX = 23.9

local negativeRotationY = -55.5

4. Booleans

Booleans can have only two possible values: true or false. Boolean variables are useful for checking event conditions.
Example:

Lua

isLaraKilled = false
if isLaraKilled == true
   print("What have you done?!!")
end

5. Tables

Tables are best understood as a collection, which may be represented by an array (a list of values) or a hash table (a look-up dictionary). They are quite complex and may not be needed for simple setups, but are still worth learning about. Tables will be discussed further in section 6

6. Userdata

Userdata is a very essential data type and will be very common during TombEngine Lua scripting. Userdata are anything that is not built-in lua type
hence in TombEngine, you will have functions and methods which are: GetMoveableByName or Color.new (explanation on these are in the Docs)

Userdata can be used like this:

Lua

local raptor = TEN.Objects.GetMoveableByName("raptor1")` 
or `Color(255, 255, 255)

7. Functions

Functions are not strictly data types, but it may help to conceptualize them as such! They are required to set up volume triggers in Tomb Editor.

Functions primarily serve to provide a clean, intuitive way to create utilitarian blocks of code which may be reused as many times as you like, avoiding the need for repetition.

Common uses for functions include:

 

    • Modifying objects in the game or in your script; for example, incrementing an ammo counter.

    • Making a query; for example, to check whether Lara’s health is below half capacity.

    • Returning a value; for example, to perform a very specific mathematical calculation.

Further reading is in sections 8 (Creating Functions for Volume Triggers) and 9 (Creating Functions Inside the Script).

How to store a boolean in a variable from a condition (optional)

You can easily store the result of a condition in a variable, which stores a boolean (true/false) of the result:

Lua

local unlimited = Lara:GetAmmoCount() == -1

This statement means: “Is the ammo count unlimited?” (note the -1)
if yes it will store is as true if not then false.

The general formula is:

Lua

local variableName = condition

where condition – something to be evaluated which then returns and stores true/false.