How do DataTables actually work?

Well I’ve read Garry’s post about DataTables and it looks all pretty simple but it doesn’t work.

I’ve done something very basic just to try but my DTVar return 0…

[lua]
function ENT:SetupDataTables()

self:DTVar( "Int", 100, "Health" )

end

function ENT:Use( activator, caller )

print(self.dt.Health)

end
[/lua]

Wasn’t the second argument the index, not the value?

Yes, 0-3… that’s why his example isn’t working.

So self:DTVar() is to declare a variable and you change it using SetDTInt() (if it’s an int) or something like that?

Also, what are the indexes?

I use this in my battery entity:



function ENT:SetupDataTables( )
	self:DTVar( "Int", 0, "Power" )
end

function ENT:UpdatePower( i )
	if i == 0 then
		self:SetColor( 100, 100, 100, 255 )
	end
	
	self.dt.Power = math.max( 0, i )
end

function ENT:TakePower( i )
	self:UpdatePower( self.dt.Power - i )
end

function ENT:GetPower( )
	return self.dt.Power
end


Make sure to copy the SetupDataTables to your cl_init file ( or do it in shared ) if you haven’t already.

Each value type has four dedicated variables, accessed with the index from 0 to 3. That’s what the index is.

So I can only use 4 ints, 4 floats, 4 bools, etc?

Good news, I made it work.
Thanks.

Can we use them on players?

Yes you can.

Awesome. Thanks.

Just an FYI you don’t need to setup the variables. You can just use SetDTInt(0, 100) for example and it’s synced.

Works with any networked entity as far as I know.

So you can actually use an dummy entity to pass all your others networks vars? Or do players have those too?

Are players entities? I think they are. :smile:

Well, garry was talking about Sents! xD