Globals globals
Global functions of Flux
Classes for Lua: the class keyword with constructors, inheritance through extends and
super, and mixins through include.
Classes are plain tables stored under their ConstantStyle name. Instances are created with
Name.new(...), which copies the class into a new table and calls its init method.
delegate copies selected methods of a class to another object, and isclass tells
classes and their instances from other values.
Summary
Functions
-2 ^ 2 is -(2 ^ 2).Functions
Turns a table into a special array that has the functions of the table library as methods. The table itself is modified and returned.
local words = a{ 'one', 'two' }
words:insert('three')
local lengths = words:map(function(v) return v:len() end) -- { 3, 3, 5 }
Parameters
- initializer Map/List table to turn into a special array
Returns
- List the same table
Adds an environment variable to the clientside environment variables list. Only the first call for a key has an effect. Key and value are converted to strings. Serverside only, does nothing on the client.
Parameters
- key String variable name
- value String variable value
Development only
Only available in the development environment.
Registers an object under a debug metric. Metrics are only collected when FLUX_ENV is set to 'development'.
Parameters
- id String metric name
- obj Any object to register
Returns
- Number amount of objects registered under this metric so far
In other environments lib/stdlib/sh_debug.lua:63
Does nothing, since debug metrics are only collected in the development environment.
Parameters
- _id String metric name
- _obj Any object to register
Defined in
- lib/stdlib/sh_debug.lua:17 shared
- lib/stdlib/sh_debug.lua:63 shared
Creates a new class. Supports constructors and inheritance.
The class is stored in the global table, or inside of another table when the name is
namespaced with ::. Every class gets a new function that creates an instance and calls
its init constructor, and an include method that mixes a module into the class.
class 'Animal'
function Animal:init(name)
self.name = name
end
class 'Dog' extends 'Animal'
function Dog:init(name)
super(name) -- calls Animal's constructor on the new object
end
Dog:include 'Talkable' -- copies missing methods from the Talkable module
local dog = Dog.new('Rex')
Parameters
- name String class name in ConstantStyle, may be namespaced (e.g. 'ActiveRecord::Model')
- parent_class String/Map optional, defaults to
nilbase class or its name
Returns
- Map the created class
See also
Creates a color. Replaces the built-in Color function to also accept hexadecimal strings and CSS color names.
Color(255, 165, 0)
Color('#ffa500')
Color('orange')
Color('orange', 100) -- orange with the alpha of 100
Variants
Color(r, g, b, a)- r Number red, 0-255
- g Number green, 0-255
- b Number blue, 0-255
- a Number optional, defaults to
255alpha, 0-255
Color(r)- r String hexadecimal color that starts with '#'
Color(r, g)- r String CSS color name such as 'orange', case-insensitive
- g Number optional, defaults to
255alpha, 0-255
Returns
- Color the created color, white if the string is not a known color name
See also
Similar to the <=> operator in other languages.
Parameters
- a Number/String left value
- b Number/String right value, has to be comparable with a
Returns
- Number -1 if a < b; 0 if a == b; and 1 if a > b
Copies the listed methods of a class onto another object.
-- Makes MyLibrary.now and MyLibrary.tomorrow point to the DateTime functions.
delegate(MyLibrary, { 'now', 'tomorrow', to = 'DateTime' })
Parameters
- obj Map object or class that receives the methods
- t Map method names as array elements, the source class or its name in the
tokey
Returns
- Boolean true, or nil if obj or t is not a table or
tois missing
Creates enumerator variables based on a provided list. Starts at 0.
-- 0 1 2
enumerate 'GENDER_MALE GENDER_FEMALE GENDER_OTHER'
Parameters
- enums String space-separated enumerator names
- existing_enumerator String optional, defaults to
nilprefix of an earlier group (such as 'GENDER') to continue the numbering of, starting right after the highest enumerator of that group
Returns
- Number highest enumerator, or nil if enums is not a string or is empty
No documentation available.
Print an error message followed by a complete stack traceback. Unlike error, this does not stop the execution of the calling code.
Parameters
- msg String error message
Prints the error in bold on a red background using ANSI escape sequences. Trailing newlines are printed after the styling is reset.
Parameters
- msg String error message
Evaluates a syntax tree node, left to right.
Parameters
- node Table syntax tree node
- context Table evaluation context with
variables,rollsanddice
Returns
- Number the value
Sets the base class of the class that is currently being created.
Copies the base class into the class created by the last class call, then calls the
base class's class_extended(new_class) method and runs the OnClassExtended hook.
class 'DateTime' extends 'Date'
-- The base class can also be passed as a table.
class 'Dog' extends(Animal)
Parameters
- parent_class String/Map base class or its name
Returns
- Boolean whether the extension succeeded
See also
Development only
Only available in the development environment.
Returns all objects that were registered under a debug metric.
Parameters
- id String metric name
Returns
- List registered objects, or nil if nothing was registered under this metric
In other environments lib/stdlib/sh_debug.lua:68
Does nothing, since debug metrics are only collected in the development environment.
Parameters
- _id String metric name
Defined in
- lib/stdlib/sh_debug.lua:30 shared
- lib/stdlib/sh_debug.lua:68 shared
Returns the Steam name of a player, or the translated name of the console if the player is not valid (as it is for commands that are run from the server console).
Parameters
- target Player player, or an invalid entity or nil for the console
Returns
- String
Gets an environment variable matching the "key" name.
Parameters
- key String variable name
- default Any optional, defaults to
nilvalue to return if the variable is not set
Returns
- Any environment variable, or default if it is not set
No documentation available.
No documentation available.
Makes a Lua pattern case-insensitive.
inflect:plural(i'(octop)us$', '%1i') -- the pattern becomes '([oO][cC][tT][oO][pP])[uU][sS]$'
Parameters
- pattern String Lua pattern
Returns
- String pattern that matches both lowercase and uppercase letters
Runs a Lua file in a restricted environment that has no access to unsafe globals such as debug, require, RunString, CompileString, setmetatable or File. The environment is a copy of the globals made when the standard library was loaded.
Parameters
- f String file path relative to the lua/ folder
Returns
- Vararg whatever the file returns
Checks whether an object is a special array created with a.
Parameters
- obj Any
Returns
- Boolean
See also
Checks whether or not an object is a class or an instance of a class.
Parameters
- obj Any object to check
Returns
- Boolean whether or not the object is a class
See also
Linearly interpolates between two colors, alpha included.
Parameters
- fraction Number interpolation fraction, 0 returns color_from and 1 returns color_to
- color_from Color
- color_to Color
Returns
- Color
No documentation available.
No documentation available.
Prints an error using ErrorNoHalt but without the character limit.
Parameters
- ... Vararg strings that are concatenated into the error message
See also
No documentation available.
Create a module with a specified name, or return the module that already exists under
that name, so that a refreshed file keeps the state of its module.
The resulting object will have the include method by default.
mod 'Talkable'
-- You can include other modules too.
Talkable:include 'Living'
function Talkable:talk()
-- ...
end
Parameters
- name String module name in ConstantStyle, may be namespaced (e.g. 'Flux::Anim')
Returns
- Object created module
Parses binary operators by precedence climbing.
Parameters
- state Table parser state
- min_precedence Number lowest operator precedence this call may consume
Returns
- Table syntax tree node
Parses unary minus, so that -2 ^ 2 is -(2 ^ 2).
Parameters
- state Table parser state
Returns
- Table syntax tree node
Development only
Only available in the development environment.
Prints the amount of objects registered under a debug metric to the console. The metric has to exist.
Parameters
- id String metric name
- format String optional, defaults to
'{count} {id} were registered.'message, {count} and {id} are replaced
See also
In other environments lib/stdlib/sh_debug.lua:74
Does nothing, since debug metrics are only collected in the development environment.
Parameters
- _id String metric name
- _format String optional, defaults to
nilmessage
Defined in
- lib/stdlib/sh_debug.lua:40 shared
- lib/stdlib/sh_debug.lua:74 shared
Development only
Only available in the development environment.
Prints the amount of registered objects of every debug metric to the console.
In other environments lib/stdlib/sh_debug.lua:78
Does nothing, since debug metrics are only collected in the development environment.
Defined in
- lib/stdlib/sh_debug.lua:54 shared
- lib/stdlib/sh_debug.lua:78 shared
Prints the contents of a table to the console, sorted by key, with nested tables indented. A better implementation of PrintTable, which it also replaces.
Parameters
- t Map/List table to print
- indent Number optional, defaults to
0indentation level to print at - done Map optional, defaults to
{}tables that are being printed already, to avoid endless recursion - indent_length Number optional, defaults to
1minimum width of the key column
Print a traceback to the current function call.
Parameters
- suppress Boolean optional, defaults to
falsedo not print the traceback, only return it - ... Vararg arguments for debug.traceback, such as a message and a level
Returns
- List string pieces of the traceback
Prints C-style formatted strings.
Parameters
- str String format string, see string.format
- ... Vararg values to put into the format string
Require a clientside-only file. The .lua file ending may be omitted. This is the equivalent of simply doing this:
if SERVER then
AddCSLuaFile(file_name)
else
include(file_name)
end
Parameters
- file_name String file name to include
Returns
- Object return value of include()
Ignore a certain realm in the require_relative function calls.
require_ignore('server', true)
require_relative('sv_thing.lua') -- will not be included
require_ignore('server', false)
require_relative('sv_thing.lua') -- will be included
Parameters
- who String realm
- should_ignore Boolean whether the realm should be ignored or not
Includes a module and returns a boolean depending on success. Does not throw Lua errors.
Parameters
- mod String module name, as passed to require
Returns
- Boolean success
Require a file relative to the current file's directory. The .lua file ending may be omitted. This function will try to guess the realm the file should be in and AddCSLuaFile accordingly and automatically.
require_relative 'cl_file' -- will include cl_file.lua only on client
require_relative 'sv_file' -- will include sv_file.lua only on server
require_relative 'file' -- will include file.lua on both client and server
Parameters
- file_name String file to include
Returns
- Object return value of include()
Require all files in a folder using require_relative.
Relative to gamemodes/flux/ by default, unless base is specified. Files are included in
alphabetical order; when recursive, the subfolders follow them, also alphabetically.
require_relative_folder 'my_folder'
Parameters
- dir String folder to include
- base String optional, defaults to
flux/base folder for relativity - recursive Boolean optional, defaults to
falseinclude all files recursively
See also
Require a serverside-only file. The .lua file ending may be omitted. This is equivalent of simply doing this:
if SERVER then
include(file_name)
end
Parameters
- file_name String file name to include
Returns
- Object return value of include()
Creates a function that returns the given field of the table passed to it. For use with table#map.
table.map(t, s'some_field')
Parameters
- what String/Number key of the field
Returns
- Function accessor(tab), returns tab[what]
Sets an environment variable "key" to "value".
Parameters
- key String variable name
- value Any new value
Returns
- Any the new value
No documentation available.
A wrapper for pcall for shorthand writing. If the function fails, the error is printed with a traceback and nothing is returned.
Parameters
- func Function function to call
- ... Vararg arguments to call the function with
Returns
- Vararg up to six return values of the function
Removes the common indentation and the surrounding blank lines from a multi-line string, so that long texts can be indented together with the code around them.
print(txt[[
Usage:
flux help
]])
-- Usage:
-- flux help
Parameters
- text String multi-line text
Returns
- String the text without the common indentation
Gets the type of an object while ensuring the output is always lowercase.
Functions exactly the same as type.
Parameters
- obj Any
Returns
- String type
See also
type
No documentation available.
No documentation available.
Returns the material of an image from a URL, starting the download on first use. A placeholder material is returned for as long as the image has not been loaded.
Parameters
- url String direct link to an image, has to end with a file extension
Returns
- Material the downloaded image, or a placeholder while it is loading
See also
Splits a string of words into an array. Words are separated by spaces or newlines.
w'data ammo equipment' -- { 'data', 'ammo', 'equipment' }
Parameters
- str String space-separated words
Returns
- List<String> words
Splits a string of space-separated words into a hash that has the words as its keys.
wk'data ammo equipment' -- { data = true, ammo = true, equipment = true }
Parameters
- str String space-separated words
Returns
- Map word => true