SetupDataTables() on game.GetWorld()

When can you call SetupDataTables on the worldspawn entity?

Players are a bit easier since you can just



local PLAYER = FindMetaTable("Player")

function PLAYER:SetupDataTables()
	self:NetworkVar( "Entity", 0, "Zone" )
	self:NetworkVar( "String", 0, "ZonePrintName" )
	self:NetworkVar( "Int", 0, "InfectionZone" )
end

hook.Add( "OnEntityCreated", "GAMEMODE_SetupPlayerDataTables", function( ent )
	if ent:IsPlayer() then
		ent:InstallDataTable()
		ent:SetupDataTables()
	end
end )

in a shared file…

What about the worldspawn entity?

You shouldn’t have the need for this. You have net library and SetGlobal* for storing data that is shared between all players.

Replace IsPlayer with IsWorld? Not sure if that would work, but you should have tried that first.

If not there are function like

SetGlobalEntity that you can use. I wouldn’t recommend doing whatever you’re doing like this though.


hook.Add( "OnEntityCreated", "GAMEMODE_SetupDataTables", function( ent )
	if ent:IsPlayer() then
		ent:InstallDataTable()
		ent:NetworkVar( "Entity", 0, "Zone" )
		ent:NetworkVar( "Entity", 1, "Base" )
		ent:NetworkVar( "Int", 0, "InfectionZone" )
		ent:NetworkVar( "Int", 1, "InfectionBase" )
	elseif ent:IsWorld() then
		ent:InstallDataTable()
		ent:NetworkVar( "Int", 0, "InfectionTotal" )
	end
end )

Works like a charm!

Now the real question, is this safe to use?

Safe, probably. Wrong implementation, definitely.

You should be using net.* library for stuff like this, as RB suggested. Especially since, my assumption, is that those values aren’t changing often, if at all.