Extensions of the util library: validation and conversion of values, joining lists into strings, waiting for entities to become valid and applying operators by name. More of them are defined next to what they work with: text measuring, easing and materials on the client, color and number conversions with Color and math. This file also holds general-purpose global functions: formatted printing, txt for indented multi-line text, error reporting with tracebacks, enumerate for creating enumerator globals and include_sandboxed for running a file in a restricted environment.

Summary

Functions

Downloads an image from a URL into data/flux/materials and caches it as a material.
Calculates the value of a cubic ease-in interpolation at a given step.
Calculates the value of a cubic ease-in-out interpolation at a given step.
Precalculates every step of a cubic ease-in-out interpolation.
Precalculates every step of a cubic ease-in interpolation.
Calculates the value of a cubic ease-out interpolation at a given step.
Precalculates every step of a cubic ease-out interpolation.
Checks whether a door is currently open.
Adjusts x, y to fit inside x2, y2 while keeping the original aspect ratio.
Returns the line height of a font, measured on the sample text 'Agw'.
Returns the equality operators together with their symbols.
Returns the equality and logical operators together with their symbols.
Gets a material by its path.
Returns the names of all operators that util.process_operator supports.
Returns the class name a panel was registered under.
Returns the comparison operators together with their symbols.
Converts a hexadecimal color to a color structure.
Converts a hexadecimal number to decimal.
Converts a single hexadecimal digit to decimal.
Joins the text representations of a list of objects into a single string.
Returns the symbol of an operator, such as '>=' for 'greater_equal'.
Joins the names of a list of players into a single comma-separated string.
Applies an operator to two values by the name of the operator.
Removes the newlines and tabs from a string, except for those inside of double quotes.
Picks a text color that stays readable on top of the given background color.
Returns the height a text takes up when drawn with the given font.
Measures the size a text takes up when drawn with the given font.
Returns the width a text takes up when drawn with the given font.
Converts a value to a boolean.
Checks whether all of the arguments in vararg are valid (via IsValid).
Determines whether a 2D point is inside of a 2D polygon.
Checks whether anything is in the way between two positions, using a line trace.
Determines whether a vector from A to B intersects with a vector from C to D.
Calls the callback as soon as the entity with the given index becomes valid, right away if it already is.
Splits a text into lines that fit into the given width when drawn with the given font.

Functions

util.cache_url_material(url)

client lib/stdlib/cl_utils.lua:224

Downloads an image from a URL into data/flux/materials and caches it as a material. The download is asynchronous: the OnURLMatLoaded hook runs with the URL and the material once it finishes. Images that were downloaded before are loaded from disk right away.

Parameters

  • url String direct link to an image, has to end with a file extension

See also

util.cubic_ease_in(cur_step, steps, from, to)

client lib/stdlib/cl_utils.lua:116

Calculates the value of a cubic ease-in interpolation at a given step.

Parameters

  • cur_step Number current step
  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • Number interpolated value

util.cubic_ease_in_out(cur_step, steps, from, to)

client lib/stdlib/cl_utils.lua:170

Calculates the value of a cubic ease-in-out interpolation at a given step. Uses util.cubic_ease_in for the first half of the steps and util.cubic_ease_out for the second half.

Parameters

  • cur_step Number current step
  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • Number interpolated value

util.cubic_ease_in_out_t(steps, from, to)

client lib/stdlib/cl_utils.lua:184

Precalculates every step of a cubic ease-in-out interpolation.

Parameters

  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • List<Number> interpolated values, one per step

See also

util.cubic_ease_in_t(steps, from, to)

client lib/stdlib/cl_utils.lua:136

Precalculates every step of a cubic ease-in interpolation.

Parameters

  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • List<Number> interpolated values, one per step

See also

util.cubic_ease_out(cur_step, steps, from, to)

client lib/stdlib/cl_utils.lua:126

Calculates the value of a cubic ease-out interpolation at a given step.

Parameters

  • cur_step Number current step
  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • Number interpolated value

util.cubic_ease_out_t(steps, from, to)

client lib/stdlib/cl_utils.lua:152

Precalculates every step of a cubic ease-out interpolation.

Parameters

  • steps Number total amount of steps
  • from Number starting value
  • to Number final value

Returns

  • List<Number> interpolated values, one per step

See also

util.door_is_opened(entity)

shared lib/stdlib/sh_utils.lua:258

