Library net

Networking library. More reliable and flexible than usermessages. Allows for networking server to client or client to server.

Functions

broadcast() Broadcasts the currently stored network message to every player.
bytesLeft() Returns the number of bytes left in the current net message.
bytesWritten() Returns the number of bytes currently written.
canSend( target ) Indicates whether a net message can be sent at this time.
getDTVar( type, index, chip ) Retrieves a DataTable value from the chip.
getMaxDTVarIndex( type, chip ) Returns the maximum index available for the given DTVar type, or 0 if the given type is invalid.
listenForAllDTVarChanges() Enables change detection for all DataTable values on the chip.
listenForDTVarChanges( type, index ) Enables change detection for the given DataTable value.
quotaMax() Returns the max amount of bytes/s allowed.
quotaUsed( target ) Returns the amount of bytes in the net quota currently used.
readAngle( lowPrecision ) Reads an angle from the net message.
readBit() Reads a bit from the net message.
readBool() Reads a boolean from the net message.
readData( length ) Reads binary data from the net message.
readDouble() Reads a double precision float from the net message.
readEntity() Reads an entity from the net message.
readFloat( double ) Reads a float from the net message.
readInt( bits, unsigned ) Reads an integer from the net message.
readMatrix() Reads a VMatrix from the net message.
readNormal() Reads a normalised vector from the net message.
readString() Reads a string from the net message.
readTable() Reads a table from the net message.
readType( type ) Reads a value from the net message of the given type enum.
readUInt( bits ) Reads an unsigned integer from the net message.
readVector( lowPrecision ) Reads a vector from the net message.
send( ply ) Sends the currently stored net message to the given player.
sendOmit( plys ) Sends the currently stored network message to every player except those you pass in.
sendPAS( pos ) Sends the currently stored network message to every player who has the given position in their PAS (Potentially Audible Set)
sendPVS( pos ) Sends the currently stored network message to every player who has the given position in their PVS (Potentially Visible Set).
sendToServer() Sends the currently stored network message to the server.
setDTVar( type, index, value, chip ) Sets a DataTable value on the given chip entity.
start( ent, unreliable ) Starts a net message.
writeAngle( ang, lowPrecision ) Writes an angle to the net message.
writeBit( bit ) Writes a bit to the current net message.
writeBool( bool ) Writes a boolean to the current message.
writeData( data, length ) Writes binary data to the net message.
writeDouble( double ) Writes a double precision float to the current net message.
writeEntity( ent ) Writes an entity to the net message.
writeFloat( float, double ) Writes a float to the current net message.
writeInt( int, bits, unsigned ) Writes an integer to the current net message.
writeMatrix( matrix ) Writes a matrix to the net message.
writeNormal( normal ) Writes a normalised vector to the net message.
writeString( string ) Writes a string to the net message.
writeTable( table ) Writes an entire table with any arbitrary structure to the net message.
writeType( value ) Writes the value's type, then its value to the net message.
writeUInt( int, bits ) Writes an unsigned integer to the current net message.
writeVector( vec, lowPrecision ) Writes a vector to the net message.


Functions

net.broadcast()
Broadcasts the currently stored network message to every player.

Environment:

Server
net.bytesLeft()
Returns the number of bytes left in the current net message. Only works inside the net hook.

Environment:

Shared

Return values:

  1. (number) The number of bytes remaining to read in the current net message.
  2. (number) The number of bits remaining to read in the current net message.
net.bytesWritten()
Returns the number of bytes currently written.

Environment:

Shared

Return values:

  1. (number) The number of bytes written to the current message.
  2. (number) The number of bits written to the current message.
net.canSend( target )
Indicates whether a net message can be sent at this time. If the target is omitted on the server, then this will return false if there is at least one player that cannot have a message sent to them at this time.

Environment:

Shared

Parameters:

  • target: The specific player to check (server only).

Return value:

    (boolean) True if a net message can be sent, false otherwise.
net.getDTVar( type, index, chip )
Retrieves a DataTable value from the chip. Use net.setDTVar() on the server to set them.

Environment:

Shared

Parameters:

  • type: Type of the DTVar. One of "Angle", "Bool", "Entity", "Float", "Int", "String", or "Vector".
  • index: An integer between 1 and 32 (1 and 4 for strings).
  • chip: Optional chip entity that you own to get the value from.

Return value:

    The value stored against the given DTVar.
net.getMaxDTVarIndex( type, chip )
Returns the maximum index available for the given DTVar type, or 0 if the given type is invalid. Some entity types can have less slots available.

Environment:

Shared

