Skip to content

Net

Networking helpers for Lilia networked variables, chunked table transfer, lightweight send caching, and network traffic profiling.


Overview

The net library centralizes shared networking helpers under `lia.net`. It provides cache markers for repeated message work, sends and receives large compressed tables in chunks, manages global networked variables, synchronizes networked data on player join or entity cleanup, and wraps Garry's Mod net functions with optional profiling output.

lia.net.isCacheHit(name, args)View Source

Purpose

Checks whether a named network cache entry exists and is still within the cache time-to-live window.

Realm

Shared

Parameters

string name The cache namespace or message name used when the entry was created.

table args Ordered arguments used to build the cache key.

Returns

boolean|nil True when the cache entry exists and has not expired. False or nil otherwise.

Example Usage

  if not lia.net.isCacheHit("SyncInventory", {client, itemID}) then
      lia.net.addToCache("SyncInventory", {client, itemID})
  end

lia.net.addToCache(name, args)View Source

Purpose

Adds or refreshes a named network cache entry and removes expired or excess cache entries.

Realm

Shared

Parameters

string name The cache namespace or message name to store.

table args Ordered arguments used to build the cache key.

Example Usage

  lia.net.addToCache("SyncInventory", {client, itemID})

lia.net.readBigTable(netStr, callback)View Source

Purpose

Registers a receiver for a chunked, compressed JSON table stream and rebuilds the table once every chunk has arrived.

Realm

Shared

Parameters

string netStr The network message name used for the chunk stream.

function callback optional Function called after the table is reconstructed. On the server it receives `(Player client, table data)`. On the client it receives `(table data)`.

Example Usage

  lia.net.readBigTable("liaLargePayload", function(client, data)
      print(client, data)
  end)

lia.net.writeBigTable(targets, netStr, tbl, chunkSize)View Source

Purpose

Sends a table to one or more clients by JSON-encoding, compressing, chunking, and streaming it over a network message.

Realm

Server

Parameters

Player|table targets optional The player, player table, or nil target set. If no valid target is provided, all human players receive the stream.

string netStr The network message name used for the chunk stream.

table tbl The table to serialize and send.

number chunkSize optional Optional maximum chunk size. The value is clamped and adjusted during reloads.

Example Usage

  lia.net.writeBigTable(client, "liaLargePayload", data)
  lia.net.writeBigTable(player.GetHumans(), "liaLargePayload", data, 1024)

lia.net.checkBadType(name, object)View Source

Purpose

Validates that a networked value does not contain functions, including functions nested inside tables.

Realm

Server

Parameters

string name The networked variable name being validated.

any object The value or nested table key/value currently being inspected.

Returns

boolean|nil True when an unsupported function value is found. Nil otherwise.

Example Usage

  if lia.net.checkBadType("score", value) then return end

lia.net.setNetVar(key, value, receiver)View Source

Purpose

Sets a global networked variable, broadcasts or sends the change to clients, and runs the `NetVarChanged` hook.

Realm

Server

Parameters

string key The global networked variable key to set.

any value The value to store and send. Functions are rejected, including functions nested in tables.

Player|table receiver optional Optional recipient or recipient table. When omitted, the change is broadcast to all clients.

Example Usage

  lia.net.setNetVar("roundState", "active")
  lia.net.setNetVar("eventName", "cleanup", client)

lia.net.getNetVar(key, default)View Source

Purpose

Gets a global networked variable value from the local global networked variable store.

Realm

Shared

Parameters

string key The global networked variable key to read.

any default Value returned when the key has not been set.

Returns

any The stored value when present, otherwise the provided default value.

Example Usage

  local state = lia.net.getNetVar("roundState", "waiting")

lia.net.profiler.log(direction, messageName, size, sender, receiver)View Source

Purpose

Records and prints a network profiler entry for a message when the profiler is active.

Realm

Shared

Parameters

string direction Message direction label, such as `S->C` or `C->S`.

string messageName The network message name being logged.

number size The message size value supplied by the wrapper. Send wrappers pass `net.BytesWritten()`, while receive wrappers pass the `len` argument from `net.Receive`.

Player|string sender optional The sending player or sender label.

Player|string receiver optional The receiving player or receiver label.

Example Usage

  lia.net.profiler.log("S->C", "liaGlobalVar", net.BytesWritten() or 0, "SERVER", client)

Hooks

Library-specific hooks documented for this library.


NetVarChanged(entity, key, oldValue, newValue)View Source

Purpose

Runs after a networked variable changes. This file triggers it for global networked variables with `entity` set to nil.

Realm

Server

Parameters

Entity entity optional The entity whose networked variable changed, or nil when a global networked variable changed.

string key The networked variable key that changed.

any oldValue The previous value stored for the key.

any newValue The new value stored for the key.