Entity/NetworkVar

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Correction on count of number of vars from 32 to 64)
 
Line 1: Line 1:
 
{{Func
 
{{Func
 
|Description=Creates a network variable on the entity and adds Set/Get functions for it. This function should only be called in {{HookFunction|ENTITY|SetupDataTables}}.
 
|Description=Creates a network variable on the entity and adds Set/Get functions for it. This function should only be called in {{HookFunction|ENTITY|SetupDataTables}}.
 +
 +
{{Warning|Make sure to not call the SetDT* and your custom set methods on the client realm unless you know exactly what you are doing.}}
 
|Realm=Shared
 
|Realm=Shared
 
|IsClass=Yes
 
|IsClass=Yes

Latest revision as of 22:49, 2 January 2020

 Entity:NetworkVar( )

Contents

Description

Creates a network variable on the entity and adds Set/Get functions for it. This function should only be called in ENTITY:SetupDataTables.

WARNING

Make sure to not call the SetDT* and your custom set methods on the client realm unless you know exactly what you are doing.

Arguments

string type

Supported choices:

  • "String"
  • "Bool"
  • "Float"
  • "Int" (32-bit signed integer)
  • "Vector"
  • "Angle"
  • "Entity"

Arguments

number slot

Each network var has to have a unique slot. The slot is per type - so you can have an int in slot 0, a bool in slot 0 and a float in slot 0 etc. but you can't have two ints in slot 0 instead you would do a int in slot 0 and another int in slot 1.

The max slots right now are 64 - so you should pick a number between 0 and 63. An exception to this is strings which has a max slots of 4.

Arguments

string name

The name will affect how you access it. If you call it "Foo" you would add two new functions on your entity - SetFoo and GetFoo. So be careful that what you call it won't collide with any existing functions (don't call it "Pos" for example).

Arguments

table extended=nil

A table of extended information.

KeyName

If the table contains a "KeyName" key the value can be set using Entity:SetKeyValue. This is useful if you're making an entity that you want to be loaded in a map. The sky entity uses this.

Edit

The edit key lets you mark this variable as editable. See Editable Entities for more information.

Examples

Example

Setting up data tables

function ENT:SetupDataTables()

	self:NetworkVar( "Float", 0, "Amount" )
	self:NetworkVar( "Vector", 0, "StartPos" )
	self:NetworkVar( "Vector", 1, "EndPos" )

end

-- Code...

-- Setting values on the entity
self:SetStartPos( Vector( 1, 0, 0 ) )
self:SetAmount( 100 )

-- Code...

-- Getting values
local startpos = self:GetStartPos()


Personal tools
Navigation