Parameters:

  • type: The type of DTVar to get the maximum index of.
  • chip: (Optional) Chip entity that you own to get the maximum index from. Default is the instance's chip.
net.listenForAllDTVarChanges()
Enables change detection for all DataTable values on the chip. Changes will be broadcast via. the "dtvarchanged" hook, passing the type, index, old value and new value. This can increase CPU usage, so should only be used if all DataTable values are used and need change detection. Use net.listenForDTVarChanges() to specify more precise change detection.

Environment:

Client
net.listenForDTVarChanges( type, index )
Enables change detection for the given DataTable value. Changes will be broadcast via. the "dtvarchanged" hook, passing the type, index, old value and new value. If an index is not provided, all indices for the given type will have change detection enabled.

Environment:

Client

Parameters:

  • type: Type of the DTVar. One of "Angle", "Bool", "Entity", "Float", "Int", "String", or "Vector".
  • index: (Optional) An integer between 1 and 32 (1 and 4 for strings).
net.quotaMax()
Returns the max amount of bytes/s allowed.

Environment:

Shared

Return value:

    (number) The maximum amount of bytes/s allowed.
net.quotaUsed( target )
Returns the amount of bytes in the net quota currently used. Note that this does not include the message currently being written, as the quota usage depends on the players the message will be sent to. If called on the server with no target specified, this returns the average quota used across all players.

Environment:

Shared

Parameters:

  • target: The player to get the current quota usage against (server only).

Return value:

    (number) The amount of bytes currently used.
net.readAngle( lowPrecision )
Reads an angle from the net message.

Environment:

Shared

Parameters:

  • lowPrecision: Should be the same as the value used for net.writeAngle().

Return value:

    (Angle) An angle from the net message.
net.readBit()
Reads a bit from the net message.

Environment:

Shared

Return value:

    (number) A bit value from the net message (either 1 or 0).
net.readBool()
Reads a boolean from the net message.

Environment:

Shared

Return value:

    (boolean) A boolean value from the net message.
net.readData( length )
Reads binary data from the net message.

Environment:

Shared

Parameters:

  • length:

Return value:

    (string) A binary string from the net message with the given length.
net.readDouble()
Reads a double precision float from the net message.

Environment:

Shared

Return value:

    (number) A double precision float value from the net message.
net.readEntity()
Reads an entity from the net message.

Environment:

Shared

Return value:

    (Entity) An entity from the net message.
net.readFloat( double )
Reads a float from the net message.

Environment:

Shared

Parameters:

  • double: Boolean flag to read a double precision float.

Return value:

    (number) A float value from the net message.
net.readInt( bits, unsigned )
Reads an integer from the net message.

Environment:

Shared

Parameters:

  • bits: Number of bits the integer was sent with. Must be between 1 and 32.
  • unsigned: Boolean flag to read an unsigned integer.

Return value:

    (number) An integer value from the net message, consuming the specified number of bits.
net.readMatrix()
Reads a VMatrix from the net message.

Environment:

Shared

Return value:

    (VMatrix) A matrix from the net message.
net.readNormal()
Reads a normalised vector from the net message.

Environment:

Shared

Return value:

    (Vector) A normalised vector from the net message.
net.readString()
Reads a string from the net message.

Environment:

Shared

Return value:

    (string) A string from the net message.
net.readTable()
Reads a table from the net message.

Environment:

Shared

Return value:

    (table) A table from the net message.
net.readType( type )
Reads a value from the net message of the given type enum. Usually the type would be sent as part of net.writeType()

Environment:

Shared

Parameters:

  • (number?) type: (Optional) The type ID to read. If omitted, the type ID is read from the net message.

Return value:

    A value of the given type from the net message.
net.readUInt( bits )
Reads an unsigned integer from the net message.

Environment:

Shared

Parameters:

  • bits: Number of bits the integer was sent with. Must be between 1 and 32.

Return value:

    (number) An unsigned integer value from the net message, consuming the specified number of bits.
net.readVector( lowPrecision )
Reads a vector from the net message.

Environment:

Shared

Parameters:

  • lowPrecision: Should be the same as the value used for net.writeVector().

Return value:

    (Vector) A vector from the net message.
net.send( ply )
Sends the currently stored net message to the given player. Passing nil is the same as net.broadcast().

Environment:

Server

Parameters:

  • ply: The player to send the message to. Passing nil sends to all.
net.sendOmit( plys )
Sends the currently stored network message to every player except those you pass in.

Environment:

Server

Parameters:

  • plys: A player or table of players to not send to.
net.sendPAS( pos )
Sends the currently stored network message to every player who has the given position in their PAS (Potentially Audible Set)

