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:
(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!)
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.
In Lua, there are seven types of data you may use in your scripts:
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 (”):
local laraCuriousString = "Huh, what is this?" local lernerAngryString = 'Lara, what are you doing?!'
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:
local entityHealth = 100 local negativeNumber = -6
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:
local orientationX = 23.9 local negativeRotationY = -55.5
Booleans can have only two possible values: true or false. Boolean variables are useful for checking event conditions.
isLaraKilled = false if isLaraKilled == true print("What have you done?!!") end
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
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:
Color.new (explanation on these are in the Docs)
Userdata can be used like this:
local raptor = TEN.Objects.GetMoveableByName("raptor1")` or `Color(255, 255, 255)
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:
Further reading is in sections 8 (Creating Functions for Volume Triggers) and 9 (Creating Functions Inside the Script).
You can easily store the result of a condition in a variable, which stores a boolean (true/false) of the result:
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:
local variableName = condition
where condition – something to be evaluated which then returns and stores true/false.