How to keep certain entities in the skybox to network.

So, after rewriting this thing x1000 times because everything that has to do with networking in Garrysmod seems to have it against me-- I cannot seem to find out how to keep my entities from turning nil (not in PVS? not existing on client?) when sent to the client. Anyone have any idea on what to do here?

My output:



I AM BEING CALLED FOR: Dremek | ππŽπ•π€π?
City server sky:
Entity [3391][func_door]
City server sky:
Entity [3393][env_sprite]
City server sky:
Entity [3394][env_sprite]
City server sky:
Entity [3396][light_spot]
City server sky:
Entity [3397][env_sprite]
City server sky:
Entity [3398][env_sprite]
City server sky:
Entity [3399][light_spot]
City server sky:
Entity [3401][light_spot]
City server sky:
Entity [3408][keyframe_rope]
City server sky:
Entity [3409][keyframe_rope]
City server sky:
Entity [3410][keyframe_rope]
City server sky:
Entity [3413][light_spot]
City server sky:
Entity [3509][prop_dynamic]
City server sky:
Entity [3533][keyframe_rope]
City server sky:
Entity [3536][env_sprite]
City server sky:
Entity [3537][env_sprite]
City server sky:
Entity [3541][func_brush]
City server sky:
Entity [3542][env_sprite]
Outlands server sky:
Entity [3846][prop_dynamic]
City server sky:
Entity [3848][func_brush]
Outlands server sky:
Entity [3991][prop_dynamic]
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
City client:
nil
Outlands client:
nil
City client:
nil
Outlands client:
nil

[ERROR] addons/rp_industrial17_skybox/lua/autorun/client/rp_industrial17_skybox.lua:45: Tried to use a NULL entity!
  1. RemoveEffects - [C]:-1
   2. func - addons/rp_industrial17_skybox/lua/autorun/client/rp_industrial17_skybox.lua:45
    3. unknown - lua/includes/extensions/net.lua:32

[ERROR] addons/rp_industrial17_skybox/lua/autorun/client/rp_industrial17_skybox.lua:23: Tried to use a NULL entity!
  1. AddEffects - [C]:-1
   2. func - addons/rp_industrial17_skybox/lua/autorun/client/rp_industrial17_skybox.lua:23
    3. unknown - lua/includes/extensions/net.lua:32


Serverside code:



util.AddNetworkString( "GetCityEnts" )
util.AddNetworkString( "GetOutlandsEnts" )
util.AddNetworkString( "PlayerPostInit" )
util.AddNetworkString( "EnterSky" )
util.AddNetworkString( "ExitSky" )

local CitySkyboxStuff = {
"skybox1_prop",
"citadel",
"nexus_tower",
"nexus_tower_sprite1",
"nexus_tower_sprite2",
"nexus_skyrope1",
"nexus_skyrope2",
"nexus_skyrope3",
"nexus_skyrope4",
"nexus_skyrope5",
"nexus_skyrope6"
}

local OutlandsSkyboxStuff = {
"skybox2_prop",
"skybox2_fog_brush"
}

hook.Add( "SetupPlayerVisibility", "AddOutlandsProp", function()
	AddOriginToPVS( ents.FindByClass( "sky_camera" )[1]:GetPos() )
end)

hook.Add( "Initialize", "SkyboxInit", function()

	print("Loading serversided code for i17 skybox.")

	for _, ent in ipairs(ents.FindByName( "trigger_outlands" )) do
		local bananas = ents.Create("trigger_outlands");
		local entity = bananas:SpawnFunction(ent:OBBMins(), ent:OBBMaxs(), ent:GetPos(), ent:GetAngles())
		print("Spawning the trigger for outlands.")
		bananas:Remove()
		ent:Remove()
	end;
	
	for k, v in pairs( ents.FindByClass( "env_sun" ) ) do
		v:Remove()
	end

	for k, v in pairs( ents.GetAll() ) do
		if ( table.HasValue( OutlandsSkyboxStuff, v:GetName() ) ) then
			v:SetModelScale( 2, 0 )
		end
	end
	
end)

hook.Add( "PlayerFullyLoaded", "SkyBoxInitialize", function( ply )

	print("I AM BEING CALLED FOR: "..ply:GetName())
	
	for k, v in pairs( ents.GetAll() ) do
		if ( table.HasValue( CitySkyboxStuff, v:GetName() ) ) then
			print("City server sky:")
			print(v)
			net.Start( "GetCityEnts" )
			net.WriteEntity( v )
			net.Send( ply )
		elseif ( table.HasValue( OutlandsSkyboxStuff, v:GetName() ) ) then
			print("Outlands server sky:")
			print(v)
			net.Start( "GetOutlandsEnts" )
			net.WriteEntity( v )
			net.Send( ply )
		end
	end
	
	net.Start( "ExitSky" )
	net.WriteEntity( ply )
	net.Send( ply )
	
end)

net.Receive( "PlayerPostInit", function()
	hook.Call( "PlayerFullyLoaded", nil, net.ReadEntity() )
end)