Checks whether a door is currently open.

Parameters

  • entity Entity

Returns

  • Boolean true if the door is open, false if it is closed or the entity is not a door

util.fit_to_aspect(x, y, x2, y2)

client lib/stdlib/cl_utils.lua:94

Adjusts x, y to fit inside x2, y2 while keeping the original aspect ratio.

Parameters

  • x Number width to fit
  • y Number height to fit
  • x2 Number maximum width
  • y2 Number maximum height

Returns

  • Number adjusted width
  • Number adjusted height

util.font_size(font)

client lib/stdlib/cl_utils.lua:71

Returns the line height of a font, measured on the sample text 'Agw'.

Parameters

  • font String optional, defaults to 'default' font name

Returns

  • Number height in pixels

util.get_equal_operators()

shared lib/stdlib/sh_utils.lua:371

Returns the equality operators together with their symbols.

Returns

  • Map operator name => String symbol, such as equal => '=='

util.get_logical_operators()

shared lib/stdlib/sh_utils.lua:342

Returns the equality and logical operators together with their symbols.

Returns

  • Map operator name => String symbol, such as unequal => '!='

util.get_material(mat, png_parameters)

client lib/stdlib/cl_utils.lua:202

Gets a material by its path. It caches the material automatically, once per path and set of PNG parameters.

Parameters

  • mat String material path
  • png_parameters String PNG parameters such as 'smooth mips', default: none

Returns

  • Material

util.get_operators()

shared lib/stdlib/sh_utils.lua:330

Returns the names of all operators that util.process_operator supports.

Returns

  • List<String> operator names

util.get_panel_class(panel)

client lib/stdlib/cl_utils.lua:78

Returns the class name a panel was registered under.

Parameters

  • panel Panel

Returns

  • String class name, or nil if the panel is invalid or has no ClassName

util.get_relational_operators()

shared lib/stdlib/sh_utils.lua:356

Returns the comparison operators together with their symbols.

Returns

  • Map operator name => String symbol, such as less_equal => '<='

util.hex_to_color(hex)

shared lib/stdlib/sh_color.lua:14

Converts a hexadecimal color to a color structure. Accepts 3 (rgb), 6 (rrggbb) or 8 (rrggbbaa) digits with an optional leading '#'.

Parameters

  • hex String hexadecimal color such as '#fff' or 'ff8800'

Returns

  • Color the converted color, or white if the string has any other length

util.hex_to_dec(hex)

shared lib/stdlib/sh_math.lua:74

Converts a hexadecimal number to decimal.

Parameters

  • hex String/Number hexadecimal number without a prefix, e.g. 'ff'; numbers pass through

Returns

  • Number decimal value

util.hex_to_decimal(hex)

shared lib/stdlib/sh_math.lua:41

Converts a single hexadecimal digit to decimal.

Parameters

  • hex String/Number hexadecimal digit, may be prefixed with '-'; numbers pass through

Returns

  • Number decimal value, or 0 (after printing an error) if it is not a hexadecimal digit

util.list_to_string(callback, separator, ...)

shared lib/stdlib/sh_utils.lua:110

Joins the text representations of a list of objects into a single string.

util.list_to_string(nil, nil, 1, 2, 3) -- '1, 2, 3'
util.list_to_string(function(obj) return obj:name() end, ' and ', player1, player2)

Parameters

  • callback Function optional, defaults to tostring callback(object), returns the String to use for the object
  • separator String optional, defaults to ', ' text to put between the objects
  • ... Vararg objects to list

Returns

  • String the joined list

util.operator_to_symbol(op)

shared lib/stdlib/sh_utils.lua:383

Returns the symbol of an operator, such as '>=' for 'greater_equal'.

Parameters

  • op String operator name

Returns

  • String symbol, or nil if there is no such operator

util.player_list_to_string(player_list)

shared lib/stdlib/sh_utils.lua:147

Joins the names of a list of players into a single comma-separated string. If the list consists of all players on the server (and there are at least two), returns the 'ui.chat.everyone' phrase instead.

Parameters

  • player_list List<Player>

Returns

  • String player names, or the 'ui.chat.everyone' phrase

util.process_operator(op, a, b)

shared lib/stdlib/sh_utils.lua:324

Applies an operator to two values by the name of the operator.

util.process_operator('greater_equal', 5, 3) -- true

