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

Turns a table into a special array that has the functions of the table library as methods.
Adds an environment variable to the clientside environment variables list.
add_debug_metric(id, obj)shared development
Registers an object under a debug metric.
Creates a new class.
Creates a color.
Similar to the <=> operator in other languages.
Copies the listed methods of a class onto another object.
Creates enumerator variables based on a provided list.
Print an error message followed by a complete stack traceback.
Prints the error in bold on a red background using ANSI escape sequences.
Evaluates a syntax tree node, left to right.
Sets the base class of the class that is currently being created.
get_debug_metric(id)shared development
Returns all objects that were registered under a debug metric.
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).
Gets an environment variable matching the "key" name.
Makes a Lua pattern case-insensitive.
Runs a Lua file in a restricted environment that has no access to unsafe globals such as debug, require, RunString, CompileString, setmetatable or File.
is_a(obj)shared
Checks whether an object is a special array created with a.
Checks whether or not an object is a class or an instance of a class.
Linearly interpolates between two colors, alpha included.
Prints an error using ErrorNoHalt but without the character limit.
mod(name)shared
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.
Parses binary operators by precedence climbing.
Parses unary minus, so that -2 ^ 2 is -(2 ^ 2).
Prints the amount of objects registered under a debug metric to the console.
print_debug_metrics()shared development
Prints the amount of registered objects of every debug metric to the console.
Prints the contents of a table to the console, sorted by key, with nested tables indented.
Print a traceback to the current function call.
Prints C-style formatted strings.
Require a clientside-only file.
Ignore a certain realm in the require_relative function calls.
Includes a module and returns a boolean depending on success.
Require a file relative to the current file's directory.
Require all files in a folder using require_relative.
Require a serverside-only file.
Require a file on both client and server.
s(what)shared
Creates a function that returns the given field of the table passed to it.
Sets an environment variable "key" to "value".
sub(...)shared
A wrapper for pcall for shorthand writing.
txt(text)shared
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.
Gets the type of an object while ensuring the output is always lowercase.
Returns the material of an image from a URL, starting the download on first use.
w(str)shared
Splits a string of words into an array.
wk(str)shared
Splits a string of space-separated words into a hash that has the words as its keys.

Functions

a(initializer)

shared lib/stdlib/sh_table.lua:572

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

add_client_env(key, value)

shared gamemode/env.lua:48

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

add_debug_metric(id, obj)

shared development lib/stdlib/sh_debug.lua:17

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

class(name, parent_class)

shared lib/stdlib/sh_class.lua:53

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 nil base class or its name

Returns

  • Map the created class

See also

Color(r, g, b, a)

shared lib/stdlib/sh_color.lua:216

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 255 alpha, 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 255 alpha, 0-255

Returns

  • Color the created color, white if the string is not a known color name

See also

compare(a, b)

shared lib/stdlib/sh_utils.lua:391

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

delegate(obj, t)

shared lib/stdlib/sh_class.lua:164

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 to key

Returns

  • Boolean true, or nil if obj or t is not a table or to is missing

enumerate(enums, existing_enumerator)

shared lib/stdlib/sh_utils.lua:498

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 nil prefix 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

equal(a, b)

shared lib/stdlib/sh_utils.lua:273

No documentation available.

error_with_traceback(msg)

shared lib/stdlib/sh_utils.lua:458

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

ErrorNoHalt(msg)

server lib/stdlib/sh_wrappers.lua:35

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

evaluate_node(node, context)

shared lib/stdlib/sh_dice.lua:964

Evaluates a syntax tree node, left to right.

Parameters

  • node Table syntax tree node
  • context Table evaluation context with variables, rolls and dice

Returns

  • Number the value

extends(parent_class)

shared lib/stdlib/sh_class.lua:190

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

get_debug_metric(id)

shared development lib/stdlib/sh_debug.lua:30

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

get_player_name(target)

shared lib/stdlib/sh_utils.lua:235

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

getenv(key, default)

shared gamemode/env.lua:18

Gets an environment variable matching the "key" name.

Parameters

  • key String variable name
  • default Any optional, defaults to nil value to return if the variable is not set

Returns

  • Any environment variable, or default if it is not set

greater(a, b)

shared lib/stdlib/sh_utils.lua:282

No documentation available.

greater_equal(a, b)

shared lib/stdlib/sh_utils.lua:288

No documentation available.

i(pattern)

shared lib/stdlib/sh_string.lua:468

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

include_sandboxed(f)

shared lib/stdlib/sh_utils.lua:479

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

is_a(obj)

shared lib/stdlib/sh_table.lua:580

Checks whether an object is a special array created with a.

Parameters

  • obj Any

Returns

  • Boolean

See also

isclass(obj)

shared lib/stdlib/sh_class.lua:23

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

LerpColor(fraction, color_from, color_to)

shared lib/stdlib/sh_color.lua:305

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

less(a, b)

shared lib/stdlib/sh_utils.lua:279

No documentation available.

less_equal(a, b)

shared lib/stdlib/sh_utils.lua:285

No documentation available.

long_error(...)

shared lib/stdlib/sh_utils.lua:433

Prints an error using ErrorNoHalt but without the character limit.

Parameters

  • ... Vararg strings that are concatenated into the error message

See also

lower(...)

shared lib/stdlib/sh_string.lua:16

No documentation available.

mod(name)

shared lib/stdlib/sh_module.lua:36

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

parse_expression(state, min_precedence)

shared lib/stdlib/sh_dice.lua:618

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

parse_unary(state)

shared lib/stdlib/sh_dice.lua:592

Parses unary minus, so that -2 ^ 2 is -(2 ^ 2).

Parameters

  • state Table parser state

Returns

  • Table syntax tree node

printf(str, ...)

shared lib/stdlib/sh_utils.lua:51

Prints C-style formatted strings.

Parameters

  • str String format string, see string.format
  • ... Vararg values to put into the format string

require_client(file_name)

shared lib/stdlib/sh_helpers.lua:125

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()

require_ignore(who, should_ignore)

shared lib/stdlib/sh_helpers.lua:20

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

require_module(mod)

server gamemode/init.lua:14

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_relative(file_name)

shared lib/stdlib/sh_helpers.lua:41

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_relative_folder(dir, base, recursive)

shared lib/stdlib/sh_helpers.lua:77

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 false include all files recursively

See also

require_server(file_name)

shared lib/stdlib/sh_helpers.lua:147

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()

require_shared(file_name)

shared lib/stdlib/sh_helpers.lua:170

Require a file on both client and server. The .lua file ending may be omitted. This is equivalent of simply doing this:

if SERVER then
  AddCSLuaFile(file_name)
  include(file_name)
else
  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]

setenv(key, value)

shared gamemode/env.lua:34

Sets an environment variable "key" to "value".

Parameters

  • key String variable name
  • value Any new value

Returns

  • Any the new value

sub(...)

shared lib/stdlib/sh_string.lua:22

No documentation available.

try(func, ...)

shared lib/stdlib/sh_wrappers.lua:19

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

txt(text)

shared lib/stdlib/sh_utils.lua:206

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

typeof(obj)

shared lib/stdlib/sh_wrappers.lua:10

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

unequal(a, b)

shared lib/stdlib/sh_utils.lua:276

No documentation available.

upper(...)

shared lib/stdlib/sh_string.lua:19

No documentation available.

URLMaterial(url)

client lib/stdlib/cl_utils.lua:274

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

Defined in