Garry's Mod Wiki

Revision Difference

Entity:NetworkVarNotify#514667

<function name="NetworkVarNotify" parent="Entity" type="classfunc">⤶ <description>⤶ Creates a callback that will execute when the given network variable changes - that is, when the Set&amp;lt;name&amp;gt; function is run.⤶ ⤶ <note>The callback is executed `before` the value is changed, and is called even if the new and old values are the same.</note>⤶ ⤶ <note>This function does not exist on entities in which <page>Entity:InstallDataTable</page> has not been called. By default, this means this function only exists on SENTs (both serverside and clientside) and on players with a [Player Class](/gmod/Player_Classes) (serverside and clientside <page>Global.LocalPlayer</page> only!). It is therefore safest to only use this in <page>ENTITY:SetupDataTables</page>.</note>⤶ ⤶ <bug request="324">The callback will not be called clientside if the var is changed right after entity spawn.</bug>⤶ </description>⤶ <realm>Shared</realm>⤶ <file line="289-299">lua/includes/extensions/entity.lua</file>⤶ <args>⤶ <arg name="name" type="string">Name of variable to track changes of</arg>⤶ <arg name="callback" type="function">The function to call when the variable changes. It is passed 4 arugments:&#xA;* &lt;page&gt;Entity&lt;/page&gt; entity - Entity whos variable changed (This will be variable called &quot;self&quot; in ENT:CallBack format.)&#xA;* &lt;page&gt;string&lt;/page&gt; name - Name of changed variable&#xA;* &lt;page&gt;any&lt;/page&gt; old - Old/current variable value&#xA;* &lt;page&gt;any&lt;/page&gt; new - New variable value that it was set to</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Example usage</description>⤶ <code>⤶ function ENT:SetupDataTables()⤶ ⤶ self:NetworkVar( "Float", 0, "Amount" )⤶ self:NetworkVar( "Vector", 1, "StartPos" )⤶ self:NetworkVar( "Vector", 2, "EndPos" )⤶ ⤶ if ( SERVER ) then⤶ self:NetworkVarNotify( "EndPos", self.OnVarChanged )⤶ end⤶ ⤶ end⤶ ⤶ function ENT:OnVarChanged( name, old, new )⤶ print( name, old, new )⤶ end⤶ </code>⤶ <output>Prints variable name, old value and new value whenever SetEndPos function is called</output>⤶ ⤶ </example>