Parameters

  • op String operator name: 'equal', 'unequal', 'less', 'greater', 'less_equal', 'greater_equal', 'and', 'or' or 'not'
  • a Any left operand
  • b Any right operand, not used by 'not'

Returns

  • Any result of the operation, a Boolean for the comparisons and 'not'

See also

util.remove_newlines(str)

shared lib/stdlib/sh_utils.lua:160

Removes the newlines and tabs from a string, except for those inside of double quotes.

Parameters

  • str String

Returns

  • String

util.text_color_from_base(base_color)

shared lib/stdlib/sh_color.lua:317

Picks a text color that stays readable on top of the given background color.

Parameters

  • base_color Color background color

Returns

  • Color black for bright backgrounds, white for dark ones

util.text_height(text, font)

client lib/stdlib/cl_utils.lua:64

Returns the height a text takes up when drawn with the given font.

Parameters

  • text String text to measure
  • font String optional, defaults to 'default' font name

Returns

  • Number height in pixels

See also

util.text_size(text, font)

client lib/stdlib/cl_utils.lua:19

Measures the size a text takes up when drawn with the given font. Results are cached, so this is cheap to call every frame. The cache is emptied once it holds results for more than 8192 different texts.

Parameters

  • text String text to measure
  • font String optional, defaults to 'default' font name

Returns

  • Number width in pixels
  • Number height in pixels

util.text_width(text, font)

client lib/stdlib/cl_utils.lua:55

Returns the width a text takes up when drawn with the given font.

Parameters

  • text String text to measure
  • font String optional, defaults to 'default' font name

Returns

  • Number width in pixels

See also

util.to_b(value)

shared lib/stdlib/sh_utils.lua:58

Converts a value to a boolean. Only true, 'true', 1 and '1' count as true.

Parameters

  • value Any

Returns

  • Boolean

util.validate(...)

shared lib/stdlib/sh_utils.lua:34

Checks whether all of the arguments in vararg are valid (via IsValid).

Parameters

  • ... Vararg objects to check

Returns

  • Boolean true if all of them are valid, false if any is not or if there are none

util.vector_in_poly(point, poly_vertices)

shared lib/stdlib/sh_math.lua:123

Determines whether a 2D point is inside of a 2D polygon.

Parameters

  • point Vector point to check, only x and y are used
  • poly_vertices List<Vector> vertices of the polygon, in order

Returns

  • Boolean whether the point is inside, or nil if the arguments are invalid

util.vector_obstructed(vec1, vec2, filter)

shared lib/stdlib/sh_utils.lua:245

Checks whether anything is in the way between two positions, using a line trace.

Parameters

  • vec1 Vector start position
  • vec2 Vector end position
  • filter Entity/List<Entity>/Function optional, defaults to nil entities for the trace to ignore, same as the filter of util.TraceLine

Returns

  • Boolean true if the trace hit something

util.vectors_intersect(from, to, from2, to2)

shared lib/stdlib/sh_math.lua:93

Determines whether a vector from A to B intersects with a vector from C to D. Works in 2D, only the x and y components are used.

Parameters

  • from Vector point A
  • to Vector point B
  • from2 Vector point C
  • to2 Vector point D

Returns

  • Boolean true if the vectors intersect or are collinear

util.wait_for_ent(ent_index, callback, delay, wait_time)

shared lib/stdlib/sh_utils.lua:78

Calls the callback as soon as the entity with the given index becomes valid, right away if it already is. Useful on the client, where an entity index can arrive before the entity itself does. Gives up without calling the callback once it runs out of attempts. Every call waits on its own, so several can be pending at once, also for the same entity.

util.wait_for_ent(ply_index, function(target)
  hook.Run('PlayerModelChanged', target, new_model, old_model)
end)

Parameters

  • ent_index Number entity index
  • callback Function callback(entity), receives the valid Entity
  • delay Number optional, defaults to 0 seconds between the attempts
  • wait_time Number optional, defaults to 100 maximum amount of attempts

util.wrap_text(text, font, width, initial_width)

client lib/stdlib/cl_utils.lua:298

Splits a text into lines that fit into the given width when drawn with the given font. Words that are wider than a whole line are broken up with a dash. Newlines are removed.

Parameters

  • text String text to wrap
  • font String font name
  • width Number maximum width of a line in pixels
  • initial_width Number optional, defaults to 0 width that is already taken up on the first line

Returns

  • List<String> lines, or nil if text, font or width is missing

Defined in