Garry's Mod Wiki

Revision Difference

net.Receive#510614

<function name="Receive" parent="net" type="libraryfunc">⤶ <description>⤶ Adds a net message handler. Only one receiver can be used to receive the net message.⤶ <note>The message-name is converted to lower-case so the message-names "`BigBlue`" and "`bigblue`" would be equal.</note>⤶ <warning>You **must** put this function **outside** of any other function or hook for it to work properly unless you know what you are doing!</warning>⤶ <warning>You **must** read information in the same order as you write it.</warning>⤶ <warning>Each net message has a length limit of 64KB!</warning>⤶ </description>⤶ <realm>Shared</realm>⤶ <file line="8">lua/includes/extensions/net.lua</file>⤶ <args>⤶ <arg name="messageName" type="string">The message name to hook to.</arg>⤶ <arg name="callback" type="function">The function to be called if the specified message was received. Arguments are:&#xA;&#xA;&lt;page&gt;number&lt;/page&gt; len - Length of the message, in bits&#xA;&lt;page&gt;Player&lt;/page&gt; ply - The player that sent the message, works only serverside</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>A simple callback</description>⤶ <code>⤶ net.Receive( "my_message", function( len, pl )⤶ if ( IsValid( pl ) and pl:IsPlayer() ) then⤶ print( "Message from " .. pl:Nick() .. " received. Its length is " .. len .. "." )⤶ else⤶ print( "Message from server received. Its length is " .. len .. "." )⤶ end⤶ end )⤶ </code>⤶ <output>⤶ When run server-side:⤶ ⤶ ⤶ ```⤶ Message from John received. Its length is 4.⤶ ```⤶ ⤶ ⤶ And client-side:⤶ ⤶ ⤶ ```⤶ Message from server received. Its length is 4.⤶ ```⤶ ⤶ </output>⤶ ⤶ </example>