Available Standard Lua Functions
Besides of special ArmyCalc API (functions beginning with 'tw...'), there are a lot of standard Lua functions available in both validation and display scripts.
In some implementations your code will be run in a special environment (so called 'sandbox').
Not all standard Lua functions are safe to run in such environment. Thats why not all of them are available.
Following is an official list of functions with short usage descriptions that should be supported in any program implementing the TWR file format.
If you will use functions from this list your Ruleset should work without any problems in such program.
For details on any function see Lua manual at lua.org.
List Sections:
- assert( v [, message] )
Generates an error if value of v is nil or false.
- error( message [, level] )
Terminates function call and generates an error.
- ipairs( table )
Can be used to iterate over table elements like: for index,value in ipairs(table) do something() end. It will iterate only trough element with indexes like: 1, 2, 3, ..
- next( table [, index] )
Returns index and value of the first table element or element just after index when index is not given.
- pairs( table )
Can be used to iterate over table elements like: for index,value in ipairs(table) do something() end. It will iterate trough all elements, including indexed with strings etc.
- pcall( f [, arg1 , arg2 ..] )
Calls function f in protected mode. Can be used to catch errors.
- select( index, ... )
Returns arguments after index.
- tonumber( e [, base] )
Tries to convert e to number or returns nil.
- tostring( e )
Converts e (of any type) to string.
- type( v )
Returns the string describing type of v. One of: "nil", "number", "string", "boolean", "table", "function", "thread", and "userdata".
- unpack( list [, i [, j [...]]] )
Returns elements from the list.
- xpcall( f , err )
Similar to pcall but you can set your own error handler.
Coroutines allows script to do multiple tasks at once. Use of multi-threaded programing in Rulesets scripting would be pretty hard to find. However, this functions are marked as safe to run in sandbox so they are listed here for clearness sake.
Visit
Lua Coroutines Tutorial at Lua users wiki for details.
- coroutine.create()
- coroutine.resume()
- coroutine.running()
- coroutine.status()
- coroutine.wrap()
This functions should be also available in an object programming style. For example "string.rep( s , n )" equals "s:rep( n )".
Lua string chars and tables are indexed from 1.
- string.byte( s [, i [, j]])
Returns codes of characters from i to j.
- string.char( c1 [, c2 [...]] )
Returns string build from characters of codes c1 , c2 , ...
- string.find( s , pattern [, init [, plain]])
Returns nil or position of pattern in s. Starts searching from init index. If plain is given function do not use regexps.
- string.format( formatstring , ... )
Works similar to C printf() function. Returns formatstring with special characters like "%d" "%c" etc. replaced with function further arguments.
- string.gmatch( s , pattern )
Can be used to iterate trough all instances of pattern in s.
- string.gsub( s , pattern , repl [, n])
Returns copy of s with all(or first n) occurrences of pattern replaced with repl.
- string.len( s )
Returns number of chars in string.
- string.lower( s )
Returns s with uppercase characters converted to lowercase.
- string.match( s , pattern [, init])
Finds pattern in s. Search from beginning or from init index.
- string.rep( s , n )
Returns string which is n times repetition of s.
- string.reverse( s )
Returns backward s.
- string.sub( s , i [, j])
Returns substring of s from index i to end or index j.
- string.upper( s )
Returns s with lowercase characters converted to uppercase.
- table.insert( table , [pos ,] value )
Inserts value as the next natural indexed element of table. If pos is given all table elements from pos are shifted one index up and tab[pos] is set to value.
- table.maxn( table )
Returns 0 or max numeric index of table.
- table.remove( table [, pos])
It sets table[pos]=nil and shifts all elements after pos one index down.
- table.sort( table [, comp])
Sorts all elements in table from 1 to table size. Uses standard Lua < operator to compare values or comp( arg1 , arg2 ) function if given.
math table gives you access to common mathematical operations. See
Lua Math Library Tutorial at Lua users wiki for details.
- math.abs( x )
Returns absolute value of x
- math.acos( x )
Arccosine of x
- math.asin( x )
Arcsine of x
- math.atan( x )
Arctangent of x
- math.atan2( y , x )
Arctangent of x/y
- math.ceil( x )
Returns smallest integer no smaller than x
- math.cos( x )
Cosine of x
- math.cosh( x )
Hyperbolic cosine
- math.deg( x )
Convert x from radians to degrees
- math.exp( x )
Returns 'e' to the power of x
- math.floor( x )
Returns biggest integer no greater then x
- math.fmod( x , y )
Returns x modulo y
- math.frexp( x )
Splits x into a normalised fraction and an exponent
- math.huge
Represents infinity. ( math.huge == (1/0) )
- math.ldexp( m , e )
Inverse function of math.frexp().
- math.log( x )
Natural logarithm of x
- math.log10( x )
Base 10 logarithm of x
- math.max( x , ... )
Returns the largest parameter
- math.min( x , ... )
Returns the smallest parameter
- math.modf( x )
Returns integral and fractional parts of x
- math.pi
Returns Pi
- math.pow( x , y )
Returns x to the power of y
- math.rad( s )
Converts x from degrees to radians
- math.random( [ [lower ,] upper] )
Without parameters generates real number between 0 and 1.
With upper given generates random integer between 1 and upper.
Otherwise generates random integer between lower and upper.
- math.sin( x )
Sine of x
- math.sinh( x )
Hyperbolic sine of x
- math.sqrt( x )
Square root of x
- math.tan( x )
Tangent of x
- math.tanh( x )
Hyperbolic tangent of x
- lua.org - biggest source of materials about Lua. Manuals, tutorials, users-wiki ..
- Google + "Lua tutorial"
- Example Rulesets - You can also watch example Rulesets in Your Rulesets section to see well commented examples of scripts.