Garry's Mod Wiki

Revision Difference

net.WriteInt#510725

<function name="WriteInt" parent="net" type="libraryfunc">⤶ <description>⤶ Appends an integer - a whole number - to the current net message. Can be read back with <page>net.ReadInt</page> on the receiving end.⤶ ⤶ Use <page>net.WriteUInt</page> to send an unsigned number (that you know will never be negative). Use <page>net.WriteFloat</page> for a non-whole number (e.g. 2.25).⤶ </description>⤶ <realm>Shared</realm>⤶ <args>⤶ <arg name="integer" type="number">The integer to be sent.</arg>⤶ <arg name="bitCount" type="number">The amount of bits the number consists of. This must be 32 or less.&#xA;&#xA;If you are unsure what to set, just set it to 32.&#xA;&#xA;&#xA;&#xA;&#x2B;Consult the table below to determine the bit count you need.| Bit Count | Minimum value | Maximum value |&#xA;-------------------------------------------------------------|------------|----------------|---------------|&#xA;| 3 | -4 | 3 |&#xA;| 4 | -8 | 7 |&#xA;| 5 | -16 | 15 |&#xA;| 6 | -32 | 31 |&#xA;| 7 | -64 | 63 |&#xA;| 8 | -128 | 127 |&#xA;| 9 | -256 | 255 |&#xA;| 10 | -512 | 511 |&#xA;| 11 | -1024 | 1023 |&#xA;| 12 | -2048 | 2047 |&#xA;| 13 | -4096 | 4095 |&#xA;| 14 | -8192 | 8191 |&#xA;| 15 | -16384 | 16383 |&#xA;| 16 | -32768 | 32767 |&#xA;| 17 | -65536 | 65535 |&#xA;| 18 | -131072 | 131071 |&#xA;| 19 | -262144 | 262143 |&#xA;| 20 | -524288 | 524287 |&#xA;| 21 | -1048576 | 1048575 |&#xA;| 22 | -2097152 | 2097151 |&#xA;| 23 | -4194304 | 4194303 |&#xA;| 24 | -8388608 | 8388607 |&#xA;| 25 | -16777216 | 16777215 |&#xA;| 26 | -33554432 | 33554431 |&#xA;| 27 | -67108864 | 67108863 |&#xA;| 28 | -134217728 | 134217727 |&#xA;| 29 | -268435456 | 268435455 |&#xA;| 30 | -536870912 | 536870911 |&#xA;| 31 | -1073741824 | 1073741823 |&#xA;| 32 | -2147483648 | 2147483647 |</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Sends the server the client's age.</description>⤶ <code>⤶ --Client⤶ function SendAge()⤶ net.Start("SendAge")⤶ net.WriteInt(3, 3) -- Only 2 bits are needed to store the number '3', but we add one because of the rule.⤶ net.SendToServer()⤶ end⤶ ⤶ --Server⤶ util.AddNetworkString("SendAge")⤶ ⤶ local function GetAge(len, ply)⤶ local age = net.ReadInt(3) -- use the same number of bits that were written⤶ print("Player " .. ply:Nick() .. " is " .. age .. " years old."⤶ end⤶ net.Receive("SendAge", GetAge)⤶ </code>⤶ <output>Player &amp;lt;name&amp;gt; is 3 years old.</output>⤶ ⤶ </example>