Entity/NetworkVarNotify

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(note template; warning about not being called clientside; remove repeated information)
(Fixed incorrect warning. Seriously though, why isn't this shared yet??)
Line 4: Line 4:
 
{{Note|The callback is executed ''before'' the value is changed, and is called even if the new and old values are the same.}}
 
{{Note|The callback is executed ''before'' the value is changed, and is called even if the new and old values are the same.}}
  
{{Warning|A clientside NetworkVarNotify will not be called when the network var is being changed serverside! This makes the function mostly useless, since setting a network var clientside is generally wrong, except special cases. This may be a bug.}}
+
{{Warning|A clientside version of this function is not available which makes the function mostly useless. See [https://github.com/Facepunch/garrysmod-requests/issues/324 here].}}
 
|Realm=Server
 
|Realm=Server
 
|IsClass=Yes
 
|IsClass=Yes

Revision as of 10:13, 19 May 2016

 Entity:NetworkVarNotify( )

Contents

Description

Creates a callback that will execute when the given network variable changes - that is, when the Set<name> 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.

WARNING

A clientside version of this function is not available which makes the function mostly useless. See here.

Arguments

string name

Name of variable to track changes of

Arguments

function callback

The function to call when the variable changes. It is passed 4 arugments:

  • Entity entity - Entity whos variable changed (This will be variable called "self" in ENT:CallBack format.)
  • string name - Name of changed variable
  • any old - Old/current variable value
  • any new - New variable value that it was set to

Examples

Example

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
Personal tools
Navigation