Garry's Mod Wiki


  Entity:NetworkVarNotify( string name, function callback )


Creates a callback that will execute when the given network variable changes - that is, when the Set<name>() function is run.

The callback is executed before the value is changed, and is called even if the new and old values are the same.

This function does not exist on entities in which Entity:InstallDataTable 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 (serverside and clientside LocalPlayer only).

It's therefore safest to only use this in ENTITY:SetupDataTables.

The callback will not be called clientside if the var is changed right after entity spawn.


1 string name
Name of variable to track changes of.
2 function callback
The function to call when the variable changes.
Function argument(s):
1 Entity entity - Entity whos variable changed.
2 string name - Name of changed variable.
3 any old - Old/current variable value.
4 any new - New variable value that it was set to.


Example usage.

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
Output: Prints variable name, old value and new value whenever SetEndPos function is called.