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[]
Random
Gets a random entity in the map.
Returns
- EntityHandle
FindInPrefab
Find an entity within the same prefab as another entity.
Will have issues in nested prefabs.
Parameters
entity
EntityHandlename
string
Returns
EntityHandle?
The found entity, nil if not found.
string
Prefab part of the name.
FindInPrefab
Find an entity within the same prefab as this entity.
Will have issues in nested prefabs.
Parameters
name
string
Returns
-
EntityHandle? -
string
Prefab part of the name.
FindAllInCone
Find all entities with a cone.
Parameters
origin
Vector
Origin of the cone in worldspace.direction
Vector
Normalized direction vector.maxDistance
number
Max distance the cone will extend towardsdirection.maxAngle
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
Find all entities within mins and maxs bounding box.
Parameters
mins
Vector
Mins vector in world-space.maxs
Vector
Maxs vector in world-space.checkEntityBounds(optional)
boolean
If true the entity bounding boxes will be used for the check instead of the origin.
Returns
- EntityHandle[]
List of entities found.
FindAllInBox
Find all entities within an origin centered box.
Parameters
width
number
Size of the box on the X axis.length
number
Size of the box on the Y axis.height
number
Size of the box on the Z axis.
Returns
- EntityHandle[]
List of entities found.
FindAllInCube
Find all entities within an origin centered cube of a given size.
Parameters
origin
Vector
World space cube position.size
number
Size of the cube in all directions.
Returns
- EntityHandle[]
List of entities found.
FindNearest
Find the nearest entity to a world position.
Parameters
origin
Vector
Position to check from.maxRadius
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[]
Returns
- EntityHandle[]
FindAllByClassnameListWithin
Finds all entities within a radius from a list of classnames.
Parameters
classes
string[]origin
VectormaxRadius
number
Returns
- EntityHandle[]
FindByClassnameListNearest
Find the entity from a list of possible classnames which is closest to a world position.
Parameters
classes
string[]origin
VectormaxRadius
number
Returns
- EntityHandle?
FindAllNPCs
Finds all NPCs within the map.
Returns
- CAI_BaseNPC[]
IterateAllNPCs
Returns an iterator to loop over all NPC entities in the map using a for loop.
E.g.
Returns
- function
Iterator
FindAllByModelWithin
Find all entities by model name within a radius.
Parameters
modelName
stringorigin
VectormaxRadius
number
Returns
- EntityHandle[]
FindByModelNearest
Find the entity by model name nearest to a point.
Parameters
modelName
stringorigin
VectormaxRadius
number
Returns
- EntityHandle?
FindByModelPattern
Find 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
Returns
- EntityHandle?
FindAllByModelPattern
Find 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
Returns
- EntityHandle[]