Garry's Mod Wiki

Revision Difference

net.WriteTable#561315

<function name="WriteTable" parent="net" type="libraryfunc"> <description> 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. <warning>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 <page>util.TableToJSON</page> and <page>util.Compress</page> and send the resulting string in **60kb** chunks, doing the opposite on the receiving end.</warning> </description> <realm>Shared</realm> <file line="139-L166">lua/includes/extensions/net.lua</file> <args> <arg name="table" type="table">The table to be sent. <warning>If the table contains a `nil` key the table may not be read correctly. Not all objects can be sent over the network. Things like functions, <page>IMaterial</page>s, etc will cause errors when reading the table from a net message.</warning></arg> <arg name="sequential" type="boolean" default="false" added="2023.09.06">Set to `true` if the input table is sequential. This saves on bandwidth, adding **8 extra bits** per key/value pair instead of 16 bits. <note>To read the table you need to give <page>net.ReadTable</page> the same value!</note> </arg> </args> </function> ⤶ <example>⤶ <description>This is an example of how the keys order may be switched:</description>⤶ <code>⤶ if ( CLIENT ) then⤶ local tbl = {⤶ Type = "Dining",⤶ Legs = "4",⤶ Material = "Wood"⤶ }⤶ ⤶ net.Start( "TableSend" )⤶ net.WriteTable( tbl )⤶ net.SendToServer()⤶ ⤶ ⤶ PrintTable( tbl ) -- Prints the order client-side.⤶ end⤶ ⤶ if ( SERVER ) then⤶ net.Receive( "TableSend", function( len, ply )⤶ PrintTable( net.ReadTable() ) -- Prints the order server-side.⤶ end )⤶ end⤶ </code>⤶ <output>⤶ Client:⤶ ```⤶ Type = "Dining"⤶ Legs = 4⤶ Material = "Wood"⤶ ```⤶ ⤶ Server:⤶ ```⤶ Legs = 4⤶ Material = "Wood"⤶ Type = "Dining"⤶ ```⤶ </output>⤶ </example>