Environment:

Server

Parameters:

  • pos: The position to check hearing for.
net.sendPVS( pos )
Sends the currently stored network message to every player who has the given position in their PVS (Potentially Visible Set).

Environment:

Server

Parameters:

  • pos: The position to check visibility for.
net.sendToServer()
Sends the currently stored network message to the server.

Environment:

Client
net.setDTVar( type, index, value, chip )
Sets a DataTable value on the given chip entity. DataTable values are networked automatically to all clients whenever their value changes. Each chip has a limited number of values available per type. The index controls which value to set under a given type. If you omit the last argument, the value will be set on the chip executing this function. For entities, use ents.NULL_ENTITY to reset the value.

Environment:

Server

Parameters:

  • type: Type of the DTVar. One of "Angle", "Bool", "Entity", "Float", "Int", "String", or "Vector".
  • index: An integer between 1 and 32 (1 and 4 for strings).
  • value: The value to set.
  • chip: (Optional) Chip entity that you own to set the value on.
net.start( ent, unreliable )
Starts a net message. All net.write functions called after this and before a send or broadcast will be sent in one net message.

Environment:

Shared

Parameters:

  • (Entity?) ent: (Optional) Starfall entity to send the message to instead of the current instance's entity. Can only send to entities you own.
  • (boolean?) unreliable: (Optional) If set to true, the message will not ensure the other end receives it and thus not wait before sending a new message.

Return values:

  1. (boolean) True if the message started successfully, false otherwise.
  2. (string) The reason the message did not start successfully, if the first returned value was false.
net.writeAngle( ang, lowPrecision )
Writes an angle to the net message.

Environment:

Shared

Parameters:

  • ang: Angle to write.
  • lowPrecision: If true, the written data will be smaller at the cost of precision.
net.writeBit( bit )
Writes a bit to the current net message.

Environment:

Shared

Parameters:

  • bit: Boolean true or false as the bit to write.
net.writeBool( bool )
Writes a boolean to the current message.

Environment:

Shared

Parameters:

  • bool: Boolean value to write.
net.writeData( data, length )
Writes binary data to the net message.

Environment:

Shared

Parameters:

  • data: The data to send (a string).
  • length: The length, in bytes, of the data.
net.writeDouble( double )
Writes a double precision float to the current net message.

Environment:

Shared

Parameters:

  • double: Double precision float to write.
net.writeEntity( ent )
Writes an entity to the net message.

Environment:

Shared

Parameters:

  • ent: Entity to write.
net.writeFloat( float, double )
Writes a float to the current net message.

Environment:

Shared

Parameters:

  • float: Floating point number to write.
  • double: Boolean flag to use double precision instead of single.
net.writeInt( int, bits, unsigned )
Writes an integer to the current net message.

Environment:

Shared

Parameters:

  • int: Integer to write.
  • bits: Number of bits the integer uses. Must be between 1 and 32.
  • unsigned: Boolean to send the integer as an unsigned integer. Unsigned integers have a larger range but can't be negative.
net.writeMatrix( matrix )
Writes a matrix to the net message.

Environment:

Shared

Parameters:

  • matrix: The VMatrix to write.
net.writeNormal( normal )
Writes a normalised vector to the net message.

Environment:

Shared

Parameters:

  • normal: Normalised vector to write.
net.writeString( string )
Writes a string to the net message. Some special characters may not be sent correctly. Use net.writeData for that.

Environment:

Shared

Parameters:

  • string: String to write.
net.writeTable( table )
Writes an entire table with any arbitrary structure to the net message. NOTE: This writes 8 bit key type, key, 8 bit value type, value for every key-value pair. Usually it's much more efficient to write data in a pre-defined structure, use this as a last resort only.

Environment:

Shared

Parameters:

  • (table) table: Table to write.
net.writeType( value )
Writes the value's type, then its value to the net message. Note that this means an extra byte for the type is sent, so use only if necessary. Most net messages should have a pre-defined structure and not need to use this function. This is especially expensive if writing an arbitrary table, as it will use net.writeTable() internally.

Environment:

Shared

Parameters:

  • value: The value to write.
net.writeUInt( int, bits )
Writes an unsigned integer to the current net message.

Environment:

Shared

Parameters:

  • int: Integer to write.
  • bits: Number of bits the integer uses. Must be between 1 and 32.
net.writeVector( vec, lowPrecision )
Writes a vector to the net message.

Environment:

Shared

Parameters:

  • vec: Vector to write.
  • lowPrecision: If true, the written data will be smaller at the cost of precision.

Valid XHTML 1.0!