The IDT
module provides enhanced versions of standard VScripts data structures, including arrays, lists, and trees, with additional methods and functionality to improve efficiency and flexibility.
- IDT/array.nut
ArrayEx(...)
FromArray(array)
WithSize(numberOfItems, fillValue)
append(value)
apply(func)
clear()
extend(other)
filter(func)
contains(value)
search(value or func)
insert(index, value)
len()
map(func)
reduce(func, initial)
unique()
pop()
push(value)
remove(index)
resize(size, fill)
reverse()
slice(start, end)
sort(func)
top()
join(separator)
get(index, default)
totable(recreate)
tolist()
- IDT/list.nut
List(...)
FromArray(array)
len()
iter()
rawIter()
append(value)
insert(index, value)
getNode(index)
get(index, defaultValue)
remove(index)
pop()
top()
reverse()
unique()
clear()
join(separator)
apply(func)
extend(other)
search(value or func)
map(func)
filter(condition)
reduce(func, initial)
totable()
toarray()
SwapNode(node1, node2)
- IDT/tree_sort.nut
- IDT/entity_creator.nut
CreateByClassname(classname, keyvalues)
CreateProp(classname, origin, modelname, activity, keyvalues)
FromEntity(CBaseEntity)
FindByClassname(classname, start_ent)
FindByClassnameWithin(classname, origin, radius, start_ent)
FindByName(targetname, start_ent)
FindByNameWithin(targetname, origin, radius, start_ent)
FindByModel(model, start_ent)
FindByModelWithin(model, origin, radius, start_ent)
FindInSphere(origin, radius, start_ent)
- IDT/entity.nut
-
Entity State and Lifecycle:
-
Naming:
-
Players Methods:
-
Transformations:
-
Appearance:
-
Key Values and Data:
-
Collision and Physics:
-
Sound:
-
Outputs and Inputs:
-
Bounding Box and Position:
-
This file defines the ArrayEx
class, which is an enhanced version of the standard VScripts array. It provides various methods for manipulating, searching, sorting, and converting arrays, making them more versatile and easier to work with.
Constructor
Creates a new ArrayEx
object from a variable number of arguments. This constructor allows you to initialize the ArrayEx
with the specified elements directly.
Parameters:
...
(any): A variable number of arguments representing the initial elements of the array.
Example:
local myArrayEx = ArrayEx(1, "hello", Vector(0, 0, 0)) // Create an ArrayEx with three elements
Static Method
Creates a new ArrayEx
object from an existing array. This method is useful when you want to convert a standard Squirrel array into an ArrayEx
object, preserving its elements. If the input array
is already an ArrayEx
object, it's returned directly without creating a new object.
Parameters:
array
(array): The initial array to use for theArrayEx
object.
Returns:
- (ArrayEx): The newly created or the input
ArrayEx
object.
Example:
local myArray = [1, 2, 3]
local myArrayEx = ArrayEx.FromArray(myArray) // Create an ArrayEx object from the existing array
local existingArrayEx = ArrayEx(4, 5, 6)
local sameArrayEx = ArrayEx.FromArray(existingArrayEx) // Returns the same ArrayEx object
Static Method
Creates a new ArrayEx
object with a specified size, optionally filling it with a default value. This method is handy for creating arrays of a predefined size with initial values.
Parameters:
numberOfItems
(int): The desired number of elements in the array.fillValue
(any, optional): The value to fill the array with (defaults tonull
).
Returns:
- (ArrayEx): The newly created
ArrayEx
object.
Example:
local myArrayEx = ArrayEx.WithSize(5, 0) // Creates an ArrayEx with 5 elements, each initialized to 0
This method appends a value to the end of the array. It automatically resizes the underlying array if necessary to accommodate the new element.
Parameters:
value
(any): The value to append to the array.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
myArrayEx.append(4) // Add the number 4 to the end of the array
This method applies a function to each element of the array and modifies the array in-place. The function should take one argument (the element value) and return the new value for the element.
Parameters:
func
(function): The function to apply to each element.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
myArrayEx.apply(function(x) { return x * 2 }) // Multiply each element by 2
This method removes all elements from the array and resets its length to 0.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
myArrayEx.clear() // Remove all elements from the array
This method extends the array by appending all elements from another array or ArrayEx
object to the end of this array. It automatically resizes the underlying array if necessary.
Parameters:
other
(array or ArrayEx): The array orArrayEx
object to append elements from.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
local otherArray = array("a", "b", "c")
myArrayEx.extend(otherArray) // Append the elements of otherArray to myArrayEx
This method creates a new ArrayEx
object containing only the elements that satisfy the predicate function.
Parameters:
func
(function): The predicate function that takes three arguments:index
(number): The index of the current element.value
(any): The value of the current element.newArray
(ArrayEx): The new array being created. The function should returntrue
if the element should be included in the new array, andfalse
otherwise.
Returns:
- (ArrayEx): A new
ArrayEx
object containing the filtered elements.
Example:
local evenNumbers = myArrayEx.filter(function(index, value) {
return value % 2 == 0 // Check if the value is even
})
This method checks if the array contains the specified value.
Parameters:
value
(any): The value to search for.
Returns:
- (boolean):
true
if the value is found in the array,false
otherwise.
Example:
if (myArrayEx.contains(5)) {
// The array contains the value 5
}
This method searches for a value or a matching element in the array and returns its index.
Parameters:
value or func
(any or function):- If a value, the method searches for the first occurrence of that value in the array.
- If a function, the method searches for the first element that satisfies the predicate function. The function should take one argument (the element value) and return
true
if it matches,false
otherwise.
Returns:
- (number or null): The index of the first matching element, or
null
if no match is found.
Example:
local index = myArrayEx.search(5) // Find the index of the value 5
local index2 = myArrayEx.search(function(x) { return x > 10 }) // Find the index of the first element greater than 10
This method inserts a value into the array at the specified index, shifting all subsequent elements to the right.
Parameters:
index
(number): The index at which to insert the value.value
(any): The value to insert.
Returns:
- (any): The inserted value.
Example:
myArrayEx.insert(2, "new element") // Insert "new element" at index 2
This method returns the number of elements in the array.
Returns:
- (number): The number of elements in the array.
Example:
local arrayLength = myArrayEx.len() // Get the length of the array
This method creates a new ArrayEx
object by applying a function to each element of this array.
Parameters:
func
(function): The function to apply to each element. The function should take one argument (the element value) and return the new value for the element.
Returns:
- (ArrayEx): A new
ArrayEx
object containing the mapped values.
Example:
local squares = myArrayEx.map(function(x) { return x * x }) // Create an array of squares of the original elements
Reduce the array to a single value.
Parameters:
func
(Function): The reducer function, which takes the accumulator and current item as arguments.initial
(any): The initial value of the accumulator.
Returns:
- (any): The reduced value.
Example:
local myArray = ArrayEx.FromArray([1, 2, 3, 4, 5])
local sum = myArray.reduce(function(acc, item) {
return acc + item
}, 0)
printl(sum) // Output: 15
Return a new array with only unique elements.
Returns:
- (ArrayEx): The new array with unique elements.
Example:
local myArray = ArrayEx.FromArray([1, 2, 2, 3, 4, 4, 5])
local uniqueArray = myArray.unique()
printl(uniqueArray) // Output: ArrayEx([1, 2, 3, 4, 5])
This method removes and returns the last element of the array.
Returns:
- (any): The removed element.
Example:
local lastElement = myArrayEx.pop() // Remove and return the last element of the array
This method appends a value to the end of the array. It is equivalent to append(value)
.
Parameters:
value
(any): The value to append to the array.
Example:
myArrayEx.push(5) // Add the value 5 to the end of the array
This method removes the element at the specified index from the array, shifting all subsequent elements to the left.
Parameters:
index
(number): The index of the element to remove.
Returns:
- (any): The value of the removed element.
Example:
myArrayEx.remove(1) // Remove the element at index 1
This method resizes the array to the specified size. If the new size is larger than the current size, the array is extended and the new elements are optionally filled with a specified value.
Parameters:
size
(number): The new size of the array.fill
(any, optional): The value to fill new elements with (default isnull
).
Example:
myArrayEx.resize(10, 0) // Resize the array to 10 elements, filling new elements with 0
This method reverses the order of elements in the array in-place.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
myArrayEx.reverse() // Reverse the order of elements in the array
This method returns a new ArrayEx
object containing a portion of the array.
Parameters:
start
(number): The starting index of the slice (inclusive).end
(number, optional): The ending index of the slice (exclusive, defaults to the end of the array).
Returns:
- (ArrayEx): A new
ArrayEx
object containing the sliced elements.
Example:
local slicedArray = myArrayEx.slice(1, 3) // Get a new array containing elements from index 1 to 2 (exclusive)
This method sorts the elements of the array in-place, optionally using a comparison function.
Parameters:
func
(function, optional): A comparison function that takes two arguments (the two elements to compare) and returns:- A negative value if the first element is less than the second element.
- Zero if the elements are equal.
- A positive value if the first element is greater than the second element. If no comparison function is provided, the elements are sorted according to their default comparison behavior.
Returns:
- (ArrayEx): The
ArrayEx
object itself (for method chaining).
Example:
myArrayEx.sort() // Sort the array in ascending order (default behavior)
myArrayEx.sort(function(a, b) { return b - a }) // Sort the array in descending order
This method returns the last element of the array.
Returns:
- (any): The last element of the array.
Example:
local lastElement = myArrayEx.top() // Get the last element of the array
This method joins the elements of the array into a string, separated by the specified separator.
Parameters:
separator
(string, optional): The separator to use between elements (default is an empty string).
Returns:
- (string): The joined string.
Example:
local joinedString = myArrayEx.join(", ") // Join the array elements into a string separated by commas and spaces
This method returns the element at the specified index in the array. If the index is out of bounds, it returns a default value.
Parameters:
index
(number): The index of the element to retrieve.default
(any, optional): The default value to return if the index is out of bounds (default isnull
).
Returns:
- (any): The element at the specified index, or the default value if the index is out of bounds.
Example:
local element = myArrayEx.get(2) // Get the element at index 2
local element2 = myArrayEx.get(10, 0) // Get the element at index 10, or 0 if the index is out of bounds
This method converts the array to a table, using the array elements as keys and setting the values to null
.
Parameters:
recreate
(boolean, optional): Whether to recreate the table even if it already exists (default isfalse
).
Returns:
- (table): The table representation of the array.
Example:
local tableRepr = myArrayEx.totable() // Convert the array to a table
This method converts the array to a List
object.
Returns:
- (List): The
List
object containing the elements of the array.
Example:
local listRepr = myArrayEx.tolist() // Convert the array to a list
This file defines the List
class, which represents a doubly linked list and provides methods for adding and removing elements, accessing elements by index, reversing the list, and converting the list to an array. A doubly linked list is a linear data structure where each element (node) contains a value and references to the previous and next nodes in the list.
Improper use of the List class can lead to memory leaks, as the garbage collector (GC) may not clean up the list if it is used incorrectly.
Constructor - Creates a new List
object with optional initial elements.
Parameters:
...
(any): The initial elements to add to the list.
Example:
local myList = List(1, 2, 3)
Creates a new List
object from an existing array.
Parameters:
array
(array): The array to create the list from.
Returns:
- (List): The new list containing the elements from the array.
Example:
local myArray = [1, 2, 3]
local myList = List.FromArray(myArray)
Gets the length of the list, which is the number of elements it contains.
Returns:
- (number): The number of elements in the list.
Example:
local listLength = myList.len()
Returns an iterator object for the list. This method is more efficient than using a built-in iterator.
Returns:
- (iterator): An iterator for the list.
Example:
foreach(value in myList.iter()) {
printl(value)
}
Similar to iter
, but returns the node instead of the node's value.
Returns:
- (iterator): An iterator for the nodes in the list.
Example:
foreach(node in myList.rawIter()) {
printl(node.next_ref.value)
}
// Output: 2
// 3
// null
Appends a value to the end of the list.
Parameters:
value
(any): The value to append.
Example:
myList.append(4)
Inserts a value at a specific index in the list.
Parameters:
index
(number): The index to insert the value at.value
(any): The value to insert.
Example:
myList.insert(2, "inserted")
Gets the node at a specific index in the list. Throws an error if the index is out of bounds.
Parameters:
index
(number): The index of the node to retrieve.
Returns:
- (ListNode): The node at the specified index.
Example:
local node = myList.getNode(1)
Gets the value at a specific index in the list, or a default value if the index is out of bounds.
Parameters:
index
(number): The index of the value to retrieve.defaultValue
(any, optional): The value to return if the index is out of bounds.
Returns:
- (any): The value at the specified index or the default value if the index is out of bounds.
Example:
local value = myList.get(2)
Removes the node at a specific index from the list.
Parameters:
index
(number): The index of the node to remove.
Returns:
- (any): The value of the removed element.
Example:
myList.remove(1)
Removes the last element from the list and returns its value.
Returns:
- (any): The value of the removed element.
Example:
local lastValue = myList.pop()
Gets the value of the last element in the list.
Returns:
- (any): The value of the last element.
Example:
local lastValue = myList.top()
Reverses the order of the elements in the list in-place.
Example:
myList.reverse()
Returns a new list with only the unique elements from the original list.
Returns:
- (List): The new list with unique elements.
Example:
local myList = List(1, 2, 2, 3, 4, 4, 5, 2, 5)
local uniqueList = myList.unique()
printl(uniqueList) // Output: List(1, 2, 3, 4, 5)
Removes all elements from the list.
Example:
myList.clear()
Joins the elements of the list into a string, separated by the specified delimiter.
Parameters:
separator
(string, optional): The delimiter to use between elements (default is an empty string).
Returns:
- (string): The joined string.
Example:
local joinedString = myList.join(", ")
Applies a function to each element of the list and modifies the list in-place.
Parameters:
func
(function): The function to apply to each element. The function should take one argument (the element value) and return the modified value.
Returns:
- (List): The
List
object itself for method chaining.
Example:
myList.apply(function(x) {
return x * 2 // Double each element
})
Extends the list by appending all elements from another iterable.
Parameters:
other
(iterable): The iterable to append elements from.
Returns:
- (List): The
List
object itself for method chaining.
Example:
local otherList = List(4, 5, 6)
myList.extend(otherList)
Searches for a value or a matching element in the list.
Parameters:
value or func
(any or function): If a value, searches for the first occurrence of that value in the list. If a function, searches for the first element that satisfies the predicate function. The function should take one argument (the element value) and returntrue
if the element is a match,false
otherwise.
Returns:
- (number or null): The index of the first matching element, or
null
if no match is found.
Example:
local index = myList.search(2) // Find the index of the value 2
Creates a new list by applying a function to each element of this list.
Parameters:
func
(function): The function to apply to each element. The function should take one argument (the element value) and return the new value for the element.
Returns:
- (List): The new list with the mapped values.
Example:
local squaredValues = myList.map(function(x) {
return x * x // Square each element
})
Filter the list by a predicate function.
Parameters:
condition
(Function): The predicate function that takesindex
,value
, andnewList
as parameters and returns a boolean.
Returns:
- (List): The filtered list.
Example:
local myList = List(1, 2, 3, 4, 5)
local evenList = myList.filter(function(idx, val, newList) {
return val % 2 == 0
})
printl(evenList) // Output: [2, 4]
Applies a function to an accumulator and each element in the list (from left to right) to reduce it to a single value.
Parameters:
func
(Function): The function to apply to each element and accumulator.initial
(any): The initial value of the accumulator.
Returns:
- (any): The final reduced value.
Example:
local myList = List(1, 2, 3, 4, 5)
local sum = myList.reduce(function(acc, item) {
return acc + item
}, 0)
printl(sum) // Output: 15
Convert the list to a table.
Returns:
- (table): The table representation.
Example:
local myList = List("a", "b", "c")
local myTable = myList.totable()
printl("b" in myTable) // Output: true
printl(myTable) // Output: {"a": null, "b": null, "c": null}
Converts the list to an array.
Returns:
- (array): An array containing the elements of the list.
Example:
local myArray = myList.toarray()
Swaps two nodes in the list. This method updates the references of the previous and next nodes accordingly.
Parameters:
node1
(ListNode): The first node to swap.node2
(ListNode): The second node to swap.
Example:
local nodeA = myList.getNode(0)
local nodeB = myList.getNode(1)
myList.SwapNode(nodeA, nodeB)
This file defines the AVLTree
class, which represents a self-balancing binary search tree (AVL tree). It provides methods for inserting and removing nodes, searching for nodes, and traversing the tree in different orders.
Constructor - Creates a new AVLTree
object with optional initial elements.
Parameters:
...
(any): The initial elements to add to the tree.
Example:
local myTree = AVLTree(5, 2, 8, 1, 3)
Creates a new AVLTree
object from an existing array.
Parameters:
array
(array): The array to create the tree from.
Returns:
- (AVLTree): The new tree containing the elements from the array.
Example:
local myArray = [5, 2, 8, 1, 3]
local myTree = AVLTree.FromArray(myArray)
Gets the number of nodes in the tree.
Returns:
- (number): The number of nodes in the tree.
Example:
local treeSize = myTree.len()
Converts the tree to an array using inorder traversal (ascending order).
Returns:
- (ArrayEx): An array containing the tree nodes in ascending order.
Example:
local sortedArray = myTree.toarray()
Converts the tree to a list using inorder traversal (ascending order).
Returns:
- (List): A list containing the tree nodes in ascending order.
Example:
local sortedList = myTree.tolist()
Inserts a new node with the given key into the tree, maintaining the AVL tree's balance property.
Parameters:
key
(any): The key of the node to insert.
Example:
myTree.insert(6) // Insert a new node with the key 6
Searches for a node with the given value in the tree and returns the node if found.
Parameters:
value
(any): The value to search for.
Returns:
- (treeNode or null): The node with the matching value, or
null
if not found.
Example:
local node = myTree.search(3)
if (node) {
// ...
}
Removes the node with the given value from the tree, maintaining the AVL tree's balance property.
Parameters:
value
(any): The value of the node to remove.
Example:
myTree.remove(2) // Remove the node with the value 2
Returns the minimum value in the tree, which is the value of the leftmost node.
Returns:
- (any): The minimum value in the tree.
Example:
local minValue = myTree.GetMin()
Returns the maximum value in the tree, which is the value of the rightmost node.
Returns:
- (any): The maximum value in the tree.
Example:
local maxValue = myTree.GetMax()
Performs an inorder traversal of the tree and returns an array of nodes in ascending order.
Returns:
- (ArrayEx): An array containing the tree nodes in ascending order.
Example:
local sortedArray = myTree.inorderTraversal()
Prints a visual representation of the tree structure to the console, showing the nodes and their relationships.
Example:
myTree.printTree()
array
: Arrays provide efficient random access to elements by index, making them suitable for situations where you need to frequently access elements by their position. However, inserting or removing elements at arbitrary positions in an array can be inefficient, as it may require shifting other elements to maintain the contiguous order.List
: Linked lists excel at insertion and deletion operations, especially at the beginning or end of the list. This is because adding or removing a node only requires updating the references of the neighboring nodes. However, accessing elements by index in a linked list is less efficient than in an array, as it requires traversing the list from the beginning until the desired index is reached.AVLTree
: AVL trees are self-balancing binary search trees that provide efficient search, insertion, and deletion operations while maintaining a sorted order. They are ideal for situations where you need to maintain a sorted collection of elements and perform frequent searches. However, AVL trees have a more complex structure than arrays or linked lists, which can make them slightly less efficient for simple insertion or deletion operations, especially at arbitrary positions.
The choice between array
, List
, and AVLTree
depends on the specific requirements of your application. Consider the following factors:
- Frequency of access by index: If you need to frequently access elements by their position, an
array
is a good choice. - Frequency of insertions and deletions: If you perform many insertions and deletions, especially at the beginning or end of the collection, a
List
might be more efficient. - Need for sorting: If you need to maintain a sorted collection and perform frequent searches, an
AVLTree
is the best option. - Complexity: If you need a simple data structure with minimal overhead, an
array
orList
might be preferable.
This file defines the entLib
class, which provides helper functions for creating and finding entities.
Creates an entity of the specified classname with the provided key-value pairs and returns it as a pcapEntity
object.
Parameters:
classname
(string): The classname of the entity to create.keyvalues
(table, optional): A table containing key-value pairs to set for the entity (default is an empty table).
Returns:
- (pcapEntity): The created entity as a
pcapEntity
object.
Example:
local myEntity = entLib.CreateByClassname("prop_physics", {
model = "models/my_model.mdl",
health = 100
})
Creates a prop entity with the specified classname, origin, model name, activity, and key-value pairs.
Parameters:
classname
(string): The classname of the prop entity to create (e.g., "prop_physics").origin
(Vector): The initial position of the prop.modelname
(string): The model name of the prop (e.g., "models/my_prop.mdl").activity
(number, optional): The initial activity of the prop (default is 1).keyvalues
(table, optional): A table containing additional key-value pairs to set for the prop (default is an empty table).
Returns:
- (pcapEntity): The created prop entity as a
pcapEntity
object.
Example:
local myProp = entLib.CreateProp("prop_physics", Vector(0, 0, 100), "models/my_prop.mdl")
Wraps a CBaseEntity
object in a pcapEntity
object.
Parameters:
CBaseEntity
(CBaseEntity): TheCBaseEntity
object to wrap.
Returns:
- (pcapEntity): The wrapped entity as a
pcapEntity
object.
Example:
local myPcapEntity = entLib.FromEntity(myEntity)
Finds an entity with the specified classname.
Parameters:
classname
(string): The classname of the entity to find.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local physicsProp = entLib.FindByClassname("prop_physics") // Find the first prop_physics entity
Finds an entity with the specified classname within a given radius from the origin point.
Parameters:
classname
(string): The classname of the entity to find.origin
(Vector): The origin point of the search sphere.radius
(number): The radius of the search sphere.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local nearbyButton = entLib.FindByClassnameWithin("prop_button", myPosition, 50)
// Find a prop_button entity within 50 units of myPosition
Finds an entity with the specified targetname.
Parameters:
targetname
(string): The targetname of the entity to find.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local triggerEntity = entLib.FindByName("my_trigger")
Finds an entity with the specified targetname within a given radius from the origin point.
Parameters:
targetname
(string): The targetname of the entity to find.origin
(Vector): The origin point of the search sphere.radius
(number): The radius of the search sphere.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local nearbyDoor = entLib.FindByNameWithin("exit_door", myPosition, 100)
// Find an entity named "exit_door" within 100 units of myPosition
Finds an entity with the specified model.
Parameters:
model
(string): The model name of the entity to find.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local crateEntity = entLib.FindByModel("models/props_junk/wood_crate001a.mdl")
Finds an entity with the specified model within a given radius from the origin point.
Parameters:
model
(string): The model name of the entity to find.origin
(Vector): The origin point of the search sphere.radius
(number): The radius of the search sphere.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local nearbyCrate = entLib.FindByModelWithin("models/props_junk/wood_crate001a.mdl", myPosition, 50)
// Find a crate entity within 50 units of myPosition
Finds entities within a sphere defined by the origin and radius.
Parameters:
origin
(Vector): The origin point of the search sphere.radius
(number): The radius of the search sphere.start_ent
(CBaseEntity or pcapEntity, optional): The entity to start the search from. If not specified, the search starts from the beginning of the entity list.
Returns:
- (pcapEntity or null): The found entity as a
pcapEntity
object, ornull
if no entity is found.
Example:
local entitiesInSphere = entLib.FindInSphere(myPosition, 100)
// Find entities within 100 units of myPosition
This file defines the pcapEntity
class, which extends the functionality of CBaseEntity
with additional methods for manipulating entity properties, setting outputs, managing user data, and retrieving information about the entity's bounding box and other attributes. Use entLib to create it
Sets the angles (pitch, yaw, roll) of the entity, ensuring that the angles are within the range of 0 to 359 degrees.
Parameters:
x
(number): The pitch angle in degrees.y
(number): The yaw angle in degrees.z
(number): The roll angle in degrees.
Example:
myPcapEntity.SetAngles(45, 90, 0) // Set the entity's angles
Sets the absolute rotation angles of the entity using a Vector.
Parameters:
angles
(Vector): The angle vector representing the desired rotation.
Example:
myPcapEntity.SetAbsAngles(Vector(0, 180, 0)) // Set the entity to face the opposite direction
Destroys the entity, removing it from the game world.
Parameters:
fireDelay
(number, optional): The delay in seconds before destroying the entity (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.Destroy() // Destroy the entity
Kills the entity, triggering its "kill" input with an optional delay.
Parameters:
fireDelay
(number, optional): The delay in seconds before killing the entity (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.Kill(2) // Kill the entity after 2 seconds
Dissolves the entity using an env_entity_dissolver
entity. This creates a visual effect of the entity dissolving away.
Parameters:
fireDelay
(number, optional): The delay in seconds before dissolving the entity (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.Dissolve() // Dissolve the entity
Checks if the entity is valid, meaning that it exists and has not been destroyed or dissolved.
Returns:
- (boolean): True if the entity is valid, false otherwise.
Example:
if (myPcapEntity.IsValid()) {
// The entity is valid and can be used
}
Checks if the entity is the player entity.
Returns:
- (boolean): True if the entity is the player, false otherwise.
Example:
if (myPcapEntity.IsPlayer()) {
// The entity is the player
}
Checks if this entity is equal to another entity based on their entity indices
Parameters:
other
(pcapEntity|CBaseEntity): The other entity to compare.
Returns:
- (boolean): True if the entities are equal, false otherwise.
Gets the eye position of the player entity as a Vector. This is the position from which the player's view is rendered.
Returns:
- (Vector): The eye position of the player, or
null
if the entity is not the player.
Example:
local eyePos = GetPlayerEx().EyePosition()
Gets the eye angles of the player entity as a Vector representing the pitch, yaw, and roll of the player's view.
Returns:
- (Vector): The eye angles of the player, or
null
if the entity is not the player.
Example:
local eyeAngles = GetPlayerEx().EyeAngles()
Gets the forward vector from the player entity's eye position. This represents the direction in which the player is looking.
Returns:
- (Vector): The forward vector from the player's eye position, or
null
if the entity is not the player.
Example:
local eyeForward = GetPlayerEx().EyeForwardVector()
Sets a key-value pair for the entity. This modifies the entity's properties and also stores the value as user data for later retrieval using GetKeyValue
.
Parameters:
key
(string): The key of the key-value pair.value
(any): The value to set for the key.fireDelay
(number, optional): The delay in seconds before setting the key-value pair (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetKeyValue("health", 100) // Set the entity's health to 100
Sets an output for the entity, connecting it to a target entity and input.
Parameters:
outputName
(string): The name of the output to set.target
(string, CBaseEntity, or pcapEntity): The target entity to connect the output to (can be a targetname or an entity object).input
(string): The name of the input on the target entity to connect to.param
(string, optional): An optional parameter override for the input.delay
(number, optional): An optional delay in seconds before triggering the input.fires
(number, optional): The number of times the output should fire (default is -1 for unlimited fires).
Example:
myPcapEntity.AddOutput("OnTrigger", "targetEntity", "Kill", "", 0.5, 1)
// Connect the "OnTrigger" output to the "Kill" input of "targetEntity" with a 0.5-second delay and fire only once
Connects an output to a script function or string with optional delay and trigger count.
Parameters:
outputName
(string or function): The name of the output to connect.script
(string): The VScripts code or function name to execute when the output is triggered.delay
(number, optional): The delay in seconds before executing the script.fires
(number, optional): The number of times the output should fire (default is -1 for unlimited fires).
Example:
myPcapEntity.ConnectOutputEx("OnTrigger", function() {
printl("Output triggered!")
})
Sets an input hook for a specified input name, attaching a closure to be executed when the input is triggered.
Parameters:
inputName
(string): The name of the input to hook.closure
(function): The function to execute when the input is triggered. This function should returntrue
if the input should proceed normally, orfalse
if the input should be blocked.
Example:
local myEntity = ...
myEntity.SetInputHook("Use", function() {
printl("Use input triggered!")
return true // Allow the input to proceed
})
myEntity.SetInputHook("Kill", function() {
printl("Kill input blocked!")
return false // Block the input
})
In this example, when the Use
input is triggered for myEntity
, the specified closure function will be executed, printing "Use input triggered!".
This function provides more control over sound playback compared to EmitSound. It allows you to adjust the volume, loop sounds that are not inherently loopable, and stop the sound playback using StopSoundEx.
Parameters:
soundName
(string): The name of the sound to play.fireDelay
(number, optional): The delay in seconds before playing the sound (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myEntity.EmitSound("ambient/machines/thumper_hit.wav") // Play the sound immediately
myEntity.EmitSound("ambient/machines/thumper_hit.wav", 2) // Play the sound after 2 seconds
Plays a sound on the entity with enhanced control over volume and looping. This method is an improved version of EmitSound
and offers more precise control over sound playback. Unlike the standard EmitSound
, this method allows you to adjust the volume and force looping for any sound, even those not inherently designed to loop.
How it Works:
This method achieves its functionality by creating a hidden prop_physics
entity with the ALWAYS_PRECACHED_MODEL
model. This entity is then attached to the calling entity and used as a dedicated sound emitter. The volume is controlled by manipulating the Z-coordinate of the hidden entity, effectively simulating distance-based attenuation. Looping is achieved by scheduling repeated calls to EmitSound
on the hidden entity. This approach allows for dynamic volume control and looping of any sound, regardless of its original properties.
Parameters:
soundName
(string): The name of the sound to play.volume
(number, optional): The volume of the sound (0-10, default is 10).looped
(boolean, optional): Whether the sound should loop (default is false).fireDelay
(number, optional): The delay in seconds before playing the sound (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myEntity.EmitSoundEx("ambient/machines/thumper_hit.wav", 5, true) // Play the sound at half volume and loop it
myEntity.EmitSoundEx("ambient/machines/thumper_hit.wav", 10, false, 2) // Play the sound at full volume after 2 seconds
Stops a sound previously started with EmitSoundEx
.
Parameters:
soundName
(string): The name of the sound to stop.fireDelay
(number, optional): The delay in seconds before stopping the sound (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myEntity.StopSoundEx("ambient/machines/thumper_hit.wav") // Stop the specified sound
Sets the targetname of the entity.
Parameters:
name
(string): The targetname to set.fireDelay
(number, optional): The delay in seconds before setting the name (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetName("my_entity") // Set the entity's targetname
Sets a unique targetname for the entity using the provided prefix and a generated unique identifier.
Parameters:
prefix
(string, optional): The prefix to use for the unique targetname (default is "pcapEnt").fireDelay
(number, optional): The delay in seconds before setting the unique name (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetUniqueName("my_entity") // Set a unique targetname starting with "my_entity_"
Sets the parent entity for the entity, establishing a parent-child relationship.
Parameters:
parentEnt
(string, CBaseEntity, or pcapEntity): The parent entity to set (can be a targetname or an entity object).fireDelay
(number, optional): The delay in seconds before setting the parent (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
local parentEntity = entLib.FindByClassname("prop_static")
myPcapEntity.SetParent(parentEntity) // Set the parent entity
Gets the parent entity of the entity, if set.
Returns:
- (pcapEntity or null): The parent entity as a
pcapEntity
object, ornull
if no parent is set.
Example:
local parent = myPcapEntity.GetParent()
if (parent) {
// ... do something with the parent entity
}
Sets the collision type of the entity, controlling how it interacts with other entities in the game world.
Parameters:
solidType
(number): The collision type to set (see the Source SDK documentation for valid values).fireDelay
(number, optional): The delay in seconds before setting the collision type (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetCollision(SOLID_NONE) // Make the entity non-solid (pass through other entities)
Sets the collision group of the entity, determining which other entities it can collide with.
Parameters:
collisionGroup
(number): The collision group to set (see the Source SDK documentation for valid values).fireDelay
(number, optional): The delay in seconds before setting the collision group (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetCollisionGroup(COLLISION_GROUP_PLAYER) // Set the collision group to the player group
Starts playing the specified animation on the entity.
Parameters:
animationName
(string): The name of the animation to play.fireDelay
(number, optional): The delay in seconds before starting the animation (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetAnimation("run") // Start playing the "run" animation
Sets the alpha (opacity) of the entity, controlling how transparent it appears.
Parameters:
opacity
(number): The alpha value between 0 (fully transparent) and 255 (fully opaque).fireDelay
(number, optional): The delay in seconds before setting the alpha (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetAlpha(128) // Set the entity to be semi-transparent
Sets the color of the entity.
Parameters:
colorValue
(string or Vector): The color value as a string (e.g., "255 0 0" for red) or a Vector with components (r, g, b).fireDelay
(number, optional): The delay in seconds before setting the color (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetColor("0 255 0") // Set the entity to green
Sets the skin of the entity, if the entity has multiple skins available.
Parameters:
skin
(number): The index of the skin to set.fireDelay
(number, optional): The delay in seconds before setting the skin (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetSkin(2) // Set the entity's skin to the third available skin
Enables or disables rendering of the entity, controlling whether it is visible in the game world.
Parameters:
isEnabled
(boolean): True to enable rendering (make the entity visible), false to disable rendering (make the entity invisible).fireDelay
(number, optional): The delay in seconds before setting the draw state (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetDrawEnabled(false) // Make the entity invisible
Disables the entity, making it invisible and preventing interactions. Equivalent to calling SetDrawEnabled(false)
and SetTraceIgnore(true)
.
Parameters:
fireDelay
(number, optional): The delay in seconds before disabling the entity (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.Disable() // Disable the entity
Enables the entity, making it visible and allowing interactions. Equivalent to calling SetDrawEnabled(true)
and SetTraceIgnore(false)
.
Parameters:
fireDelay
(number, optional): The delay in seconds before enabling the entity (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.Enable() // Enable the entity
Checks if the entity is set to be drawn (visible).
Returns:
- (boolean): True if the entity is set to be drawn, false otherwise.
Example:
if (myPcapEntity.IsDrawEnabled()) {
// The entity is visible
}
Sets whether the entity should be ignored during traces. This can be used to prevent the entity from blocking traces or being detected by them.
Parameters:
isEnabled
(boolean): True to ignore the entity during traces, false otherwise.fireDelay
(number, optional): The delay in seconds before setting the trace ignore state (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetTraceIgnore(true) // Ignore the entity during traces
Sets the spawnflags of the entity, which can affect its behavior and properties.
Parameters:
flag
(number): The spawnflags value to set.fireDelay
(number, optional): The delay in seconds before setting the spawnflags (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetSpawnflags(128) // Set the NPC_GAG spawnflag for an NPC entity
Sets the scale of the entity's model, making it larger or smaller.
Parameters:
scaleValue
(number): The scale value to set.fireDelay
(number, optional): The delay in seconds before setting the model scale (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetModelScale(2) // Double the size of the entity's model
Gets the current model scale of the entity.
Returns:
- (number): The current model scale value.
Example:
local currentScale = myPcapEntity.GetModelScale()
Sets the center of the entity's bounding box. This effectively moves the entity while maintaining its orientation.
Parameters:
vector
(Vector): The new center position for the entity's bounding box.
Example:
myPcapEntity.SetCenter(Vector(10, 20, 30)) // Set the center of the entity's bounding box
Sets the bounding box of the entity using vectors or string representations of vectors.
Parameters:
minBounds
(Vector or string): The minimum bounds of the bounding box as a Vector or a string representation (e.g., "-5 -5 -5").maxBounds
(Vector or string): The maximum bounds of the bounding box as a Vector or a string representation (e.g., "5 5 5").
Example:
myPcapEntity.SetBBox(Vector(-10, -10, -10), Vector(10, 10, 10)) // Set a bounding box of 20x20x20 units
Sets a context value for the entity, which can be used for storing additional information or state associated with the entity.
Parameters:
name
(string): The name of the context value.value
(any): The value to set for the context.fireDelay
(number, optional): The delay in seconds before setting the context value (default is 0).eventName
(string, optional): The name of the event to schedule for (default is "global").
Example:
myPcapEntity.SetContext("activated", true) // Set a context value indicating that the entity has been activated
Stores an arbitrary value associated with the entity using a name. This is a general-purpose mechanism for attaching custom data to entities.
Parameters:
name
(string): The name of the user data value.value
(any): The value to store.
Example:
myPcapEntity.SetUserData("myData", {count = 10, message = "Hello"})
Retrieves a stored user data value by name.
Parameters:
name
(string): The name of the user data value to retrieve.
Returns:
- (any or null): The stored value, or
null
if no value is found with the given name.
Example:
local myData = myPcapEntity.GetUserData("myData")
macros.PrintIter(myData)
Returns the bounding box of the entity as a table with two Vector properties: min
and max
, representing the minimum and maximum extents of the box.
Returns:
- (table): A table with
min
andmax
Vector properties representing the bounding box.
Example:
local bbox = myPcapEntity.GetBBox()
printl("Bounding box min:", bbox.min)
printl("Bounding box max:", bbox.max)
This function checks if the bounding box of an entity is a cube, meaning all sides of the bounding box have equal length.
Returns:
- (boolean): True if the bounding box is a cube, false otherwise.
Example:
local isCube = entity.IsSquareBbox()
if (isCube) {
// Bounding box is a cube
} else {
// Bounding box is not a cube
}
Returns the axis-aligned bounding box (AABB) of the entity, which is a box that is aligned with the world axes and tightly encloses the entity.
Returns:
- (table): A table with three Vector properties:
min
,max
, andcenter
, representing the minimum and maximum extents of the AABB, as well as its center point.
Example:
local aabb = myPcapEntity.GetAABB()
printl("AABB min:", aabb.min)
printl("AABB max:", aabb.max)
printl("AABB center:", aabb.center)
Returns the index of the entity, which is a unique identifier for the entity within the game.
Returns:
- (number): The index of the entity.
Example:
local entityIndex = myPcapEntity.GetIndex()
Returns the value of a key-value pair for the entity, if it was set using the SetKeyValue
method or other pcapEntity
methods. It cannot retrieve values set directly through Hammer or the console.
Parameters:
key
(string): The key of the key-value pair to retrieve.
Returns:
- (any or null): The value associated with the key, or
null
if the key is not found.
Example:
local healthValue = myPcapEntity.GetKeyValue("health")
Returns the spawnflags of the entity, if they were set using the SetSpawnflags
method.
Returns:
- (number or null): The spawnflags value, or
null
if the spawnflags were not set usingSetSpawnflags
.
Example:
local spawnflags = myPcapEntity.GetSpawnflags()
Returns the alpha (opacity) value of the entity, if it was set using the SetAlpha
method.
Returns:
- (number or null): The alpha value, or 255 if the alpha was not set using
SetAlpha
.
Example:
local alpha = myPcapEntity.GetAlpha()
Returns the color of the entity as a string, if it was set using the SetColor
method.
Returns:
- (string or null): The color string in format "R G B", or "255 255 255" if the color was not set using
SetColor
.
Example:
local color = myPcapEntity.GetColor()
Returns the skin index of the entity, if it was set using the SetSkin
method.
Returns:
- (number or null): The skin index, or 0 if the skin was not set using
SetSkin
.
Example:
local skin = myPcapEntity.GetSkin()
Returns the prefix of the entity's name, if the name contains a "-" separator.
Returns:
- (string): The prefix of the entity name, or the entire name if no "-" separator is found.
Example:
local prefix = myPcapEntity.GetNamePrefix()
Returns the postfix of the entity's name, if the name contains a "-" separator.
Returns:
- (string): The postfix of the entity name, or the entire name if no "-" separator is found.
Example:
local postfix = myPcapEntity.GetNamePostfix()
Retrieves the portal partner instance of the entity, prioritizing the actual partner entity if available, and falling back to user-stored partner data if necessary.
Returns:
- (Entity|null): The partner entity, or null if no partner is found.
Example:
local partner = myPortalEntity.GetPartnerInstance()
if (partner) {
// ... do something with the partner entity
}
Returns a specific face of the entity's oriented bounding box (AABB) as a vector. This is required for GetAABB
Parameters:
stat
(number): An index indicating which face of the AABB to return (0 for min bounds, 7 for max bounds, 4 for center).
Returns:
- (Vector): The vector representing the specified face of the AABB.
Example:
local minBounds = myPcapEntity.CreateAABB(0) // Get the minimum bounds of the AABB
Returns an array of vectors representing the 8 vertices of the entity's axis-aligned bounding box (AABB). This is required for CreateAABB
Returns:
- (array): An array of 8 Vectors, each representing a vertex of the AABB.
Example:
local vertices = myPcapEntity.getBBoxPoints() // Get the vertices of the AABB
This method retrieves the faces of an entity's bounding box as an array of triangle vertices. It is used to access and work with the individual triangular faces that make up the bounding box.
Returns:
- (array): An array of 12 Vector triplets, where each triplet represents the three vertices of a triangle face.
Example:
local myEntity = entLib.FindByClassname("prop_physics")
local faces = myEntity.getBBoxFaces()
// Iterate over the faces and print the vertices of each triangle
foreach(i, face in faces ) {
printl("Face", i, ":")
foreach(j, vertex in face.vertices) {
printl(" Vertex", j, ":", vertex)
}
}