Garry's Mod Wiki

Revision Difference

Entity:NetworkVarNotify#527460

<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&lt;name&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 <page text="Player Class">Player_Classes</page> (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> <file line="307-L313">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: * <page>Entity</page> entity - Entity whos variable changed (This will be variable called "self" in ENT:CallBack format.) * <page>string</page> name - Name of changed variable * <page>any</page> old - Old/current variable value * <page>any</page> 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>