Extensions Entities
scripts/vscripts/alyxlib/extensions/entities.lua
Methods
FindBestMatching
Finds the best matching entity based on a given unique name (if provided), classname, position, and search radius.
If a name is provided, it attempts to find an exact match by name first.
-
If only one entity with that name exists, it is returned immediately.
-
If multiple entities share the name, the one closest to the given position is chosen.
If no name is provided, the function falls back to finding the nearest entity of the given classname.
Parameters
name
string,""
The unique name of the entity (if available, "" if not)class
string
The classname of the entity (fallback if name isn't available)position
Vector
The position to search aroundradius(optional)
number
The max search radius (default: 128)
Returns
- EntityHandle
The best-matching entity found, or nil if none found
All
Gets an array of every entity that currently exists.
Returns
- EntityHandle[]
List of all entities
Random
Gets a random entity in the map.
Returns
- EntityHandle
A random entity
FindInPrefab
Finds a named entity within the same name-fixed-up prefab as another entity.
Might have issues in nested prefabs.
Parameters
entity
EntityHandle
A known entity in the prefabname
string
The name to search for
Returns
EntityHandle?
The found entity, or nil if not found
string
Prefab part of the name
FindInPrefab
Finds a named entity within the same name-fixed-up prefab as this entity.
Might have issues in nested prefabs.
Parameters
name
string
The name to search for
Returns
EntityHandle?
The found entity, or nil if not found
string
Prefab part of the name
FindAllInCone
Finds all entities with a cone.
Parameters
origin
Vector
Origin of the cone in worldspacedirection
Vector
Normalized direction vectormaxDistance
number
Max distance the cone will extend towardsdirectionmaxAngle
number
Field-of-view in degrees that the cone can see, [0-180]checkEntityBounds
boolean
If true the entity bounding box will be tested as well as the origin
Returns
- EntityHandle[]
List of entities found within the cone
FindAllInBounds
Finds all entities within mins and maxs bounding box.
Parameters
mins
Vector
Mins vector in world-spacemaxs
Vector
Maxs vector in world-spacecheckEntityBounds(optional)
boolean
trueif the bounding boxes should be used for the check instead of their origin
Returns
- EntityHandle[]
List of entities found
FindAllInBox
Finds all entities within an origin centered box.
Parameters
width
number
Size of the box on the X axislength
number
Size of the box on the Y axisheight
number
Size of the box on the Z axis
Returns
- EntityHandle[]
List of entities found
FindAllInCube
Finds all entities within an origin centered cube of a given size.
Parameters
origin
Vector
World space cube positionsize
number
Size of the cube in all directions
Returns
- EntityHandle[]
List of entities found
FindNearest
Finds the nearest entity to a world position.
Parameters
origin
Vector
Position to check frommaxRadius
number
Maximum radius to check fromorigin
Returns
- EntityHandle?
The nearest entity found, or nil if none found
FindAllByClassnameList
Finds all entities in the map from a list of classnames.
Parameters
classes
string[]
List of classnames
Returns
- EntityHandle[]
List of entities
FindAllByClassnameListWithin
Finds all entities within a radius from a list of classnames.
Parameters
classes
string[]
List of classnamesorigin
Vector
Position to check frommaxRadius
number
Maximum radius to check fromorigin
Returns
- EntityHandle[]
List of entities
FindByClassnameListNearest
Find the entity from a list of possible classnames which is closest to a world position.
Parameters
classes
string[]
List of classnamesorigin
Vector
Position to check frommaxRadius
number
Maximum radius to check fromorigin
Returns
- EntityHandle?
The nearest entity found, or nil if none found
FindAllNPCs
Finds all NPCs within the map.
Returns
- CAI_BaseNPC[]
List of NPC entities
IterateAllNPCs
Returns an iterator to loop over all NPC entities in the map using a for loop.
E.g.
Returns
- function
Iterator
FindAllByModelWithin
Finds all entities by model name within a radius.
Parameters
modelName
string
Model nameorigin
Vector
Position to check frommaxRadius
number
Maximum radius to check fromorigin
Returns
- EntityHandle[]
List of entities
FindByModelNearest
Finds the entity by model name nearest to a point.
Parameters
modelName
string
Model nameorigin
Vector
Position to check frommaxRadius
number
Maximum radius to check fromorigin
Returns
- EntityHandle?
The nearest entity found, or nil if none found
FindByModelPattern
Finds the first entity whose model name contains namePattern.
This works by searching every entity in the map and may incur a performance hit in large maps if used often.
Parameters
namePattern
string
Pattern to search for
Returns
- EntityHandle?
The found entity, or nil if not found
FindAllByModelPattern
Finds all entities whose model name contains namePattern.
This works by searching every entity in the map and may incur a performance hit in large maps if used often.
Parameters
namePattern
string
Pattern to search for
Returns
- EntityHandle[]
List of entities