Lua Language
Lua is an interpreted language that is used for scripting in Total War. It's lightweight, fast, and easy to use.
Lua instructions are written in a text file, which is generally given a .lua
file extension. The game looks to load certain script files when loading, and may be configured to load specific scripts when loading a particular campaign or battle.
Version 5.1 of lua is used in Total War scripting - the latest at time of writing is version 5.3.5.
Whitespace in lua is ignored, so scripts can be spaced out according to the scripters preference. Lua statements may optionally be terminated with a semicolon. Individual lines may be commented with --
- this causes the rest of the line to be ignored. Blocks of script of any size may be commented with --[[ script ]]
. These blocks may not be nested, however.
Lua is case-sensitive, so variables named value
, Value
and VALUE
are all different. This is a common source of bugs.
Example:
-- this line is commented
--[[
this line is also commented
]]
value = 6
print(value); -- semicolon optional
print(Value)
6
nil
Relevant in Campaign | |
Relevant in Battle | |
Relevant in Frontend |
More information about lua can be found on the following sites:
https://www.lua.org | Lua homepage. |
https://www.lua.org/demo.html | Lua demo site - allows snippets of script to be tested outside of the game (very useful). |
https://www.lua.org/manual/5.1/ | The lua manual. |
http://lua-users.org/wiki/ | Lua wiki - contains useful information about supplied libraries. |
https://www.tutorialspoint.com/lua/ | Lua tutorial - others are available. |
Lua supports only eight data types, six of which are documented further down this page:
Data Type | Description |
---|---|
nil | The absence of a value. |
boolean | true /false values. |
number | Floating-point numeric values. |
string | Text values. |
function | Executable chunks of script. |
table | Dynamically-sized key/value lists, may be used to build complex data structures. |
userdata | Objects provided by the host program to lua, with an interface on which script may make function calls. |
thread | Represents independent threads of execution - not supported in Total War scripting. |
Variables created in lua are global by default, which means they are accessible across the entire scope of the script. Variables can optionally be declared with local scope, whereby they only persist for the lifetime of the block in which they are declared. It is strongly encouraged to declare variables with local scope unless there is a compelling reason to do otherwise, as it lessens the risk of them being accessed or overwritten by accident.
Example:
-- global and local variable declarations
g_var_one = 10
local l_var_one = 20
-- declarations within a block
if g_var_one then
g_var_two = 20
local l_var_two = 30 -- will fall out of scope as the block ends
end
print("g_var_one: " .. tostring(g_var_one))
print("l_var_one: " .. tostring(l_var_one))
print("g_var_two: " .. tostring(g_var_two))
print("l_var_two: " .. tostring(l_var_two))
g_var_one: 10
l_var_one: 20
g_var_two: 20
l_var_two: nil
An if
statement may be used to perform a logical test and, based on the boolean result of that test, execute different blocks of instruction. An if
statement will execute a block of script specified by a mandatory then
operator if the logical test evaluates to true
, or a second optional block specified by an else
operator if it doesn't. Each if
statement must be terminated by the keyword end
.
An else
operator following another if
statement may be combined into an elseif
operator. Converting nested if
statements to elseif
operators in this way saves on having to terminate each if
statement with end
- see the example below.
Example:
t = true
f = false
if t == true then
print("t is true")
end
if f == true then
print("f is true")
else
print("f is false")
end
n = 28
if n < 5 then
print("n is less than 5")
elseif n < 10 then
print("n is less than 10")
elseif n < 15 then
print("n is less than 15")
elseif n < 20 then
print("n is less than 20")
else
print("n is greater than or equal to 20")
end
t is true
f is false
n is greater than or equal to 20
Conditional tests may be performed by structures such as if
statements, while
loops and repeat
loops to make decisions about what scripts to execute. Any values can be evaluated in a conditional test - non-boolean values may be evaluated in a boolean manner as follows:
false
boolean values, andnil
values, evaluate tofalse
.- Any other value evaluates to
true
(including the number 0, which evaluates tofalse
in C).
The logical operator not
may be used to negate the result of a boolean - if passed a true
value it returns false
, and if passed a false
value it returns true
. The value returned by the not
operator is always boolean, even if the value supplied was not. The statement x = not not x
converts x to a boolean value, therefore.
The logical operators and
and or
may be used to assess multiple boolean conditions together. The and
operator returns true
if both of the expressions passed to it evaluate to true
themselves. The or
operator returns the second value passed to it if the first evaluates to false
, otherwise the first value is returned. Unlike and
, not
and other comparison operators, therefore, or
can return something other than a boolean value. This can be useful for setting default values for a variable - see the example below.
The lua interpreter reads forward when performing conditional tests, and will not evaluate expressions that it doesn't need to. For example, if the first expression passed to an and
operator evaluates to false
then the second is never evaluated. Likewise, if the first expression passed to an or
operator evaluates to true
then the second expression is never evaluated (and the first is returned). These constructs can both be useful in different ways - see the examples below.
The not
operator has precedence (meaning it gets evaluated first), followed by the and
operator and finally the or
operator. Parenthesis ()
can be used to override the natural order of precedence.
Example - Relying on conversion to boolean to test existence of value:
num_value = 10
-- A number of any value evaluates to true.
-- This is akin to saying "if num_value has been set.."
if num_value then
print("num_value evaluates to true")
end
num_value evaluates to true
Example - Using 'not' to convert to boolean:
num_value = 10
print("not num_value: " .. not num_value)
print("not not num_value: " .. not not num_value)
not num_value: false
not not num_value: true
Example - and/or operator examples:
t = true
f = false
if t and f then
print("this should never get printed")
end
if t and not f then -- "not f" evaluates to true
print("this should get printed")
end
if t or f then
print("either t or f is true")
end
this should get printed
either t or f is true
Example - Compound logical test of greater length, with parenthesis:
t = true
f = false
if not (t and (not f or not t)) then
print("???")
else
print("wha?")
end
wha?
Example - Using 'or' to set a default value during an assignment operation:
function returns_nothing()
-- do nothing
end
-- returns_nothing() evaluates to nil/false,
-- so 'or' will return the second value here
value = returns_nothing() or 1
print("value is " .. tostring(value))
value is 1
Example - Using 'and' to test existence or type before value of variable, to prevent errors :
This example shows how the 'and' operator can be used in a serial to guard operations on values that might otherwise fail and cause script errors. Performing a numeric comparison such as > on a non-numeric value is illegal and would cause a script failure. This is prevented by the first expression which tests whethervalue
is a number. If value
is not a number, and the type
check returns false
, lua will not proceed on to evaluate the second expression (containing the numeric comparison) as the interpreter is smart enough to realise that it can't affect the final result.
function test_value(value)
if type(value) == "number" and value > 10 then
print("value " .. tostring(value) .. " is a number > 10")
else
print("value " .. tostring(value) .. " is not a number or a number <= 10")
end
end
test_value(20)
test_value("hello")
test_value(false)
value 20 is a number > 10
value hello is not a number or a number <= 10
value false is not a number or a number <= 10
Lua provides a number of standard functions for various puposes, some of which are documented below.
-
print(...
values to print)
-
Prints one or more supplied values to the standard output. In Total War games, the standard output is the game console, and optionally also a text file. Non-string values passed to the function are cast to string before printing.
Parameters:
1
...
values to print
Returns:
nil
Example:
print("hello") -- string
print(5, 3) -- two numbers
print({}) -- a table
hello
5 3
table: 0xb805do
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 192
-
type(variable
variable)
-
Returns the type of a specified value as a string.
Parameters:
1
variable
variable
Returns:
typestring
Example:
print(type(not_defined))
print(type("hello"))
print(type(5))
print(type({}))
nil
string
number
table
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 203
-
tostring(variable
variable)
-
Returns the specified value cast to a string. The specified value is unaffected.
Parameters:
1
variable
variable
Returns:
variable cast to stringstring
Example:
value = 6
value_as_string = tostring(value)
print(value, type(value))
print(value_as_string, type(value_as_string))
6 number
6 string
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 217
-
tonumber(variable
variable)
-
Returns the specified string value cast to a number. This only works for string values that contain numeric characters (e.g. "65"). If the supplied value is a string that does not contain numeric characters, or is any other value other than a number then
nil
is returned.Parameters:
1
variable
variable
Returns:
variable cast to numbernumber
Example:
numeric_str_value = "26"
non_numeric_str_value = "hello"
number_value = 18
boolean_value = true
table_value = {}
print(tonumber(numeric_str_value), tonumber(non_numeric_str_value), tonumber(number_value), tonumber(boolean_value), tonumber(table_value))
26 nil 18 nil nil
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 229
-
loadstring(
lua stringstring
)
-
Returns an executable function created from a string. This string may be constructed manually, or generated by another function such as
string.dump
. When the returned function is executed the lua script contained within the specified string is run.
Use this function sparingly. See external documentation for more information.Parameters:
1
lua string
Returns:
lua functionfunction
Example:
function test_func()
print("hello");
end;
local str_a = string.dump(test_func);
local str_b = "print(\"123\")";
func_a = loadstring(str_a);
func_b = loadstring(str_b);
func_a()
func_b()
hello
123
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 242
Nil
The data type nil
represents the absence of a value in lua. nil
is both the name of the type and also the only value that a variable of that type can take. All variables contain the value nil
, and are of type nil
, before they are declared. Assigning nil
to a variable is the same as deleting it.
nil
values resolve to false
when considered in a logical expression.
Example:
print("no_value has a value of " .. tostring(no_value) .. " and is of type " .. type(no_value))
some_value = 6
some_value = nil -- deleting
if not some_value then
print("some_value is false or nil")
end
no_value has a value of nil and is of type nil
some_value is false or nil
Booleans
Boolean values may be either true
or false
. Boolean values are of most use when evaluated by language structures such as if
statements and while
loops that perform logical tests and take action based on the result. The logical operators and
, or
and not
can be used to evaluate booleans.
See the section on Conditional Operators
for more information.
Example:
t = true
-- must use tostring as the .. concatenation operator wont work with booleans
print("t is " .. tostring(t) .. " and of type " .. type(t))
f = 6 > 7 -- logical expression evaluates to false
print("f is " .. tostring(f))
print("not f is " .. tostring(not f))
print("f and t is " .. tostring(f or t))
if f or t then
print("f or t must be true!")
end
t is true and of type boolean
f is false
not f is true
f and t is true
f or t must be true!
Numbers
Numeric values in lua are real, so they may contain decimal places. There is no integer numeric type. The default lua language specification sets numbers to be stored as double-precision floats. At time of writing, however, numbers in Total War's implementation of lua are stored as single-precision floats, which offer only about 7 digits of precision. Scripters should be aware of this limitation when planning scripts that may potentially have to deal with very large or precise numbers.
Number values can be added, subtracted, multiplied and divided with the +, -, * and / operators respectively. Exponents may be expressed with the ^ operator.
Example:
a = 5
b = a + 10
c = b * 2
d = c / 10
e = a ^ d
print(a, b, c, d, e)
5 15 30 3.0 125.0
Strings
Strings are variables containing representations of text. A string may be specified by enclosing a value in matching single or double quotes. A string can be zero, one or more characters long, with no upper limit beyond the amount of memory available.
Strings may be joined together using the concatenation operator, ..
.
Example:
str_a = "hello"
str_b = "world"
print(str_a .. " " .. str_b)
hello world
Some of the string functions described in the next section make use of string patterns, a method of matching sequences of characters akin to regular expressions. More information about lua string patterns may be found on lua-users.org
here.
Lua provides a number of operations that can be performed on strings, listed below. More detailed information about these functions may be found on the dedicated page on lua-users.org
here.
The functions may be called in the form string.<function>(<string>, <arguments>)
, or <string>:<function>(<arguments>)
where appropriate.
-
string.byte([
first charnumber
], [
last charnumber
])
-
Returns the numerical code corresponding to the characters of a specified portion of the string. The portion of the string is specified by index positions of the start and end characters.
Parameters:
1
optional, default value=1
Position of first character in the substring. If this is not a valid character index for this string then
nil
is returned.2
optional, default value=1
Position of last character in the substring. If the first character position is specified and this is not, then this is set to the value of the first character position (so that only one character is returned).
Returns:
...
number
character value(s)
Example:
print(string.byte("hello")) -- prints first character by default
print(string.byte("hello", 4)) -- print single character
print(string.byte("hello", 1, 3)) -- prints range of characters
104
108
104 101 108
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 383
-
string.char(...
character values)
-
Returns a string constructed from the specified numeric character values. Number character values can be obtained with
string.byte
.Parameters:
1
...
Vararg of
number
character values.Returns:
nil
Example:
print(string.char(104, 101, 108, 108, 111))
hello
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 396
-
string.dump(
functionfunction
)
-
Returns a string representation of a supplied function, which can later be passed to the
loadstring
function to be reconstituted as an executable function.Parameters:
1
function
Returns:
string representationstring
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 403
-
string.ends_with(
subject stringstring
,
substringstring
)
-
Returns
true
if the subject string ends with the supplied substring, orfalse
othewise.Parameters:
1
subject string
2
substring
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 409
-
string.find(
stringstring
,
substringstring
, [
start indexnumber
])
-
Returns the position of the first occurrence of a supplied substring in the subject string. If the supplied substring is found within the subject string then the start and end character positions of the substring are returned.
nil
is returned if the substring is not found.
This implementation oflen
is provided by our game code to support utf8 strings. It does not support pattern matching - seestring.find_lua
for the original language implementation which does.Parameters:
1
Subject string.
2
String pattern to search for.
3
optional, default value=1
Position of character at which to start the search. Supply a negative number to specify a character from the end of the string, counting back.
Returns:
Example:
local str = "This is a test string"
print(string.find(str, "test"))
11 14
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 418
-
string.find_lua(
stringstring
,
substringstring
, [
start indexnumber
], [
disable pattern matchingboolean
])
-
Returns the position of the first occurrence of a supplied substring in the subject string. If the supplied substring is found within the subject string then the start and end character positions of the substring are returned.
nil
is returned if the substring is not found.
This is the original lua language implementation oflen
, which does not support utf8 strings but which does support pattern matching. See the lua wiki for more information about lua patterns.Parameters:
1
Subject string.
2
String pattern to search for.
3
optional, default value=1
Position of character at which to start the search. Supply a negative number to specify a character from the end of the string, counting back.
4
optional, default value=false
Disables pattern matching if set to
true
.Returns:
Example:
local str = "this is a test string"
print(string.find_lua(str, "%s")); -- find first space
5 5
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 431
-
string.format(
container stringstring
, ...
values to insert)
-
Returns a formatted string from the formatting string and then arguments provided, in a similar style to the C function
printf
. The formatting string may contain the following special characters:
The function will throw an error if it's unable to convert the number specified into an integer value (should one be expected).Character Data Type Description
%c
number
(character code)The supplied numerical character code (see string.byte
) will be converted into its string representation.%d
number
(integer)The supplied integer, to be preceded by up to up to seven leading zeroes, the number of which may optionally be specified alongside the special character e.g. %04d
. If no number is specified then the integer is included in the returned string as it was given.%e
number
The supplied number will be formatted as an exponent, with the output in lower-case. %E
number
The supplied number will be formatted as an exponent, with the output in upper-case. %f
number
The specified number will be formated as a floating-point value. A specific format for the number may optionally be specified alongside the special character e.g. %4.1f would specify that the floating point number should be formatted with four digits preceding the decimal point and one digit following it. %g
number
The specified number will be formated as a compact floating-point value, or as an exponent (if too many digits) in lower-case. %G
number
The specified number will be formated as a compact floating-point value, or as an exponent (if too many digits) in upper-case. %i
number
(integer)The supplied integer value will be formatted as a signed integer. %o
number
(integer)The supplied integer value will be formatted as an octal value. %q
string
The supplied string will be enclosed in strings (as a quotation) when returned. %s
string
A string value. %u
number
(integer)The supplied value will be formatted as an unsigned integer. %x
number
(integer)The supplied value will be formatted as a hexadecimal integer in lower-case. %X
number
(integer)The supplied value will be formatted as a hexadecimal integer in upper-case. Parameters:
1
String containing special characters to insert values into.
2
...
One or more values to insert into the container string, in the order that the special characters are found.
Returns:
resultstring
Example - Inserting character codes with %c:
local str = string.format("hello %c %c %c, pleased to meet you", 65, 66, 67)
print(str)
hello A B C, pleased to meet you
Example - Specifying the string length of an integer with %d:
local str = string.format("These integers will be displayed with at least 5 digits: %05d %05d %05d", 12, 1234, 123456)
print(str)
These integers will be displayed with at least 5 digits: 00012 01234 123456
Example - Lower/Upper-case exponents with %e and %E:
local str = string.format("Exponents: %e %E", 1234.56, 1234.56)
print(str)
Exponents: 1.234560e+03 1.234560E+03
Example - Floating point values with %f:
local str = string.format("Floating point values: %f %3.1f", 123456.78, 123456.78)
print(str)
Floating point values: 123456.780000 123456.8
Example - Compact floating point values with %g and %G:
local str = string.format("Compact floating point values: %g %g %G", 123456, 12345678, 12345678)
print(str)
Compact floating point values: 123456 1.23457e+07 1.23457E+07
Example - Signed, Unsigned, Octal and Hexadecimal integers:
local str = string.format("Signed: %i, Unsigned: %u, Octal: %o, Hex: %x", -100, -100, -100, -100)
print(str)
Signed: -100, Unsigned: 4294967196, Octal: 37777777634, Hex: ffffff9c
Example - Strings:
local str = string.format("Unquoted: %s, quoted: %q", "test", "test")
print(str)
Unquoted: test, quoted: "test"
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 445
-
string.gmatch(
subject stringstring
,
patternstring
)
-
Returns a pattern-finding iterator. More information about iterators and lua string patterns may be found externally - see
String Patterns
.Parameters:
1
subject string
2
pattern
Returns:
iterator
Example:
local str = "This is a test string"
local wordcount = 0
for word in string.gmatch(str, "%a+") do
wordcount = wordcount + 1
end
print(string.format("%q contains %d words", str, wordcount))
"This is a test string" contains 5 words
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 496
-
string.gsub(
subjectstring
,
patternstring
,
replacementstring
, [
countnumber
])
-
This function takes a subject string, a pattern string and a replacement string, and performs a search based on the pattern string within the subject string. Should any parts of the pattern match, those parts of the subject string are replaced with the replacement string. The resulting string is then returned. An optional count argument may also be specified to limit the number of pattern replacements that may be performed.
Parameters:
1
Subject string.
2
Pattern string. More information about lua patterns may be found here:
String Patterns
3
Replacement string.
4
optional, default value=nil
Maximum number of times the replacement can be performed. If left unset, then no maximum is applied.
Returns:
resultstring
Example:
-- replace all spaces with underscores
result = string.gsub("this is a test string", " ", "_")
print(result)
this_is_a_test_string
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 510
-
string.len(
inputstring
)
-
Returns the number of characters in the supplied string. This implementation of
len
is provided by our game code, seestring.len_lua
for the original language implementation.Parameters:
1
input
Returns:
lengthnumber
Example:
str = "hello"
print(str:len())
5
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 523
-
string.len_lua(
inputstring
)
-
Returns the number of characters in the supplied string. This is the original lua language implementation of
len
, which does not support utf8 strings.Parameters:
1
input
Returns:
lengthnumber
Example:
str = "hello"
print(str:len_lua())
5
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 532
-
string.lower(
inputstring
)
-
Returns the supplied string, converted to lowercase.
Parameters:
1
input
Returns:
converted stringstring
Example:
str = "A Test String"
print(string.lower(str))
a test string
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 541
-
string.match(
subjectstring
,
patternstring
, [
start characternumber
])
-
Returns a substring of the supplied string, by a supplied pattern. An optional index may also be used to specify a character at which the search should be started.
More information about patterns may be found here:String Patterns
.Parameters:
1
Subject string to search.
2
Search pattern.
3
optional, default value=1
Start character within the subject string.
Returns:
matching stringstring
Example:
str = "THIS WORD here IS LOWERCASE"
print(string.match(str, "%l+"))
here
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 550
-
string.rep(
sourcestring
,
countnumber
)
-
Generates and returns a string which is a supplied number of copies of the supplied string, all concatenated together.
Parameters:
1
source
2
count
Returns:
resultstring
Example:
print(string.rep("test", 3))
testtesttest
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 562
-
string.reverse(
inputstring
)
-
Returns the supplied string with the character order reversed.
Parameters:
1
input
Returns:
reversed stringstring
Example:
print(string.reverse("forward"))
drawrof
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 571
-
string.starts_with(
subject stringstring
,
substringstring
)
-
Returns
true
if the subject string starts with the supplied substring, orfalse
othewise.Parameters:
1
subject string
2
substring
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 579
-
string.sub(
inputstring
,
start indexnumber
, [
end indexnumber
])
-
Returns a section of the supplied string, specified by start and (optionally) end character positions. The substring will include the characters specified by the start and end positions.
This implementation ofsub
is provided by our game code, seestring.sub_lua
for the original language implementation.Parameters:
1
Subject string.
2
Position of the starting character of the substring. If a negative number is specified then the function counts back from the end of the string.
3
optional, default value=nil
Position of the end character of the desired substring. If omitted, then the end of the supplied string is used as the end of the substring. If a negative number is specified then the function counts back from the end of the string to find this character.
Returns:
nil
Example - From character 11 until the end:
str = "this is a test string"
print(string.sub(str, 11))
test string
Example - From characters 11 through to 14 :
str = "this is a test string"
print(string.sub(str, 11, 14))
test
Example - From 13 characters from the end, onwards:
str = "this is a test string"
print(string.sub(str, -13))
a test string
Example - From 13 characters from the end until 9 from the start:
str = "this is a test string"
print(string.sub(str, -13, 9))
a
Example - From 13 characters from the end until 8 from the end:
str = "this is a test string"
print(string.sub(str, -13, -8))
a test
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 588
-
string.sub_lua(
inputstring
,
start indexnumber
, [
end indexnumber
])
-
Returns a section of the supplied string, specified by start and (optionally) end character positions. The substring will include the characters specified by the start and end positions.
This is the original lua language implementation ofsub
, which does not support utf8 strings.Parameters:
1
Subject string.
2
Position of the starting character of the substring. If a negative number is specified then the function counts back from the end of the string.
3
optional, default value=nil
Position of the end character of the desired substring. If omitted, then the end of the supplied string is used as the end of the substring. If a negative number is specified then the function counts back from the end of the string to find this character.
Returns:
nil
Example - From character 11 until the end:
str = "this is a test string"
print(string.sub_lua(str, 11))
test string
Example - From characters 11 through to 14 :
str = "this is a test string"
print(string.sub_lua(str, 11, 14))
test
Example - From 13 characters from the end, onwards:
str = "this is a test string"
print(string.sub_lua(str, -13))
a test string
Example - From 13 characters from the end until 9 from the start:
str = "this is a test string"
print(string.sub_lua(str, -13, 9))
a
Example - From 13 characters from the end until 8 from the end:
str = "this is a test string"
print(string.sub_lua(str, -13, -8))
a test
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 616
-
string.split(
subject_strstring
,
split_strstring
)
-
Returns a list representing the specified subject_str split up by occurances of split_str, not including those occurances.
An empty table is returned if no occurances of split_str are found.Parameters:
1
The string to be split.
2
The character or string to split the subject string at.
Returns:
nil
Example - Splitting a string by spaces:
string.split("The quick brown fox", ' ')
{ "The", "quick", "brown", "fox" }
Example - Splitting a string with another string (note that the pattern matches at its earliest possible location):
{ "Bananas: Price ", "-- £150" }
Example - Trimming the start and end occurances of spaces:
string.split("NoSpacesHere", ' ')
{ "NoSpacesHere" }
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 644
-
string.upper(
inputstring
)
-
Returns the supplied string, converted to uppercase.
Parameters:
1
input
Returns:
converted stringstring
Example:
str = "A Test String"
print(string.upper(str))
A TEST STRING
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 677
Functions
Functions are chunks of script that, when executed, perform one or more operations and then return control to the script that called them. Function declarations begin with the function
keyword, and end with a corresponding end
keyword. They are useful for encapsulating tasks that need to be performed repeatedly.
Functions can optionally take one or more argument values from the calling script. This allows a single function to produce different results when run repeatedly, based on its input values. Functions may also optionally return one or more values to the script that called them.
Functions in lua are first-class values
, which means that they may be assigned and re-assigned to variables in exactly the same manner as number
values, string
values and so on. As with other variables types in lua it's possible for functions to be anonymous, where they are created without a name.
Once declared, a function can be called and executed in script by appending open and close parenthesis characters to its name in the form <function_name>(arg1, arg2)
. See the examples below.
Example - Simple function declaration and execution:
-- declare a function variable called test_function that takes no arguments
function test_function()
print("test_function() called");
end
-- call test_function, after it has been declared
test_function()
test_function() called
Example - Alternative form of function declaration:
This alternative form of function declaration is identical to the previous example. It better illustrates the nature of functions as just another type of variable, created by assignment like a string or number.
-- declare a function variable called test_function that takes no arguments
test_function = function()
print("test_function() called");
end
-- call test_function, after it has been declared
test_function()
test_function() called
Example - Function taking two arguments:
Arguments can be specified in the function specification as follows. The arguments will be accessible as local variables throughout the lifetime of the function.
-- declare a function that takes two arguments called 'first' and 'second'
f = function(first, second)
print("f() called");
print("\tfirst is " .. tostring(first));
print("\tsecond is " .. tostring(second));
end
-- call f
f("hello", "goodbye")
f("lonely") -- deliberately not supplying a second arg
f(nil, "lonely") -- deliberately not supplying a first arg
f() called
first is hello
second is goodbye
f() called
first is lonely
second is nil -- value of unsupplied arguments is nil
f() called
first is nil
second is lonely
Example - Function returning two values:
function get_1080p_screen_resolution()
return 1920, 1080;
end
local x, y = get_1080p_screen_resolution()
print("1080p screen resolution is [" .. x .. ", " .. y .. "]")
1080p screen resolution is [1920, 1080]
Example - Example of passing functions as arguments to another function:
This includes an example of an anonymous function.
-- this function takes an argument and prints whether it's a function or not
function is_function(name, obj)
if type(obj) == "function" then
print(name .. " is a function");
else
print(name .. " is not a function");
end
end
a_number = 5
a_function = function() print("hello!") end
-- make calls to is_function. First arg is string name, second arg is value to test
is_function("a_number", a_number)
is_function("a_function", a_function)
-- pass through an anonymous function
is_function("anonymous_function", function() print("this is an anonymous function!") end)
-- pass is_function itself into is_function...
is_function("is_function", is_function)
a_number is not a function
a_function is a function
anonymous_function is a function
is_function is a function
A function can also take a variable number of arguments in a vararg
structure. Using varargs, an unlimited number of arguments can be supplied to a function, which accesses them as elements in a table
created within the function block called arg
. The number of arguments can be accessed at the special element n
within the arg
table. See the examples below.
Example:
-- the ... argument in the function declaration specifies that the function takes a vararg
function print_all(...)
for i = 1, arg.n do
print("arg " .. i .. " is " .. tostring(arg[i]))
end
end
-- call print_all() with as many arguments as we like
print_all("hello", "goodbye", 1, 2, 3)
arg 1 is hello
arg 2 is goodbye
arg 3 is 1
arg 4 is 2
arg 5 is 3
Tables
Tables are the sole container type in lua, and as such are the mechanism available for building complex data structures. They are associative arrays, which means they store sets of key/value pairs. A value
in a table is stored in a record corresponding to a key
, and that value may later accessed or modified using that same key. Keys are most commonly number
or string
values, but may be of any type other than nil
. Values stored against keys can be of any type, including function
or table
(allowing tables to be nested), although a value of nil
is the same as the value/record not existing.
Elements in a table may be accessed using []
square brackets, or by using the .
accessor if the key is a string, as shown in the examples below:
Example:
-- assign a string value to table t at a numeric key
t[5] = "hello"
-- assign a number value at a string key
t["age"] = 25
-- assign a value to a key, but where we evaluate the key from another variable
key_name = "hair_colour"
t[key_name] = "brown"
-- alternative method for assigning to a string key within a table
t.name = "Peter"
-- access elements in the table (using a variety of methods)
print(t[name] .. " is " .. t[age] .. " years old, his hair colour is " .. t.hair_colour)
Peter is 25 years old, his hair colour is brown
Before a table can be used it must be created with a table constructor, which is a pair of {}
braces. Values, or key/value pairs, can be included within the constructor so that they are stored in the table from creation. See the examples below:
Example - Create an empty table:
t = {}
Example - Create a table containing a list of values:
Each new value is inserted into the table at the first available integer key, starting at 1.
-- The value "first" is stored with a key of 1
-- The value "second" is stored with a key of 2
-- .. and so on
t = { "first", "second", "third", "fourth" }
Example - Create a table with key/values:
t = { [10] = "ten", [20] = "twenty", [30] = "thirty" }
Example - Create a table with key/values, but where the key is evaluated:
key_name = "title"
t = { [key_name] = "doctor" }
print(t.title)
doctor
Tables can be traversed by numeric sequence if their keys are arranged accordingly. The size of a table where values are assigned at ascending numeric integer keys can be accessed with the #
operator. This reports the first integer key n
for which no value is stored at key n + 1
.
Example - Examples of #t:
t1 = { "yes", "no", "maybe", "sometimes", "rarely" } -- values are stored at ascending keys
t2 = { [1] = "hello", [2] = "bonjour", [3] = "gutentag", [5] = "salut" } -- missing value stored at key 4
print("size of t1: " .. #t1)
print("size of t2: " .. #t2)
size of t1: 5
size of t2: 3
Example - Traversal by numeric sequence:
t = { "my", "enormous", "green", "boots", "dont", "fit" }
for i = 1, #t do
print("\tkey: " .. i .. ", value: " .. t[i])
end
key: 1, value: my
key: 2, value: enormous
key: 3, value: green
key: 4, value: boots
key: 5, value: dont
key: 6, value: fit
The iterator functions ipairs
and pairs
are available for iterating over tables where standard traversal may not be possible or desirable.
The ipairs
iterator behaves much the same as traversing over a table in numeric sequence. The main difference is that local variables for the key and optionally the value are automatically created. The ipairs
iterator stops at the first integer key to which a value is not assigned.
Unlike ipairs
and numeric traversal, the pairs
iterator allows traversal over all elements in a table, regardless of key type or any "gaps" in the data. The main caveat to usage is that the order of traversal is not defined, and can change between iteration instances. As such, the pairs
iterator is unsafe to use in multiplayer scripts as two different machines will traverse the same table in different orders.
Example - ipairs example:
t = { [1] = "first", [2] = "second", [3] = "third", [5] = "fifth" } -- gap at 4
for key, value in ipairs(t) do
-- local variables called 'key' and 'value' are automatically created each loop cycle
print("key: " .. key .. ", value: " .. value);
end
key: 1, value: first
key: 2, value: second
key: 3, value: third
Example - pairs example:
Note how the order of the output is arbitrary.
-- table with keys of lots of different types - impossible to traverse with #t or ipairs
t = {
[1] = "first",
["greeting"] = "hello",
[5] = "fifth",
[10] = "tenth",
[true] = "this has a boolean key",
[function() print("hello") end] = "This has a function key! Weird but possible"
}
for key, value in pairs(t) do
print("key: " .. tostring(key) .. ", value: " .. tostring(value));
end
key: 1, value: first
key: greeting, value: hello
key: function: 0x2194d10, value: This has a function key! Weird but possible
key: 5, value: fifth
key: true, value: this has a boolean key
key: 10, value: tenth
Lua provides a library of operations that can be performed on tables, listed below. More detailed information about these functions may be found on the dedicated page on lua-users.org
here.
The functions may be called in the form table.<function>(<t>, <arguments>)
.
-
table:concat(
tabletable
, [
separatorstring
], [
start elementnumber
], [
end elementnumber
])
-
Concatenates the values in the supplied table into a
string
, with optional separator characters, and returns that string. This can be a memory-efficient way of concatenating large amounts of strings together.Parameters:
1
Table to concatenate.
2
optional, default value=""
Separator string to insert between elements from the table.
3
optional, default value=1
Table element at which to start concatenating.
4
optional, default value=<table_size>
Table element at which to finish concatenating.
Returns:
nil
Example:
t = {"first", "second", "third", "fourth", "fifth"}
print(table.concat(t))
print(table.concat(t, " ", 2, 4))
firstsecondthirdfourthfifth
second third fourth
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 923
-
table:insert(
tabletable
, [
positionnumber
], obj
value)
-
Inserts a value into the supplied table. The index at which to insert the value may optionally be specified - note that this can change the sequence of arguments.
If no position argument is specified then the first available integer key in ascending order is chosen, starting from1
.Parameters:
1
Table to insert element in to.
2
optional, default value=<table_size>
Position at which to insert value. Note that this may be omitted, in which case the value to insert should be specified as the second argument. In this case the first available integer key is chosen, ascending from
1
.3
obj
Value to insert. If a third argument is omitted, the second is taken as the value to insert.
Returns:
nil
Example:
t = {1, 2, 3}
table.insert(t, "hello") -- insert "hello" as the last element in the table
table.insert(t, 3, "goodbye") -- insert "goodbye" as the third element in the table
print(table.concat(t, " "))
1 2 goodbye 3 hello
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 936
-
table:maxn(
tabletable
)
-
Returns the largest positive numeric index within the supplied table at which a value is assigned. Unlike the
#
operator, this will not stop counting if a gap in the integer sequence is found.Parameters:
1
table
Returns:
highest indexnumber
Example:
t = {}
t[1] = "goodbye"
t[2] = "adios"
t[4] = "auf"
-- no t[5]
t[6] = "la revedere"
t["8"] = "au revoir" -- not counted as the key is a string
print("table.maxn(t) is " .. table.maxn(t))
print("#t is " .. #t)
table.maxn(t) is 6
#t is 4
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 949
-
table:remove(
tabletable
, [
positionnumber
])
-
Removes the element from the supplied table at the supplied numeric index.
Parameters:
1
Table.
2
optional, default value=<table_size>
Position within table of element to remove.
Returns:
nil
Example:
t = {1, 2, 3, 4, 5}
table.remove(t) -- remove element at the end of the table
table.remove(t, 2) -- remove second element from the table
print(table.concat(t))
134
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 966
-
table:sort(
tabletable
, [
comparisonfunction
])
-
Sorts the elements in the supplied table into a new order. A comparison function may be supplied, which should take two table elements as arguments and return
true
if the first of these elements should appear in the final sorted order before the second, orfalse
if the opposite is desired. The table is sorted into ascending numerical order by default, which is the equivalent of supplying the sort functionfunction(a, b) return a < b end
.Parameters:
1
Table to sort.
2
optional, default value=<ascending_sort_order>
Comparison function.
Returns:
nil
Example - Default ascending sort order:
t = {4, 5, 3, 1, 8, 10}
table.sort(t)
print(table.concat(t, " "))
1 3 4 5 8 10
Example - Reverse sort order:
t = {4, 5, 3, 1, 8, 10}
table.sort(t, function(a, b) return a > b end)
print(table.concat(t, " "))
10 8 5 4 3 1
Example - Sorting a table of strings into length order:
t = {"very_long", "short", "longer", "really_really_long"}
table.sort(t, function(a, b) return string.len(a) < string.len(b) end)
print(table.concat(t, "..."))
short...longer...very_long...really_really_long
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 977
-
table:tostring(
tabletable
,
for campaign savegameboolean
, [
max depthnumber
], [
tab levelnumber
])
-
Converts a table into a
string
representation of that table for debug output. Note that this function is not provided with lua but is provided by Total War's script libraries.Parameters:
1
Subject table.
2
Set to
true
3
optional, default value=3
Maximum depth. This is the maximum depth of tables-within-tables to which the function will descend. Supply
-1
to set no maximum depth, which makes an infinite loop a possibility.4
optional, default value=0
Starting tab level. This number of tabs will be prepended to the start of each new line in the output.
Returns:
table to stringstring
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1001
-
table:contains(
tabletable
, value
value)
-
Reports if the supplied table contains the supplied value. If the table does contain the supplied value, the corresponding key at which it may be found is returned, otherwise
false
is returned.Parameters:
1
table
2
value
value
Returns:
value
key corresponding to value
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1107
-
table:is_empty(
tabletable
)
-
Returns whether the supplied table is empty.
Parameters:
1
Subject table.
Returns:
is emptyboolean
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1126
-
table:copy(
tabletable
)
-
Returns a deep copy of the supplied table. All subtables are copied. Metatables are not copied. Cyclical references are preserved.
Parameters:
1
Subject table
Returns:
copytable
Example - Loading faction script:
This script snippet requires the path to the campaign faction folder, then loads the "faction_script_loader" script file, when the game is created.my_table = { fruit = "apple", topping = { flavour = "cinnamon" } }
copied_table = table.copy(my_table)
copied_table.fruit = "banana"
copied_table.topping.flavour = "chocolate"
print(my_table.fruit .. " topped with " .. my_table.topping.flavour)
print(copied_table.fruit .. " topped with " .. copied_table.topping.flavour)
apple topped with cinnamon
banana topped with chocolate
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1139
-
table:mem_address(
objecttable
, [
leave punctuationboolean
])
-
Returns the memory address portion of the result of calling tostring() with a table argument. The function will fail if the supplied table has a metatable that's write-protected.
If the leave punctuation flag is set then the memory address is supplied back still with its leading colon and space characters.Parameters:
1
object
2
optional, default value=false
leave punctuation
Returns:
address stringstring
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1185
-
table:indexed_to_lookup(
indexed tabletable
)
-
Returns another table containing all records in the supplied indexed table, except with the values set to keys. The values in the returned table will be
true
for all records.Parameters:
1
indexed table
Returns:
lookup tabletable
Example:
indexed_t = {"oranges", "apples", "strawberries"}
lookup_t = table.indexed_to_lookup(indexed_t)
for key, value in pairs(lookup_t) do
print(key, value)
end
strawberries true
oranges true
apples true
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1226
-
table:compile_tables(
table_listtable
, [rules
(fully recursive])
-
Given a list of tables, sorts all tables by an optional
load_order
and then merges each one onto the previous successively. Can be used to merge and override data tables for mod compatibility.Parameters:
1
The list of tables to be merged, indexed numerically.
2
rules
optional, default value=default rules (fully recursive, allow overrides)
allow overrides)]
table
merge_rules, A table describing the rules for merging, includingrecursion_levels
andallow_overrides
.Returns:
Returns the fully compiled table (a new table reference).table
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1291
-
table:merge_tables(
merge_totable
,
merge_fromtable
,
merge_rulestable
, [
recursion_levelnumber
])
-
Merge two tables given a set of merge rules, implanting the data from
merge_from
intomerge_to
.Parameters:
1
The table to which data will be merged. The return value is this table, following the merge.
2
The table from which data will be merged.
3
A table describing the rules for merging, including
recursion_levels
andallow_overrides
.4
optional, default value=1
The number of recursions this function call is at, i.e. the number of table layers into the original table that the merge has reached. If unspecified, this begins at 1.
Returns:
Returns the original merge_to table, but with the contents of merge_from incoorporated into it.table
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1320
Userdata
Userdata is an area of memory accessible to lua but created by the host program. In our case, the host program is the game executable. It allows the host program to package up code functionality in a black box that can be used in fixed ways script.
The interface objects supplied to script by the game in campaign and battle are typically userdata
. It's not possible for script to meaningfully modify or query userdata objects in ways that weren't specifically set up by the creating code.
Math
The math
library provides mathematical functions and values in a table
that be called from within lua.
The math libraries provides the following constants that can be looked up:
Variables in this section:
These variables may be accessed via <interface>.<variable_name>
:
pi | number | Value of Pi. |
huge | number | The value HUGE_VAL, a value equal to or larger than any other numerical value. |
-
math.abs(
valuenumber
)
-
Returns the absolute of the supplied value, converting it to a positive value if negative.
Parameters:
1
value
Returns:
absolute valuenumber
Example:
print(math.abs(-4))
4
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1402
-
math.acos(
valuenumber
)
-
Returns the arc cosine of the supplied value, in radians. The supplied value should be between -1 and 1.
Parameters:
1
value
Returns:
acos value in radiansnumber
Example:
print(math.acos(0.5))
1.0471975511966
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1410
-
math.asin(
valuenumber
)
-
Returns the arc sine of the supplied value, in radians. The supplied value should be between -1 and 1.
Parameters:
1
value
Returns:
asin value in radiansnumber
Example:
print(math.asin(0.5))
0.5235987755983
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1418
-
math.atan(
valuenumber
)
-
Returns the arc tangent of the supplied value, in radians.
Parameters:
1
value
Returns:
atan value in radiansnumber
Example:
print(math.atan2(1))
0.64350110879328
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1426
-
math.atan2(
oppositenumber
,
adjacentnumber
)
-
Returns the arc tangent of the supplied opposite value divided by the supplied adjacent value, in radians. The sign of both arguments is used to find the quadrant of the result.
Parameters:
1
opposite
2
adjacent
Returns:
atan value in radiansnumber
Example:
print(math.atan2(5, 5))
print(math.atan2(-5, 5))
0.78539816339745
-0.78539816339745
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1434
-
math.ceil(
valuenumber
)
-
Returns the smallest integer that is larger than or equal to the supplied value.
Parameters:
1
value
Returns:
ceil valuenumber
Example:
print(math.ceil(2.2))
3
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1445
-
math.cos(
valuenumber
)
-
Returns the cosine of the supplied radian value.
Parameters:
1
value
Returns:
cosine valuenumber
Example:
print(math.cos(1.0471975511966))
0.5
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1453
-
math.cosh(
valuenumber
)
-
Returns the hyperbolic cosine of the supplied value.
Parameters:
1
value
Returns:
hyperbolic cosine valuenumber
Example:
print(math.cosh(1))
1.5430806348152
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1461
-
math.deg(
radian valuenumber
)
-
Converts the supplied radian value into an angle in degrees. See also
math.rad
.Parameters:
1
radian value
Returns:
value in degreesnumber
Example:
print(math.deg(math.pi))
180.0
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1469
-
math.exp(
exponentnumber
)
-
Returns the numerical constant
e
to the power of the supplied value. Supply a value of1
to just returne
.Parameters:
1
exponent
Returns:
e ^ exponentnumber
Example:
print(math.exp(1))
2.718281828459
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1477
-
math.floor(
valuenumber
)
-
Returns the largest integer that is smaller than or equal to the supplied value.
Parameters:
1
value
Returns:
floor valuenumber
Example:
print(math.floor(2.2))
2
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1485
-
math.fmod(
dividendnumber
,
divisornumber
)
-
Returns remainder of the division of the first supplied value by the second supplied value.
Parameters:
1
dividend
2
divisor
Returns:
floor valuenumber
Example:
-- 5 * 4 is 20, leaving a remainder of 3 to reach 23
print(math.fmod(23, 5))
3
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1493
-
math.frexp(
x valuenumber
)
-
Returns the values of
m
andexp
in the expressionx = m * 2 ^ exp
, wherex
is the value supplied to the function.exp
is an integer and the absolute value of the mantissam
is in the range 0.5 - 1 (or zero whenx
is zero).Parameters:
1
x value
Returns:
Example:
print(math.frexp(10)))
0.625 4
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1503
-
math.ldexp(
mnumber
,
expnumber
)
-
Returns
m * 2 ^ exp
, where the mantissam
and exponentexp
are values supplied to the function.exp
should be an integer value.Parameters:
1
m
2
exp
Returns:
m * 2 ^ expnumber
Example:
print(math.ldexp(2, 4))
32.0
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1512
-
math.log(
valuenumber
)
-
Returns the natural logarithm of the supplied value.
Parameters:
1
value
Returns:
log valuenumber
Example:
print(math.log(10))
2.302585092994
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1521
-
math.log10(
valuenumber
)
-
Returns the base-10 logarithm of the supplied value.
Parameters:
1
value
Returns:
log valuenumber
Example:
print(math.log(10))
1.0
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1529
-
math.max(...
values)
-
Returns the maximum numeric value amongst the arguments given.
Parameters:
1
...
Returns:
max valuenumber
Example:
print(math.max(12, 10, 14, 3, 8, 13))
14
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1537
-
math.min(...
values)
-
Returns the minimum numeric value amongst the arguments given.
Parameters:
1
...
Returns:
min valuenumber
Example:
print(math.min(12, 10, 14, 3, 8, 13))
3
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1545
-
math.modf(
input valuenumber
)
-
Returns the integral part of the supplied value and the fractional part of the supplied value.
Parameters:
1
input value
Returns:
Example:
print(math.modf(5))
print(math.modf(5.4))
5 0.0
5 0.4
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1553
-
math.normalize(
valuenumber
, [
minimumnumber
], [
maximumnumber
])
-
Scales a supplied value to between supplied minimum and maximum values.
Parameters:
1
value
2
optional, default value=0
minimum
3
optional, default value=1
maximum
Returns:
normalized valuenumber
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1564
-
math.pow(
xnumber
,
ynumber
)
-
Returns the first supplied number value to the power of the second supplied number value.
Parameters:
1
x
2
y
Returns:
x ^ ynumber
Example:
print(math.pow(2, 4))
16.0
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1577
-
math.rad(
degree valuenumber
)
-
Converts the supplied angle in degrees into an angle in radians. See also
math.deg
.Parameters:
1
degree value
Returns:
value in radiansnumber
Example:
print(math.rad(180))
3.1415926535898
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1586
-
math.random([
first limitnumber
], [
second limitnumber
])
-
Provides an interface to the pseudo-random number generator provided by ANSI C. This function returns a random number between two optionally-supplied limits. If no arguments are supplied, those limits are
0
and1
. If one argumenta
is supplied, those limits are1
anda
. If two argumentsa
andb
are supplied then those limits area
andb
.
If no arguments are supplied the returned value is real, whereas if any arguments are supplied the returned value is an integer.
Note that use of this function is discouraged, as it will generate different results on different clients in a multiplayer game. Acting upon the result of this function in multiplayer scripts will likely cause desyncs.Parameters:
1
optional, default value=nil
first limit
2
optional, default value=nil
second limit
Returns:
random numbernumber
Example:
print(math.random()) -- returned value will be 0..1
print(math.random(10)) -- returned value will be 1..10
print(math.random(5, 10)) -- returned value will be 5..10
0.84018771676347
4
9
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1594
-
math.randomseed(
seednumber
)
-
Sets the supplied value as the seed for the random number system.
Parameters:
1
seed
Returns:
nil
Example:
math.randomseed(os.clock())) -- use os clock as random seed
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1609
-
math.sin(
valuenumber
)
-
Returns the sine of the supplied radian value.
Parameters:
1
value
Returns:
sine valuenumber
Example:
print(math.sin(0.5235987755983))
0.5
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1615
-
math.sinh(
valuenumber
)
-
Returns the hyperbolic sine of the supplied value.
Parameters:
1
value
Returns:
hyperbolic sine valuenumber
Example:
print(math.sinh(0.5235987755983))
0.54785347388804
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1623
-
math.sqrt(
valuenumber
)
-
Returns the square root of the supplied value.
Parameters:
1
value
Returns:
square root valuenumber
Example:
print(math.sqrt(4))
2.0
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1631
-
math.tan(
valuenumber
)
-
Returns the tangent of the supplied radian value.
Parameters:
1
value
Returns:
tan valuenumber
Example:
print(math.tan(0.64350110879328))
0.74999999999999
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1639
-
math.tanh(
valuenumber
)
-
Returns the hyperbolic tangent of the supplied value.
Parameters:
1
value
Returns:
hyperbolic tan valuenumber
Example:
print(math.tanh(0.64350110879328))
0.56727870240097
defined in ../../warhammer/working_data/script/_lib/lib_lua_extensions.lua, line 1647