sending ragdoll entindex and using darkrp CalcView

Hi,

I have this client side:


net.Receive( "plyRagdoll", function( len )
	LocalPlayer().ragdoll = Entity( net.ReadFloat() );
end );

and this server side after a ragdoll entity has been created:


	net.Start( "plyRagdoll" );	
	net.WriteFloat( ragdoll:EntIndex() );
	net.Send( ply );

I then try to use darkrps CalcView hook with my code:


	hook.Add("CalcView", "rp_deathPOV", function(ply, origin, angles, fov)

	if( LocalPlayer().ragdoll and LocalPlayer().ragdoll:IsValid() and LocalPlayer().deathTimer > 0 or LocalPlayer().knockTimer > 0 ) then
	-- Entity:Alive() is being slow as hell, we might actually see ourselves from third person for frame or two
	if not GAMEMODE.Config.deathpov then return end
		
	local Ragdoll = LocalPlayer().ragdoll;

	local head = Ragdoll:LookupAttachment("eyes")
	head = Ragdoll:GetAttachment(head)
	if not head or not head.Pos then return end

	if not Ragdoll.BonesRattled then
		Ragdoll.BonesRattled = true

		Ragdoll:InvalidateBoneCache()
		Ragdoll:SetupBones()

		local matrix

		for bone = 0, (Ragdoll:GetBoneCount() or 1) do
			if Ragdoll:GetBoneName(bone):lower():find("head") then
				matrix = Ragdoll:GetBoneMatrix(bone)
				break
			end
		end

		if IsValid(matrix) then
			matrix:SetScale(Vector(0, 0, 0))
		end
	end

	view.origin = head.Pos + head.Ang:Up() * 8
	view.angles = head.Ang

	return view
	end
end)

but this just spams Tried to use a NULL entity!


for this line: local head = Ragdoll:LookupAttachment("eyes")

What am I doing wrong, the ragdoll exists and it is received client side?

The issue might be that the entity is created one frame prior to the client seeing it so it’ll always be NULL ( empty / blank entity ) because that’s where you read it…

It’d be better to simply read the EntIndex ( as UInt ) and then check Entity( _p.ragdoll ) in the CalcView instead of trying to read it as an Entity ( note, you can use net.ReadEntity and net.WriteEntity which does the same thing you are doing but will have the same problems )

You should have no issue using GetRagdollEntity like this:


//
// First Person Death - Josh 'Acecool' Moser
//
hook.Add( "CalcView", "FirstPersonDeathView", function( _p, _pos, _ang, _fov )
	if ( IsValid( _p ) ) then return; end

	local _ragdoll = _p:GetRagdollEntity( );
	if ( !IsValid( _ragdoll ) || _ragdoll:GetClass( ) != "prop__ragdoll" ) then return; end

	local _eyes = _ragdoll:GetAttachment( _ragdoll:LookupAttachment( "_eyes" ) );

	local view = {
		_pos = _eyes.Pos;
		_ang = _eyes.Ang;
		_fov = 20;
	};

	return view;
end );