# utils

## Functions:

### console\_exec

`utils.console_exec(cmd: string[, ...])`

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>cmd</strong></td><td><strong><code>string</code></strong></td><td>The console command(s) to execute</td></tr><tr><td><strong>...</strong></td><td></td><td>Comma-separated arguments to concatenate with <strong>cmd</strong></td></tr></tbody></table>

Executes a console command. Multiple commands can be combined with ';'. Be careful when passing user input (including usernames) to it.

### execute\_after

`utils.execute_after(delay: number, callback: function[, ...])`

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>delay</strong></td><td><strong><code>number</code></strong></td><td>Time in seconds to wait before calling callback</td></tr><tr><td><strong>callback</strong></td><td><strong><code>function</code></strong></td><td>The Lua function that will be called after the delay</td></tr><tr><td><strong>...</strong></td><td></td><td>Arguments that will be passed to the callback</td></tr></tbody></table>

Executes the callback after delay seconds, passing the arguments to it.

### net\_channel

`utils.net_channel():` <mark style="color:purple;">`NetChannel`</mark>

Returns the [`NetChannel`](#netchannel) struct.

### trace\_line

{% embed url="<https://developer.valvesoftware.com/wiki/UTIL_TraceLine>" %}

`utils.trace_line(from: vector, to: vector[, skip: entity/table/function, mask: number, type: number]):` <mark style="color:purple;">`trace`</mark>

<table><thead><tr><th width="150">Name</th><th width="175.17772006464614">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>from</strong></td><td><strong><code>vector</code></strong></td><td>Vector to start tracing from</td></tr><tr><td><strong>to</strong></td><td><strong><code>vector</code></strong></td><td>Vector to trace to</td></tr><tr><td><strong>skip</strong></td><td><strong><code>entity</code></strong>, <strong><code>table</code></strong>, <strong><code>function</code></strong></td><td>Optional. Entity skipping options</td></tr><tr><td><strong>mask</strong></td><td><strong><code>number</code></strong></td><td>Optional. Trace mask</td></tr><tr><td><strong>type</strong></td><td><strong><code>number</code></strong></td><td>Optional. Trace type [0-3]<br><mark style="color:blue;"><code>0</code></mark>: Trace everything <code>[Default]</code><br><mark style="color:blue;"><code>1</code></mark>: Trace world only<br><mark style="color:blue;"><code>2</code></mark>: Trace entities only<br><mark style="color:blue;"><code>3</code></mark>: Trace everything filter props</td></tr></tbody></table>

> 📌 The `skip` argument can either be an `entity` object, a table with `entity` objects, or a function, like the ShouldHitEntity callback. If you use it as a callback, you can access the `entity` and `contents_mask` by adding them to the function arguments. Inside the callback, return true if tracing should not skip the entity, otherwise return false.

Returns a [`trace`](#struct-trace) struct containing all the information.

### trace\_hull

`utils.trace_hull(from: vector, to: vector, mins: vector, maxs: vector[, skip: entity/table/function, mask: number, type: number]):` <mark style="color:purple;">`trace`</mark>

<table><thead><tr><th width="150">Name</th><th width="174.17772006464614">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>from</strong></td><td><strong><code>vector</code></strong></td><td>Vector to start tracing from</td></tr><tr><td><strong>to</strong></td><td><strong><code>vector</code></strong></td><td>Vector to trace to</td></tr><tr><td><strong>mins</strong></td><td><strong><code>vector</code></strong></td><td>Hull mins</td></tr><tr><td><strong>maxs</strong></td><td><strong><code>vector</code></strong></td><td>Hull maxs</td></tr><tr><td><strong>skip</strong></td><td><strong><code>entity</code></strong>, <strong><code>table</code></strong>, <strong><code>function</code></strong></td><td>Optional. Entity skipping options</td></tr><tr><td><strong>mask</strong></td><td><strong><code>number</code></strong></td><td>Optional. Trace mask</td></tr><tr><td><strong>type</strong></td><td><strong><code>number</code></strong></td><td>Optional. Trace type [0-3]<br><mark style="color:blue;"><code>0</code></mark>: Trace everything <code>[Default]</code><br><mark style="color:blue;"><code>1</code></mark>: Trace world only<br><mark style="color:blue;"><code>2</code></mark>: Trace entities only<br><mark style="color:blue;"><code>3</code></mark>: Trace everything filter props</td></tr></tbody></table>

> 📌 The `skip` argument can either be an `entity` object, a table with `entity` objects, or a function, like the ShouldHitEntity callback. If you use it as a callback, you can access the `entity` and `contents_mask` by adding them to the function arguments. Inside the callback, return true if tracing should not skip the entity, otherwise return false.

Returns a [`trace`](#struct-trace) struct containing all the information.

### trace\_bullet

`utils.trace_bullet(from_entity: entity, from: vector, to: vector[, skip: entity/table/function]):` <mark style="color:purple;">`number`</mark>, <mark style="color:purple;">`trace`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>from_entity</strong></td><td><strong><code>entity</code></strong></td><td>Player whose weapon will be used for this trace</td></tr><tr><td><strong>from</strong></td><td><strong><code>vector</code></strong></td><td>Vector to start tracing from</td></tr><tr><td><strong>to</strong></td><td><strong><code>vector</code></strong></td><td>Vector to trace to</td></tr><tr><td><strong>skip</strong></td><td><strong><code>entity</code></strong>, <strong><code>table</code></strong>, <strong><code>function</code></strong></td><td>Optional. Entity skipping options. If not passed, the skip entity will be <code>from_entity</code></td></tr></tbody></table>

> 📌 The `skip` argument can either be an `entity` object, a table with `entity` objects, or a function, like the ShouldHitEntity callback. If you use it as a callback, you can access the `entity` and `contents_mask` by adding them to the function arguments. Inside the callback, return true if tracing should not skip the entity, otherwise return false.

Returns the `damage`, [`trace`](#struct-trace) arguments.

#### 🔗 struct <mark style="color:blue;">`trace`</mark>

<table><thead><tr><th width="221.87840137519683">Name</th><th width="150">Type</th><th width="341.1829205510523">Description</th></tr></thead><tbody><tr><td><strong>start_pos</strong></td><td><strong><code>vector</code></strong></td><td>Start position</td></tr><tr><td><strong>end_pos</strong></td><td><strong><code>vector</code></strong></td><td>Final position</td></tr><tr><td><strong>plane</strong></td><td><strong><code>table</code></strong></td><td>Surface normal at impact. Contains <mark style="color:blue;"><code>normal</code></mark>, <mark style="color:blue;"><code>dist</code></mark>, <mark style="color:blue;"><code>type</code></mark>, and <mark style="color:blue;"><code>signbits</code></mark> values</td></tr><tr><td><strong>fraction</strong></td><td><strong><code>number</code></strong></td><td>Percentage in the range [0.0, 1.0]. How far the trace went before hitting something. <mark style="color:blue;"><code>1.0</code></mark> - didn't hit anything</td></tr><tr><td><strong>contents</strong></td><td><strong><code>number</code></strong></td><td>Contents on other side of surface hit</td></tr><tr><td><strong>disp_flags</strong></td><td><strong><code>number</code></strong></td><td>Displacement flags for marking surfaces with data</td></tr><tr><td><strong>all_solid</strong></td><td><strong><code>boolean</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if the plane is invalid</td></tr><tr><td><strong>start_solid</strong></td><td><strong><code>boolean</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if the initial point was in a solid area</td></tr><tr><td><strong>fraction_left_solid</strong></td><td><strong><code>number</code></strong></td><td>Percentage in the range [0.0, 1.0]. How far the trace went before leaving solid. Only valid if we started in solid</td></tr><tr><td><strong>surface</strong></td><td><strong><code>table</code></strong></td><td>Surface hit (impact surface). Contains <mark style="color:blue;"><code>name</code></mark>, <mark style="color:blue;"><code>props</code></mark>, and <mark style="color:blue;"><code>flags</code></mark> values</td></tr><tr><td><strong>hitgroup</strong></td><td><strong><code>number</code></strong></td><td><mark style="color:blue;"><code>0</code></mark> - generic, non-zero is specific body part</td></tr><tr><td><strong>physics_bone</strong></td><td><strong><code>number</code></strong></td><td>Physics bone that was hit by the trace</td></tr><tr><td><strong>world_surface_index</strong></td><td><strong><code>number</code></strong></td><td>Index of the msurface2_t, if applicable</td></tr><tr><td><strong>entity</strong></td><td><strong><code>entity</code></strong></td><td>Entity that was hit by the trace</td></tr><tr><td><strong>hitbox</strong></td><td><strong><code>number</code></strong></td><td>Box that was hit by the trace</td></tr><tr><td><strong>did_hit</strong></td><td><strong><code>function</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if there was any kind of impact at all</td></tr><tr><td><strong>did_hit_world</strong></td><td><strong><code>function</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if the <code>entity</code> points at the world entity</td></tr><tr><td><strong>did_hit_non_world</strong></td><td><strong><code>function</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if the trace hit something and it wasn't the world</td></tr><tr><td><strong>is_visible</strong></td><td><strong><code>function</code></strong></td><td>Returns <mark style="color:purple;"><code>true</code></mark> if the final position is visible</td></tr></tbody></table>

### opcode\_scan

`utils.opcode_scan(module: string, signature: string[, offset: number]):` <mark style="color:purple;">`userdata`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>module</strong></td><td><strong><code>string</code></strong></td><td>Module name, in which the signature will be scanned.</td></tr><tr><td><strong>signature</strong></td><td><strong><code>string</code></strong></td><td>IDA style signature, the wildcard is "<mark style="color:yellow;"><code>?</code></mark>"</td></tr><tr><td><strong>offset</strong></td><td><strong><code>number</code></strong></td><td>Optional offset to apply to the pointer address.</td></tr></tbody></table>

Returns a pointer to the specified pattern if it was found. Otherwise returns `nil`.

### create\_interface

`utils.create_interface(module: string, interface: string):` <mark style="color:purple;">`userdata`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>module</strong></td><td><strong><code>string</code></strong></td><td>Module name containing the interface.</td></tr><tr><td><strong>interface</strong></td><td><strong><code>string</code></strong></td><td>Interface name.</td></tr></tbody></table>

Returns a pointer to the specified interface if it was found. Otherwise returns `nil`.

### get\_netvar\_offset

`utils.get_netvar_offset(table: string, prop: string):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>table</strong></td><td><strong><code>string</code></strong></td><td>Datatable name</td></tr><tr><td><strong>prop</strong></td><td><strong><code>string</code></strong></td><td>Prop name</td></tr></tbody></table>

Returns the offset of the specified prop. Can be used to manually navigate to the net prop.

### get\_vfunc

`utils.get_vfunc(index: number, ...):` <mark style="color:purple;">`function`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>index</strong></td><td><strong><code>number</code></strong></td><td>Virtual table index of the function.</td></tr><tr><td><strong>...</strong></td><td></td><td>FFI C Type definition.</td></tr></tbody></table>

Creates and returns a wrapper function that calls a virtual table function on the specified index.

### get\_vfunc

`utils.get_vfunc(module: string, interface: string, index: number, ...):` <mark style="color:purple;">`function`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>module</strong></td><td><strong><code>string</code></strong></td><td>Module name containing the interface.</td></tr><tr><td><strong>interface</strong></td><td><strong><code>string</code></strong></td><td>Interface name.</td></tr><tr><td><strong>index</strong></td><td><strong><code>number</code></strong></td><td>Virtual table index of the function.</td></tr><tr><td><strong>...</strong></td><td></td><td>FFI C Type definition.</td></tr></tbody></table>

Creates and returns a wrapper function that calls a virtual table function from the specified interface on the specified index.

### random\_int

`utils.random_int(min: number, max: number):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>min</strong></td><td><strong><code>number</code></strong></td><td>Minimum boundary for the random value, included</td></tr><tr><td><strong>max</strong></td><td><strong><code>number</code></strong></td><td>Maximum boundary for the random value, included</td></tr></tbody></table>

Returns a random integer value.

### random\_float

`utils.random_float(min: number, max: number):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>min</strong></td><td><strong><code>number</code></strong></td><td>Minimum boundary for the random value, included</td></tr><tr><td><strong>max</strong></td><td><strong><code>number</code></strong></td><td>Maximum boundary for the random value, included</td></tr></tbody></table>

Returns a random float value.

### random\_seed

`utils.random_seed(seed: number)`

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>seed</strong></td><td><strong><code>number</code></strong></td><td>New random seed value</td></tr></tbody></table>

Sets the new random seed.

## 🔗 <mark style="color:blue;">`NetChannel`</mark>

{% hint style="info" %}
Access the struct via [<mark style="color:purple;">`.net_channel`</mark>](#net_channel)function

* `FLOW:`<mark style="color:green;">`OUTGOING`</mark>` ``= 0`
* `FLOW:`<mark style="color:green;">`INCOMING`</mark>` ``= 1`
  {% endhint %}

### time

`net.time` `:` <mark style="color:purple;">`number`</mark>

Current network time.

### time\_connected

`net.time_connected` `:` <mark style="color:purple;">`number`</mark>

Connection time in seconds.

### time\_since\_last\_received

`net.time_since_last_received` `:` <mark style="color:purple;">`number`</mark>

Time since last received packet in seconds.

### is\_loopback

`net.is_loopback` `:` <mark style="color:purple;">`boolean`</mark>

Returns <mark style="color:green;">`true`</mark> if server is a loopback (local server).

### is\_playback

`net.is_playback` `:` <mark style="color:purple;">`boolean`</mark>

Returns <mark style="color:green;">`true`</mark> if demo is being played.

### is\_timing\_out

`net.is_timing_out` `:` <mark style="color:purple;">`boolean`</mark>

Returns <mark style="color:green;">`true`</mark> if client is timing out.

### sequence\_nr\[<mark style="color:blue;">`flow`</mark>]

`net.sequence_nr[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.sequence_nr[1]` `:` <mark style="color:purple;">`number`</mark>

Last sent sequence number.

### latency\[<mark style="color:blue;">`flow`</mark>]

`net.latency[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.latency[1]` `:` <mark style="color:purple;">`number`</mark>

Current latency (RTT), more accurate but jittering.

### avg\_latency\[<mark style="color:blue;">`flow`</mark>]

`net.avg_latency[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.avg_latency[1]` `:` <mark style="color:purple;">`number`</mark>

Average latency in seconds.

### loss\[<mark style="color:blue;">`flow`</mark>]

`net.loss[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.loss[1]` `:` <mark style="color:purple;">`number`</mark>

Percentage in the range \[0.0, 1.0] of the current packet loss.

### choke\[<mark style="color:blue;">`flow`</mark>]

`net.choke[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.choke[1]` `:` <mark style="color:purple;">`number`</mark>

Percentage in the range \[0.0, 1.0] of the current packet choke.

### packets\[<mark style="color:blue;">`flow`</mark>]

`net.packets[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.packets[1]` `:` <mark style="color:purple;">`number`</mark>

Average amount of packets/sec.

### data\[<mark style="color:blue;">`flow`</mark>]

`net.data[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.data[1]` `:` <mark style="color:purple;">`number`</mark>

Data flow in bytes/sec.

### total\_packets\[<mark style="color:blue;">`flow`</mark>]

`net.total_packets[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.total_packets[1]` `:` <mark style="color:purple;">`number`</mark>

Total amount of packets/sec.

### total\_data\[<mark style="color:blue;">`flow`</mark>]

`net.total_data[0]` `:` <mark style="color:purple;">`number`</mark>\
`net.total_data[1]` `:` <mark style="color:purple;">`number`</mark>

Total data flow in bytes/sec.

### :get\_server\_info

`net:get_server_info():` <mark style="color:purple;">`table`</mark>

Returns a table containing <mark style="color:blue;">`rate`</mark>, <mark style="color:blue;">`name`</mark>, <mark style="color:blue;">`address`</mark>, <mark style="color:blue;">`frame_time`</mark>, and <mark style="color:blue;">`deviation`</mark> (or nil on failure).

### :is\_valid\_packet

`net:is_valid_packet(flow: number, frame: number):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>flow</strong></td><td><strong><code>number</code></strong></td><td>Channel (Flow)</td></tr><tr><td><strong>frame</strong></td><td><strong><code>number</code></strong></td><td>Sequence number</td></tr></tbody></table>

Returns <mark style="color:green;">`true`</mark> if the packet is valid.

### :get\_packet\_time

`net:get_packet_time(flow: number, frame: number):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>flow</strong></td><td><strong><code>number</code></strong></td><td>Channel (Flow)</td></tr><tr><td><strong>frame</strong></td><td><strong><code>number</code></strong></td><td>Sequence number</td></tr></tbody></table>

Returns the time when the packet was sent.

### :get\_packet\_bytes

`net:get_packet_bytes(flow: number, frame: number, group: number):` <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>flow</strong></td><td><strong><code>number</code></strong></td><td>Channel (Flow)</td></tr><tr><td><strong>frame</strong></td><td><strong><code>number</code></strong></td><td>Sequence number</td></tr><tr><td><strong>group</strong></td><td><strong><code>number</code></strong></td><td>Group of this packet</td></tr></tbody></table>

Returns the group size of this packet.

### :get\_packet\_response\_latency

`net:get_packet_response_latency(flow: number, frame: number):` <mark style="color:purple;">`number`</mark>, <mark style="color:purple;">`number`</mark>

<table><thead><tr><th width="150">Name</th><th width="162.52330706200414">Type</th><th width="410.3276962436035">Description</th></tr></thead><tbody><tr><td><strong>flow</strong></td><td><strong><code>number</code></strong></td><td>Channel (Flow)</td></tr><tr><td><strong>frame</strong></td><td><strong><code>number</code></strong></td><td>Sequence number</td></tr></tbody></table>

Returns `latency_msecs`, `choke` of this packet.
