Globals
scripts/vscripts/alyxlib/globals.lua
Global variables
| Name | Value |
|---|---|
AlyxLibAddons |
table |
Functions
RegisterAlyxLibAddon
Registers an addon with AlyxLib.
Parameters
name
string
Full display name of the addon, e.g. "My New Addon"version
string
SemVer version string of the addon, e.g. "v1.2.3"workshopID(optional)
string
The ID of the addon on the Steam workshopshortName(optional)
string
Short unique name of the addon without spaces, e.g. "myaddon". Defaults tonamewithout spaces and converted to lowercaseminAlyxLibVersion(optional)
string
Minimum AlyxLib version that this addon works with, defaults to "v1.0.0"maxAlyxLibVersion(optional)
string
Maximum AlyxLib version that this addon works with, defaults toALYXLIB_VERSION
Returns
- integer
The index of the addon for use in other AlyxLib functions
RegisterAlyxLibDiagnostic
Registers a diagnostic function for an addon to help users describe issues back to the developer.
The diagnostic function should return two values:
-
trueif the addon is working as expected,falseotherwise -
An array of strings or a string containing diagnostic messages
Common AlyxLib and game information will be printed alongside the diagnostic messages for users to copy.
Parameters
addonIndex
integer
The index of the addon for use in other AlyxLib functionsfunc
function
Diagnostic function to check if the addon is working, and any diagnostic messages
CompareVersions
Compares two semantic version strings and returns an integer indicating their relative order.
It compares the versions based on their major, minor, and patch components.
If a version is incomplete, the missing components are assumed to be 0.
-
-1ifv1is older thanv2. -
1ifv1is newer thanv2. -
0if both versions are equal.
Parameters
v1
string
The first version string to compare. May include leading "v" and whitespace, and may have missingminororpatchcomponentsv2
string
The second version string to compare. Similar format and rules tov1
Returns
- -1|0|1
GetScriptFile
Gets the file name of the current script without folders or extension. E.g. util.util
Parameters
sep(optional)
string
Separator character (default:.)level(optional)
integer,function
Function level, View documents
Returns
- string
File name
GetEnabledAddons
Gets the list of enabled addons from the default_enabled_addons_list convar.
Returns
- string[]
List of enabled addons
IsAddonEnabled
Checks if the addon with the given workshopID is enabled.
Parameters
workshopID
string
The workshop ID of the addon
Returns
- boolean
true if the addon is enabled, false otherwise
IsEntity
Checks if the given handle value is an entity, regardless of if it's still alive.
A common usage is replacing the often used entity check:
With:
Parameters
value
EntityHandle,any
The value to checkcheckValidity(optional)
boolean
Optionally check validity with IsValidEntity
Returns
- boolean
AddOutput
Adds an output to a given entity.
Parameters
entity
EntityHandle,string
The entity to add theoutputtooutput
string
The output name to addtarget
EntityHandle,string
The entity the output should target, either handle or targetnameinput
string
The input name ontargetparameter(optional)
string
The parameter override forinput(default:"")delay(optional)
number
Delay for the output in seconds (default:0)activator(optional)
EntityHandle
Activator for the output (default:nil)caller(optional)
EntityHandle
Caller for the output (default:nil)fireOnce(optional)
boolean
If the output should only fire once (default:false)
module_exists
Checks if the module/script exists.
Parameters
name(optional)
string
Name of the module
Returns
- boolean
true if the module exists, false otherwise
ifrequire
Loads the given module, returns any value returned by the given module(true when module returns nothing).
Then runs the given callback function.
If the module fails to load then the callback is not executed and no error is thrown, but a warning is displayed in the console.
Parameters
modname
string
Name of the module/scriptcallback
function?
Callback function
Returns
- unknown|nil
The value returned by the module, or nil if the module fails to load
IncludeScript
Executes a script file. Included in the current scope by default.
Parameters
scriptFileName
string
Name of the scriptscope(optional)
ScriptScope
Scope to include the script in
Returns
- boolean
true if the script was successfully included, false otherwise
IsVREnabled
Checks if the game was started in VR mode.
Returns
- boolean
true if the game was started in VR mode, false otherwise
prints
Prints all arguments with spaces between instead of tabs.
Parameters
...
printn
Prints all arguments on a new line instead of tabs.
Parameters
...
devprint
Prints all arguments if convar "developer" is greater than 0.
Parameters
...
devprints
Prints all arguments on a new line instead of tabs if convar "developer" is greater than 0.
Parameters
...
devprintn
Prints all arguments with spaces between instead of tabs if convar "developer" is greater than 0.
Parameters
...
devprint2
Prints all arguments if convar "developer" is greater than 1.
Parameters
...
devprints2
Prints all arguments on a new line instead of tabs if convar "developer" is greater than 1.
Parameters
...
devprintn2
Prints all arguments with spaces between instead of tabs if convar "developer" is greater than 1.
Parameters
...
warn
Prints a warning in the console, along with a vscript print if inside tools mode.
Parameters
...
devwarn
Prints a warning in the console, along with a vscript print if inside tools mode. But only if convar "developer" is greater than 0.
Parameters
...
Expose
Add a function to the calling entity's script scope with alternate casing.
Makes a function easier to call from Hammer through I/O.
E.g.
Example
Parameters
func
function
The function to exposename(optional)
string
Optionally the name of the function for faster processingscope(optional)
table
Optionally the explicit scope to put the exposed function in
IsVector
Checks if a value is a Vector.
Parameters
value
any
Value to check
Returns
- boolean
true if the value is a Vector, false otherwise
IsQAngle
Checks if a value is a QAngle.
Parameters
value
any
Value to check
Returns
- boolean
true if the value is a QAngle, false otherwise
DeepCopyTable
Copy all keys from tbl and any nested tables into a brand new table and return it.
This is a good way to get a unique reference with matching data.
Any functions and userdata will be copied by reference, except for:
Vector,
QAngle
Parameters
tbl
table
Table to copy
Returns
- table
A copy of tbl
TableRemove
Searches for value in tbl and sets the associated key to nil, returning the key if found.
If your table is an array you should use ArrayRemove instead.
Parameters
tbl
table
Table to searchvalue
any
Value to search for
Returns
- any
The key of value if found, nil otherwise
TableRandom
Returns a random key/value pair from a unordered table.
Parameters
tbl
table
Table to get a random pair from
Returns
-
any
key
Random key selected -
any
value
Value linked to the random key
TableKeys
Returns all keys of a table as a new ordered array.
Parameters
tbl
table<K,any>
Table to get keys from
Returns
- K[]
List of keys
TableValues
Returns all values in a table as a new ordered array.
Parameters
tbl
table<any,V>
Table to get values from
Returns
- V[]
List of values
TableSize
Returns the size of a table by counting all keys.
Parameters
tbl
table
The table to count
Returns
- number
The size of the table
TablePluck
Collects all values for a specific key from a list of tables.
Parameters
tbl
table[]
List of tableskey
any
Key to get values from
Returns
- any[]
List of values found for key
TableFindKey
Returns the key of the first value that matches the predicate.
Parameters
tbl
table
Table to searchpredicate
function
Predicate function
Returns
- any
Key of the first value that matches the predicate
ArrayRandom
Returns a random value from an array.
Parameters
array
T[]
Array to get a value frommin(optional)
integer
Optional minimum boundmax(optional)
integer
Optional maximum bound
Returns
-
T
one
The random value -
integer
two
The random index
ArrayShuffle
Shuffles a given array in-place.
Parameters
array
any[]
Array to shuffle
ArrayRemove
Remove an item from an array at a given position.
This is exponentially faster than table.remove for large arrays.
Parameters
array
T
The array to remove frompos
integer
Position to remove at
Returns
- T
The same array passed in
ArrayRemoveVal
Remove a value from an array.
This is exponentially faster than table.remove for large arrays.
Parameters
array
T
The array to remove fromvalue
any
The value to remove
Returns
- T
The same array passed in
ArrayAppend
Appends array2 onto array1 as a new array.
Safe extend function alternative to vlua.extend, neither input arrays are modified.
Parameters
array1
T1[]
Base arrayarray2
T2[]
Array which will be appended onto the base array
Returns
- T1[]|T2[]
The new appended array
ArrayAppends
Appends any number of arrays onto array as a new array object.
Safe extend function alternative to vlua.extend, no input arrays are modified.
Parameters
array
T[]
Base array
Returns
- T[]
The new appended array
TraceLineExt
Does a raytrace along a line with extended parameters.
You ignore multiple entities as well as classes and names.
Because the trace has to be redone multiple times, a timeout parameter can be defined to cap the number of traces.
Parameters
parameters
TraceTableLineExt
Trace parameters
Returns
- boolean
true if the trace was successful
TraceLineWorld
Does a raytrace along a line until it hits the world or reaches the end of the line.
Parameters
parameters
TraceTableLine
Trace parameters
Returns
- TraceTableLine
Trace parameters with results
TraceLineEntity
Does a raytrace along a line until it hits the specified entity or reaches the end of the line.
Parameters
parameters
TraceTableLine
Trace parameters
Returns
- TraceTableLine
Trace parameters with results
TraceLineSimple
Performs a simple line trace and returns the trace table.
Parameters
startpos
Vector
Start positionendpos
Vector
End positionignore(optional)
EntityHandle
Entity to ignoremask(optional)
integer
Trace mask
Returns
- TraceTableLine
Trace table with results
IsWorld
Checks if an entity is the world entity.
Parameters
entity
EntityHandle
Entity to check
Returns
- boolean
true if the entity is the world entity, false otherwise
GetWorld
Gets the world entity.
Returns
- EntityHandle
World entity
IsPhysicsObject
Checks if an entity is a physical entity.
Parameters
entity
EntityHandle
Entity to check
Returns
- boolean
true if the entity is a physical entity, false otherwise
haskey
Checks if a table has a key (this essentially the same as tbl[key] ~= nil).
Parameters
tbl
table
Table to checkkey
any
Key to look for
Returns
- boolean
true if the table has the key, false otherwise
truthy
Checks if a value is truthy or falsy.
falsy == nil|false|0|""|{}
Parameters
value
any
The value to be checked
Returns
- boolean
true if the value is truthy, false otherwise
SearchEntity
Searches an entity for a key using a search pattern. E.g. "getclass" will find "GetClassname"
Works with class.lua EntityClass entities.
Parameters
entity
EntityHandle,EntityClass
The entity to searchsearchPattern
string
The name pattern to search for
Returns
-
string?
key
The full name of the first key matchingsearchPattern -
any?
value
The value of the key found
LerpAngle
Linearly interpolates between two angles.
Parameters
t
number
The interpolation parameter, where0returnsangle_startand1returnsangle_endangle_start
number
The starting angle in degreesangle_end
number
The ending angle in degrees
Returns
- number
The interpolated angle
CalcClosestPointOnEntityOBBAdjusted
Calculates the closest point on an entity's OBB to a position.
This is a modified version of the original function that fixes off-center models not calculating correctly.
Parameters
entity
EntityHandle
The entity to calculate the closest point onposition
Vector
The position to calculate the closest point to
Returns
- Vector
The closest point on the entity's OBB to the position
DefaultTable
Assigns a default value to a table which will be returned if an invalid key is accessed.
Parameters
tbl
T
The table to which the default value will be assigneddefault
any
The default value to be returned for invalid keys
Returns
- T
The table with the default value assigned
Wrap
Wraps a value within a specified range.
Parameters
value
number
The value to be wrappedmin
number
The minimum value of the rangemax
number
The maximum value of the range
Returns
- number
The wrapped value within the specified range
CreateToggleBehavior
This function creates a toggle behavior function that switches between two provided functions based on a condition.
Example:
Example
Example
Parameters
on(optional)
function
Function called when the condition is trueoff(optional)
function
Function called when the condition is false
Returns
- function
The created toggle function
CalcClosestCornerOnEntityAABB
Computes the closest corner relative to a vector on the AABB of an entity.
Parameters
entity
EntityHandle
The entity to calculate the closest corner onposition
Vector
The vector to calculate the closest corner to
SetPhysVelocity
Sets the absolute world velocity of an entity.
Parameters
velocity
Vector
The target velocity in units/second.
RandomChance
Returns one of two values based on a percentage chance.
If the random roll succeeds, returns onTrue (default: true).
If it fails, returns onFalse (default: false).
Parameters
chance
number
The percentage chance of success (0-100)onTrue(optional)
any
Value to return if the chance succeeds (default:true)onFalse(optional)
any
Value to return if the chance fails (default:false)
Returns
- boolean|any
Types
AlyxLibAddon
A registered AlyxLib addon.
| Field | Type | Description |
|---|---|---|
| name | string |
Full display name of the addon, e.g. My "New Addon" |
| version | string |
SemVer version string of the addon, e.g. "v1.2.3" |
| shortName | string |
Short unique name of the addon without spaces, e.g. "myaddon" |
| minAlyxLibVersion | string |
Minimum AlyxLib version that this addon works with |
| maxAlyxLibVersion | string |
Maximum AlyxLib version that this addon works with |
| workshopID? | string |
The ID of the addon on the Steam workshop |
| diagnosticFunction? | function |
Diagnostic function to check if the addon is working, and any diagnostic messages |
TraceTableLineExt
Inherits from:
TraceTableLine
| Field | Type | Description |
|---|---|---|
| ignore | (EntityHandle|EntityHandle[])? |
Entity or array of entities to ignore |
| ignoreclass | (string|string[])? |
Class or array of classes to ignore |
| ignorename | (string|string[])? |
Name or array of names to ignore |
| timeout | integer? |
Maxmimum number of traces before returning regardless of parameters |
| traces | integer |
Number of traces done |
| dontignore | EntityHandle |
A single entity to always hit, ignoring if it exists in ignore |