garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

DT/NetworkVar Changes

Open LeQuackers opened this issue 3 years ago • 0 comments

This is an attempt to close Facepunch/garrysmod-requests/issues/798. This allows you to omit the slot of DT/NetworkVars, which simplifies DataTable inheritance. Entity:IsDTVarSlotUsed(typename, slot) and minor optimizations are also included.

The DT/NetworkVars slot parameter can either be nil or completely omitted:

self:NetworkVar("Int", 2, "MyVar1") -- current way, slot 2 is used
self:NetworkVar("Int", nil, "MyVar2") -- slot is nil, slot 0 is backfilled
self:NetworkVar("Int", "MyVar3") -- slot is omitted, slot 1 is backfilled
self:NetworkVar("Int", "MyVar4") -- slot 3 is the smallest unused slot, so it's used

DataTable inheritance test:

local ENT = {}

ENT.Type = "anim"

function ENT:SetupDataTables()
	self:NetworkVar("Int", 1, "Base1")
	self:NetworkVar("Int", 3, "Base2")
	self:NetworkVar("Int", 5, "Base3")
	
	self:NetworkVar("Float", 0, "Base4")
	self:NetworkVar("Float", 1, "Base5")
	
	if SERVER then
		self:SetBase1(1)
		self:SetBase2(2)
		self:SetBase3(3)
		self:SetBase4(4.4)
		self:SetBase5(5.5)
	end
end

function ENT:Initialize()
	print(self, self:GetClass())
	for i=0, 31 do
		if self:IsDTVarSlotUsed("Int", i) then
			print("Int " .. i .. " used", self:GetDTInt(i))
		end
	end
	for i=0, 31 do
		if self:IsDTVarSlotUsed("Float", i) then
			print("Float " .. i .. " used", self:GetDTFloat(i))
		end
	end
	PrintTable(self:GetNetworkVars())
end

scripted_ents.Register(ENT, "github_test")



local ENT = {}

DEFINE_BASECLASS("github_test")

ENT.Base = "github_test"

function ENT:SetupDataTables()
	BaseClass.SetupDataTables(self)
	
	self:NetworkVar("Int", nil, "Test1") -- backfills Int 0
	self:NetworkVar("Int", "Test2") -- backfills Int 2
	self:NetworkVar("Int", "Test3") -- backfills Int 4
	self:NetworkVar("Int", "Test4") -- uses Int 6
	-- Int 0 through 6 are used
	
	self:NetworkVar("Float", "Test5") -- uses Float 2
	self:NetworkVar("Float", "Test6") -- uses Float 3
	-- Float 0 through 3 are used
	
	if SERVER then
		self:SetTest1(-1)
		self:SetTest2(-2)
		self:SetTest3(-3)
		self:SetTest4(-4)
		self:SetTest5(-5.5)
		self:SetTest6(-6.6)
	end
end

scripted_ents.Register(ENT, "github_test_child")

LeQuackers avatar Apr 09 '21 03:04 LeQuackers