3d2d text appear above entity when player view an entity and keep text there until other entity.



function GM:PostDrawOpaqueRenderables() 
	--local trace = LocalPlayer():GetEyeTrace()
	--local angle = trace.HitNormal:Angle()
	--local tr = util.GetPlayerTrace( LocalPlayer() )
	--local trace = util.TraceLine( tr )
	--Angle(0,plyAngles.y,0)
	local tr = util.GetPlayerTrace( LocalPlayer() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return false end
	if (!trace.HitNonWorld) then return false end
	local ply = LocalPlayer()
	--if !IsValid(ply) then return end
	
	local plyPos = ply:GetPos()
	local plyAngles = ply:GetAngles()
	local text_color = Color(255, 200, 60, 255)
	local offset = Angle(0,270,90)
	--local hp = ent:Health().." Health"
	if (trace.Entity:IsPlayer()) then
		cam.Start3D2D(trace.Entity:GetPos()+Vector(0,0,90),  Angle(0,plyAngles.y,0) + offset, 0.1)
			draw.SimpleText(trace.Entity:Nick(), "coolvetica2", 0, -120, text_color, TEXT_ALIGN_CENTER, 1) 
			draw.SimpleText(trace.Entity:Health().." Health", "coolvetica2", 0, 50, text_color, TEXT_ALIGN_CENTER, 1) 
		cam.End3D2D()
	--end
	elseif (trace.Entity:IsNPC()) then
		if (trace.Entity:GetClass() == "npc_alyx") then
	--for _, ent in pairs(ents.FindByClass("npc_alyx")) do
		cam.Start3D2D(trace.Entity:GetPos()+Vector(0,0,90),  Angle(0,plyAngles.y,0) + offset, 0.1)
			draw.SimpleText(trace.Entity:GetClass(), "coolvetica2", 0, -120, text_color, TEXT_ALIGN_CENTER, 1) 
			draw.SimpleText(trace.Entity:Health().." Health", "coolvetica2", 0, 50, text_color, TEXT_ALIGN_CENTER, 1) 
		cam.End3D2D() 
		end
	end
end


I am working on this hard I know trace how is working but how can I keep the text above enity until I change my view into another entity?

Save player entity to a variable like this:

if ( IsValid( trace.Entity )) then
theSavedEntity = trace.Entity
end

and then only use theSavedEntity variable in your drawing code.

declare the variable outside of the hook like this:

local theSavedEntity

hook.Add(…)

-snip-

Where I should put this:

if ( IsValid( trace.Entity )) then
theSavedEntity = trace.Entity
end

I am cheking the callback with print but if entity isnt in trace it wont do anything.

Any other idea?

EDITED:

Im gonna post it in help forum because here not so much people can answer.