Clientsided code:



local isInOutlands = false;
local CityEnts = {};
local OutlandsEnts = {};

net.Receive( "GetCityEnts", function()
	local ent = net.ReadEntity()
	print("City client:")
	print(v)
	table.insert( CityEnts, ent )
end)

net.Receive( "GetOutlandsEnts", function()
	local ent = net.ReadEntity()
	print("Outlands client:")
	print(v)
	table.insert( OutlandsEnts, ent )
end)

net.Receive( "EnterSky", function()
	local ply = net.ReadEntity()
	if ply ~= LocalPlayer() then return end
	for k, v in pairs ( CityEnts ) do
		v:AddEffects( EF_NODRAW )
	end
	for k, v in pairs ( OutlandsEnts ) do
		v:RemoveEffects( EF_NODRAW )
	end
	hook.Add( "SetupSkyboxFog", "SetupOutlandsFog", function( skyboxscale )
			render.FogMode( 1 )
			render.FogColor( 13, 15, 17 )
			render.FogStart( 50 * skyboxscale )
			render.FogEnd( 10000 * skyboxscale )
			render.FogMaxDensity( 0.9 )
			return true
	end)
end)

net.Receive( "ExitSky", function()
	local ply = net.ReadEntity()
	if ply ~= LocalPlayer() then return end
	for k, v in pairs ( OutlandsEnts ) do
		v:AddEffects( EF_NODRAW )
	end
	for k, v in pairs ( CityEnts ) do
		v:RemoveEffects( EF_NODRAW )
	end
	hook.Remove( "SetupSkyboxFog", "SetupOutlandsFog" )
end)

hook.Add( "InitPostEntity", "PlayerFullyLoadedH", function()
	print("Clientside done Init.")
	net.Start("PlayerPostInit")
	net.WriteEntity( LocalPlayer() )
	net.SendToServer()
end)


My brush entity:



ENT.Type = "brush"
ENT.Spawnable = false -- Nope.
ENT.AdminSpawnable = false -- We want to spawn and modify within lua only and not be controlled outside of the files.

-- Called when the entity is spawned.
function ENT:SpawnFunction(minb, maxb, pos, ang)
    local entity = ents.Create("trigger_outlands");
    entity:Spawn();
	entity:SetCollisionBoundsWS(minb, maxb)
	entity:SetPos(pos)
end

-- Called when the entity inits.
function ENT:Initialize()
	self:SetSolid( SOLID_BBOX ) -- Not Solid.
    self:SetTrigger(true) -- So self is editable with lua.
end

-- Called when the entity starts touching something.
function ENT:StartTouch( entity )
	if ( !entity:IsValid() ) then return end -- Check to make sure the entity is valid.
	if ( !entity:IsPlayer() ) then return end -- Check if a player
	net.Start( "EnterSky" )
	net.WriteEntity( entity )
	net.Send( entity )
end

-- Called when the entity stops touching something.
function ENT:EndTouch( entity )
	if ( !entity:IsValid() ) then return end -- Check to make sure the entity is valid.
	if ( !entity:IsPlayer() ) then return end -- Check if a player
	net.Start( "ExitSky" )
	net.WriteEntity( entity )
	net.Send( entity )
end


I did try in the past to have it send every single time the StartTouch and EndTouch was called, but this seemed unecessary as I would just be sending the same entities over and over. Do entity references change over time and cause the client/server entity references to become nil?

Thanks,

You could force your entity to be always networked using http://wiki.garrysmod.com/page/ENTITY/UpdateTransmitState with TRANSMIT_ALWAYS. And as you have noticed once the client leaves the PVS cluster the entity will vanish for him.

Edit: Keep in mind you can only use it on sents.

Edit 2: You could keep a list of the entities on the client by just storing the ent index, within the hook http://wiki.garrysmod.com/page/GM/NetworkEntityCreated you could check if the ent index is inside your list and then add the effects.

I’m assuming this (clientsided) hook is called when an entity is networked from the server over to the client (or only when it is sent over net.ReadEntity)? How would I check it against my list of names if names are only serverside?

Either way, thanks for the advice! :slight_smile:

Do you think I should try setting a network string or boolean onto the entities serverside and having the client read them when NetworkEntityCreated is called? Instead of trying to send a list to clients?

Ok, so I think it’s the fact that my method of hiding is causing them to stop being the PVS or networking at all, both the AddEffects and SetNoDraw do this.

This is because they hide correctly the first time, but not after that.

EDIT: I discovered Entity:SetPreventTransmit which should work for my needs. Why send it to the client and hide it when you can just not send it in the first place? Testing now.

[editline]8th February 2017[/editline]

i just spent days rewriting this over and over until i realized i had an older version of the code randomly overwriting my /lua/ folder in /addons/blabla/lua.

asdbuo[has]diasji]pda]pji[ndpinjqkwd]pijqwpijerq]pi[hjwe]pijh2u- 4asdasd, kas;dmklaspjkl;dja;lksdj’klo

FUCK