util library
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
Functions
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
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
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
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
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
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
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
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
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
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
Returns the equality operators together with their symbols.
Returns
- Map operator name => String symbol, such as equal => '=='
Returns the equality and logical operators together with their symbols.
Returns
- Map operator name => String symbol, such as unequal => '!='
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
Returns the names of all operators that util.process_operator supports.
Returns
- List<String> operator names
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
Returns the comparison operators together with their symbols.
Returns
- Map operator name => String symbol, such as less_equal => '<='
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
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
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
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
tostringcallback(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
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
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
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
Removes the newlines and tabs from a string, except for those inside of double quotes.
Parameters
- str String
Returns
- String
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
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
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
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
Converts a value to a boolean. Only true, 'true', 1 and '1' count as true.
Parameters
- value Any
Returns
- Boolean
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
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
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
nilentities for the trace to ignore, same as the filter of util.TraceLine
Returns
- Boolean true if the trace hit something
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
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
0seconds between the attempts - wait_time Number optional, defaults to
100maximum amount of attempts
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
0width that is already taken up on the first line
Returns
- List<String> lines, or nil if text, font or width is missing