Garry's Mod Wiki

Revision Difference

Networking_Usage#548237

<cat>Dev</cat>⤶ <title>Networking Options, Limits and Errors</title>⤶ ⤶ This page lists which Networking Options there are and how they work, all Networking Limits and all found Networking Errors and how to solve them⤶ # Networking Options⤶ ⤶ ## umsg (DEPRECATED)⤶ The <page>umsg</page> library is a deprecated serverside networking library that was previously the most common way of sending information from the server to the client. In order for clients to receive an umsg, it has to use the <page>usermessage</page> library, which is shared for the client and the server.⤶ ⤶ This library was used in some functions to send a message like text to the client and then to display it, and it is still used in some old functions like <page>Global.PrintMessage</page>, <page>Player:PrintMessage</page> and <page>Player:ChatPrint</page>⤶ ⤶ Hello World example with the <page>umsg</page> and <page>usermessage</page> library.⤶ ```lua⤶ if SERVER then⤶ local filter = RecipientFilter()⤶ filter:AddAllPlayers()⤶ umsg.Start( "Example" )⤶ umsg.String( "Hello World" )⤶ umsg.End()⤶ else⤶ usermessage.Hook( "Example", function( msg )⤶ print( msg:ReadString() )⤶ end)⤶ end⤶ ```⤶ ⤶ ## net⤶ The <page>net</page> library is one of the ways to send data between client and server. One of the major advantages of the net library is the ability to send data backwards - from the client to the server. ⤶ ⤶ This library is commonly used in the network of values to a specific client only, or to network bigger values like Strings(<page>net.WriteString</page>) or Ints(<page>net.WriteInt</page>). ⤶ You can read more about this library here: <page>Net_Library_Usage</page> and <page>Net_Library_Example</page>⤶ ⤶ Hello World Example with the <page>net</page> library⤶ ```lua⤶ if SERVER then⤶ util.AddNetworkString( "Example" )⤶ net.Start( "Example" )⤶ net.WriteString( "Hello World" )⤶ net.Broadcast()⤶ else⤶ net.Receive( "Example", function()⤶ print( net.ReadString() )⤶ end)⤶ end⤶ ```⤶ ⤶ ## NW⤶ The NW system allows for a value to be networked on an entity to all clients with the SetNW functions like <page>Entity:SetNWString</page> and the value can be returned by using the GetNW functions like <page>Entity:GetNWString</page>. ⤶ It also allows to set global values with the SetGlobal* functions like <page>Global.SetGlobalString</page> and the value can be retured by using the GetGlobal* functions like <page>Global.GetGlobalString</page>⤶ ⤶ <note>the value will be the same on all clients if they haven't been modified by the client. ⤶ All SetNW* functions will network the value every 10 seconds after it has been set.</note>⤶ ⤶ Hello World Example with the NW system⤶ ```lua⤶ if SERVER then⤶ Entity( 1 ):SetNWString( "Example", "Hello World" )⤶ else⤶ Entity( 1 ):SetNWVarProxy( "Example", function(_, _, _, value )⤶ print( value )⤶ end)⤶ end⤶ ```⤶ ⤶ ## NW2⤶ The NW2 system is the successor to the NW system, but it hasn't been officially finished. It allows for values to be networked on an entity to all clients in the entity's [PVS(Potential Visibility Set)](https://developer.valvesoftware.com/wiki/PVS "PVS - Valve Developer Community") with the given value with the SetNW2* functions like <page>Entity:SetNW2String</page> and the value can be returned by using the GetNW2* functions like <page>Entity:GetNW2String</page>.⤶ ⤶ <note>The value will only be updated clientside if the entity is or enters the client's [PVS(Potential Visibility Set)](https://developer.valvesoftware.com/wiki/PVS "PVS - Valve Developer Community"). ⤶ The value will only be networked if it isn't the same as the current value and unlike SetNW* the value will only be networked once and not every 10 seconds.</note>⤶ ⤶ It also allows to set global values with the SetGlobal2* functions like <page>Global.SetGlobal2String</page> and the value can be returned by using the GetGlobal2* functions like <page>Global.GetGlobal2String</page>. All SetGlobal2* functions will update the value clientside, and it will ignore the PVS.⤶ ⤶ Hello World Example with the NW2 system⤶ ```lua⤶ if SERVER then⤶ Entity( 1 ):SetNW2String( "Example", "Hello World" )⤶ else⤶ Entity( 1 ):SetNW2VarProxy( "Example", function( _, _, _, value )⤶ print( value )⤶ end)⤶ end⤶ ```⤶ ⤶ or⤶ ⤶ ```lua⤶ if SERVER then⤶ Entity( 1 ):SetNW2String( "Example", "Hello World" )⤶ else⤶ hook.Add( "EntityNetworkedVarChanged", "Example", function( _, _, _, value )⤶ print( value )⤶ end)⤶ end⤶ ```⤶ ⤶ # Networking Limits⤶ ⤶ ## umsg⤶ The <page>umsg</page> library has a 256 bytes limit per message.⤶ ⤶ ## net⤶ The <page>net</page> library has a 64kb (65536 bytes) limit per message.⤶ The library has an internal buffer that has roughly a 256kb limit before it overflows, and if it overflows, it will cause clients that receive the net message to disconnect. So do **NOT** fill the buffer.⤶ ⤶ <example>⤶ <description>Creating a net message that uses the maximum size</description>⤶ <code>⤶ local string = ""⤶ for k=1, 65532 do⤶ string = string .. "a"⤶ end⤶ net.Start( "test" )⤶ net.WriteString( string )⤶ net.Broadcast()⤶ </code>⤶ <output>⤶ 65536 524288⤶ </output>⤶ </example>⤶ ⤶ ## NW⤶ The NW system uses a stringtable that has 4095 slots and is used by all SetGlobal*, SetNW* functions and the <page>util.AddNetworkString</page> function. By default, 95 slots are used.⤶ ⤶ <example>⤶ <description>Returning the amount of used slots and the last key</description>⤶ <code>⤶ for k = 1, 4096 do⤶ if !util.NetworkIDToString( k ) then⤶ print( k - 1, util.NetworkIDToString( k - 1 ) )⤶ break⤶ end⤶ end⤶ </code>⤶ <output>```lua 95 ServerName```</output>⤶ </example>⤶ ⤶ ⤶ ## NW2⤶ The NW2 system uses a separate stringtable that has 4095 slots, so it is not influenced by the NW stringtable. The NW2 stringtable is used by all SetNW2* and SetGlobal2* functions. By default, the stringtable is completely empty. ⤶ Currently, there is no way of checking the usage of the NW2 string table.⤶ ⤶ ⤶ ⤶ # Networking Errors⤶ ⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58d7428c9c6.png)⤶ ```lua⤶ Host_Error: SV_PackEntity: SendTable_Encode returned false (ent 1).⤶ ```⤶ ⤶ This error is created when you try to network too many NW2Vars at the same time. When this error occurs, it will close the server and on your game will crash when you try to start a new game without restarting gmod first. ⤶ If you should ever get this error, you should reduce the amount of NW2Vars that you're trying to network at the same time⤶ ⤶ ---⤶ ⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58d7428c9c6.png)⤶ ```lua⤶ Warning: Table networkstring is full, can't add [key]⤶ ```⤶ This error is created when you exceed the NWVar limit, which is currently at 4095 slots. Reduce the amount of NWVars or consider using the <page>net</page> liabry or the SetNW* functions to solve this error.⤶ ⤶ ⤶ ---⤶ ⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58d7428c9c6.png)⤶ ```lua⤶ Too many NWVar names, could not add name [key]⤶ ```⤶ ⤶ This error is created when you exceed the NW2Var limit, which is currently at 4095 slots. Reduce the amount of NW2Vars or consider using the <page>net</page> library to solve this error.⤶ ⤶ ---⤶ ⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58d7428c9c6.png)⤶ ```lua⤶ Error sending usermessage - too large ([key])⤶ ```⤶ This error is created serverside when you exceed the umsg limit of 256 bytes. Reduce the size of your usermessage to solve this error.⤶ ⤶ --- ⤶ ⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58bc25e14dd.png)⤶ ```lua⤶ Warning: Unhandled usermessage '[key]'⤶ ```⤶ This error is created clientside when you forgot to use <page>usermessage.Hook</page> with the right key.⤶ ⤶ <note>The <page>umsg</page> is deprecated, and you should use the <page>net</page> library instead.</note>⤶ <example>⤶ <description>How to properly use the umsg library</description>⤶ <code>⤶ if SERVER then⤶ local filter = RecipientFilter()⤶ filter:AddAllPlayers()⤶ umsg.Start( "Example" )⤶ umsg.String( "Hello World" )⤶ umsg.End()⤶ else⤶ usermessage.Hook( "Example", function( msg )⤶ print( msg:ReadString() )⤶ end)⤶ end⤶ </code>⤶ </example>⤶ ⤶ ---⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58d7428c9c6.png)⤶ ⤶ ```lua⤶ [addon] Trying to send an overflowed net message!⤶ ⤶ ```⤶ This error is created serverside when you exceed the net limit of 64kb (65536 bytes). Reduce the size of your net message to solve this error.⤶ ⤶ ---⤶ ![realm](https://files.facepunch.com/wiki/files/19952/8d7b58bc25e14dd.png)⤶ ⤶ <image src="https://i.imgur.com/D0UEQrB.png"/>⤶ ⤶ This error is created clientside when you overflowed the net reliable buffer, which has roughly a 256kb limit, and it will cause a client to disconnect. You can send unreliable net messages that won't kick a Client, but instead all net messages that don't fit in the buffer won't be received at all.⤶ ⤶ <example>⤶ <description>Overflowing the reliable buffer while using unreliable net messages won't kick the client</description>⤶ <code>⤶ local string = ""⤶ for k = 1, 65532 do⤶ string = string .. "a"⤶ end⤶ util.AddNetworkString( "Example" )⤶ for k = 1, 4 do⤶ net.Start( "Example", true )⤶ net.WriteString( string )⤶ net.Broadcast()⤶ end⤶ </code>⤶ <output>⤶ Instead of crashing, not all net messages will be received by the client.⤶ </output>⤶ </example>