Garry's Mod Wiki

net

The net library is one of a number of ways to send data between the client and server.

The major advantages of the net library are the large size limit (64kb/message) and the ability to send data backwards - from the client to the server.

Refer to Net Library Usage for a short introduction.

Fields

This is used internally - although you're able to use it you probably shouldn't. A list of types that can be sent over the network via net. ReadType.
This is used internally - although you're able to use it you probably shouldn't. This is NOT a function, it's a table used internally by the net library to store net receivers added with net. Receive. The key is the lowercase net message name and the value is the message's callback function. Modifying net. Receivers won't affect the net string pool used in util. AddNetworkString.
This is used internally - although you're able to use it you probably shouldn't. A list of types that can be sent over the network via net. WriteType.

Methods

net.Abort()
Cancels a net message started by net. Start, so you can immediately start a new one without any errors.
net.Broadcast()
Sends the currently built net message (see net. Start) to all connected players. More information can be found in Net Library Usage.
Returns the amount of data left to read in the current message. Does nothing when sending data. This will include 6 extra bits (or 1 byte rounded-up) used by the engine internally.
Returns the size of the current message. This will include 3 extra bytes (24 bits) used by the engine internally to send the data over the network.
net.Incoming( number length, Player client )
This is used internally - although you're able to use it you probably shouldn't. You may be looking for net. Receive. Function called by the engine to tell the Lua state a message arrived.
Reads an angle from the received net message. You must read information in same order as you write it.
Reads a bit from the received net message. You must read information in same order as you write it.
Reads a boolean from the received net message. You must read information in same order as you write it.
table net.ReadColor( boolean hasAlpha = true )
Reads a Color from the current net message. You must read information in same order as you write it.
string net.ReadData( number length )
Reads pure binary data from the message. You must read information in same order as you write it.
Reads a double-precision number from the received net message. You must read information in same order as you write it.
Reads an entity from the received net message. You should always check if the specified entity exists as it may have been removed and therefore NULL if it is outside of the players PVS (Potential Visibility Set) or was already removed. You must read information in same order as you write it.
Reads a floating point number from the received net message. You must read information in same order as you write it.
This is used internally - although you're able to use it you probably shouldn't. Reads a word, basically unsigned short. This is used internally to read the "header" of the message which is an unsigned short which can be converted to the corresponding message name via util. NetworkIDToString.
number net.ReadInt( number bitCount )
Reads an integer from the received net message. You must read information in same order as you write it.
Reads a VMatrix from the received net message. You must read information in same order as you write it.
Reads a normal vector from the net message. You must read information in same order as you write it.
Reads a player entity that was written with net. WritePlayer from the received net message. You should always check if the specified entity exists as it may have been removed and therefore NULL if it is outside of the local players PVS or was already removed. You must read information in same order as you write it.
Reads a null-terminated string from the net stream. The size of the string is 8 bits plus 8 bits for every ASCII character in the string. You must read information in same order as you write it.
table net.ReadTable( boolean sequential = false )
Reads a table from the received net message. See net. WriteTable for extra info. Sometimes when sending a table through the net library, the order of the keys may be switched. So be cautious when comparing (See example 1). You may get net. ReadType: Couldn't read type X during the execution of the function, the problem is that you are sending objects that cannot be serialized/sent over the network. You must read information in same order as you write it.
any net.ReadType( number typeID = net.ReadUInt(8) )
This is used internally - although you're able to use it you probably shouldn't. Used internally by net. ReadTable. Reads a value from the net message with the specified type, written by net. WriteType. You must read information in same order as you write it.
number net.ReadUInt( number bitCount )
Reads an unsigned integer with the specified number of bits from the received net message. You must read information in same order as you write it.
Reads a unsigned integer with 64 bits from the received net message. You must read information in same order as you write it.
Reads a vector from the received net message. Vectors sent by this function are compressed, which may result in precision loss. See net. WriteVector for more information. You must read information in same order as you write it.
net.Receive( string messageName, function callback )
Adds a net message handler. Only one receiver can be used to receive the net message. You can use the net. Read* functions within the message handler callback. The message-name is converted to lower-case so the message-names "BigBlue" and "bigblue" would be equal. You should put this function outside of any other function or hook for it to work properly unless you know what you are doing! You must read information in the same order as you write it. Each net message has a length limit of 64KB!
net.Send( Player ply )
Sends the current net message (see net. Start) to the specified player, or to all players listed in the table.
net.SendOmit( Player ply )
Sends the current message (see net. Start) to all except the specified, or to all except all players in the table.
net.SendPAS( Vector position )
Sends current net message (see net. Start) to all players that are in the same Potentially Audible Set (PAS) as the position, or simply said, it adds all players that can potentially hear sounds from this position.
net.SendPVS( Vector position )
Sends current net message (see net. Start) to all players in the PVS (Potential Visibility Set) of the position, or, more simply said, sends the message to players that can potentially see this position.
Sends the current net message (see net. Start) to the server. Each net message has a length limit of 65,533 bytes (approximately 64 KiB) and your net message will error and fail to send if it is larger than this. The message name must be pooled with util. AddNetworkString beforehand!
boolean net.Start( string messageName, boolean unreliable = false )
Begins a new net message. If another net message is already started and hasn't been sent yet, it will be discarded. After calling this function, you will want to call net. Write functions to write your data, if any, and then finish with a call to one of the following functions: net. Send net. SendOmit net. SendPAS net. SendPVS net. Broadcast net. SendToServer Each net message has a length limit of 65,533 bytes (approximately 64 KiB) and your net message will error and fail to send if it is larger than this. The net library has an internal buffer that sent messages are added to that is capable of holding roughly 256 kb at a time. Trying to send more will lead to the client being kicked because of a buffer overflow. More information on net library limits can be found here. The message name must be pooled with util. AddNetworkString beforehand! Net messages will not reliably reach the client until the client's GM:InitPostEntity hook is called.
net.WriteAngle( Angle angle )
Writes an angle to the current net message.
net.WriteBit( boolean boolean )
Appends a boolean (as 1 or 0) to the current net message. Please note that the bit is written here from a boolean (true/false) but net. ReadBit returns a number.
net.WriteBool( boolean boolean )
Appends a boolean to the current net message. Alias of net. WriteBit.
net.WriteColor( table Color, boolean writeAlpha = true )
Appends a Color to the current net message.
net.WriteData( string binaryData, number length = #binaryData )
Writes a chunk of binary data to the message.
net.WriteDouble( number double )
Appends a double-precision number to the current net message.
net.WriteEntity( Entity entity )
Appends an entity to the current net message using its Entity:EntIndex. See net. ReadEntity for the function to read the entity.
net.WriteFloat( number float )
Appends a float (number with decimals) to the current net message.
net.WriteInt( number integer, number bitCount )
Appends a signed integer - a whole number, positive/negative - to the current net message. Can be read back with net. ReadInt on the receiving end. Use net. WriteUInt to send an unsigned number (that you know will never be negative). Use net. WriteFloat for a non-whole number (e. g. 2. 25).
net.WriteMatrix( VMatrix matrix )
Writes a VMatrix to the current net message.
net.WriteNormal( Vector normal )
Writes a normalized/direction vector ( Vector with length of 1 ) to the net message. This function uses less bandwidth compared to net. WriteVector and will not send vectors with length of > 1 properly.
net.WritePlayer( Player ply )
Appends a player entity to the current net message using its Entity:EntIndex. This saves a small amount of network bandwidth over net. WriteEntity. See net. ReadPlayer for the function to read the entity.
net.WriteString( string string )
Appends a string to the current net message. The size of the string is 8 bits plus 8 bits for every ASCII character in the string. The maximum allowed length of a single written string is 65532 characters.
net.WriteTable( table table, boolean sequential = false )
Appends a table to the current net message. Adds 16 extra bits per key/value pair, so you're better off writing each individual key/value as the exact type if possible. All net messages have a 64kb buffer. This function will not check or error when that buffer is overflown. You might want to consider using util. TableToJSON and util. Compress and send the resulting string in 60kb chunks, doing the opposite on the receiving end.
net.WriteType( any Data )
This is used internally - although you're able to use it you probably shouldn't. Used internally by net. WriteTable. Appends any type of value to the current net message. An additional 8-bit unsigned integer indicating the type will automatically be written to the packet before the value, in order to facilitate reading with net. ReadType. If you know the data type you are writing, use a function meant for that specific data type to reduce amount of data sent.
net.WriteUInt( number unsignedInteger, number bitCount )
Appends an unsigned integer with the specified number of bits to the current net message. Use net. WriteInt if you want to send negative and positive numbers. Use net. WriteFloat for a non-whole number (e. g. 2. 25). Unsigned numbers do not support negative numbers.
net.WriteUInt64( string uint64 )
Appends an unsigned integer with 64 bits to the current net message. The limit for an uint64 is 18'446'744'073'709'551'615. Everything above the limit will be set to the limit. Unsigned numbers do not support negative numbers.
net.WriteVector( Vector vector )
Appends a vector to the current net message. Vectors sent by this function are compressed, which may result in precision loss. XYZ components greater than 16384 or less than -16384 are irrecoverably altered (most significant bits are trimmed) and precision after the decimal point is low.