Networking Entities

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Corrected max number of DT variables to 64 (instead of 32) and max string length to 512 (instead of 1024))
 
Line 33: Line 33:
 
|Type||The type is a string, and can be "String", "Bool", "Float", "Int", "Vector", "Angle", "Entity"
 
|Type||The type is a string, and can be "String", "Bool", "Float", "Int", "Vector", "Angle", "Entity"
 
|-
 
|-
|Slot||You can store up to 32 of each type in your entity except for the "String" type, which can only store up to 4 slots. This should be a number between 0 and 31 ( or 3 for strings ), identifying which slot you want to store this variable in.
+
|Slot||You can store up to 64 of each type in your entity except for the "String" type, which can only store up to 4 slots. This should be a number between 0 and 63 ( or 3 for strings ), identifying which slot you want to store this variable in.
 
|-
 
|-
 
|Name||Whatever name you choose will create two functions (Set[name] and Get[name]) -  so be careful that your name doesn't conflict with other existing functions.
 
|Name||Whatever name you choose will create two functions (Set[name] and Get[name]) -  so be careful that your name doesn't conflict with other existing functions.
 
|}
 
|}
  
Note that the String type is up to 1024 chars. If you have common strings that are repeated you should add them to the string table and network the integer ID instead.
+
Note that the String type is up to 512 chars. If you have common strings that are repeated you should add them to the string table and network the integer ID instead.
  
 
[[Category:Modding Tutorials]]
 
[[Category:Modding Tutorials]]
 
[[Category:Lua Reference]]
 
[[Category:Lua Reference]]
 
[[Category:Lua Tutorials]]
 
[[Category:Lua Tutorials]]

Latest revision as of 01:00, 6 July 2019

Data Table Variables

These variables are the preferred method as they use the built in network variable system, so they get networked, saved and predicted properly. You should use this method by default.

To add a new variable simply add a SetupDataTables() function to it. For example.

function ENT:SetupDataTables()

	self:NetworkVar( "Float", 0, "Amount" )
	self:NetworkVar( "Vector", 0, "BloodPos" )
	self:NetworkVar( "Vector", 1, "UrinePos" )

end

You have now defined three variables on your entity. You can access these variables like this.

local bloodpos = self:GetBloodPos()

And set them like

self:SetBloodPos( Vector( 1, 0, 0 ) )
self:SetAmount( 100 )

You can of course set and get from outside of the entity

MyEntity:GetAmount()

The syntax of NetworkVar is thus:

Type The type is a string, and can be "String", "Bool", "Float", "Int", "Vector", "Angle", "Entity"
Slot You can store up to 64 of each type in your entity except for the "String" type, which can only store up to 4 slots. This should be a number between 0 and 63 ( or 3 for strings ), identifying which slot you want to store this variable in.
Name Whatever name you choose will create two functions (Set[name] and Get[name]) - so be careful that your name doesn't conflict with other existing functions.

Note that the String type is up to 512 chars. If you have common strings that are repeated you should add them to the string table and network the integer ID instead.

Personal tools
Navigation