Drawing entities near player? (removing text afterwards)

I’ve been trying out a method of drawing entities near the player by comparing the X and Z distances between the entity and LocalPlayer(), it works fine however I receive massive FPS drop because I’m technically not getting rid of the text when the distance is further than the limit of (500). Thus I easily lose around 20-30 FPS. So, the real question is - besides removing the hook or setting the color as 0, 0, 0, 0 how would I get rid of the text? I think removing the hook will remove every text object located with that function. Essentially this works, but at the expense of 20 FPS.

By the way, outlineVar was just a way of changing the color depending on if I required an outline for that text or not (in this case I did)

function findDist( varA, varB, outlineVar )
		if varA < 500 and varB < 500 and outlineVar == 0 then
			return Color ( 255, 246, 10, 255 )
		else if varA < 500 and varB < 500 and outlineVar == 1 then
			return Color ( 0, 0, 0, 255 )
		else 
			return Color ( 0, 0, 0, 0 )
		end
	end
end

function drawEntText()
	if !bool then return end
		local getLPos = LocalPlayer():GetPos()
		
		for _,v in pairs( getAllEnt() ) do
			if v:IsValid() then
				local getEntPos = v:GetPos():ToScreen()
				local getFullPos = v:GetPos()
				local relativePosX =  getFullPos.x - getLPos.X
				local relativePosY =  getFullPos.Y - getLPos.Y
		
					createText( v:GetClass(), "Default", getEntPos.x, getEntPos.y, findDist( relativePosX, relativePosY, 0 ), 0, 0, 1, findDist( relativePosX, relativePosY, 1 ) )	
					
			end
		end
end

I don’t even know if this is a good way of calculating distance either - so any input would be well appreciated.

You don’t delete text, you simply just stop drawing it. Your comparing only X and Y positions, which is not totally distance, but just put an if statement checking the distance around your createText function. A lot of optimization could be made also if you want to see even better fps.

I tried to ‘stop drawing it’ but it still drew when I got further.

Replace your getAllEnt() function with this.

[LUA]
local function getAllEnt(PlayerPos)
local tbl = {}
local pos
for k,v in pairs(ents.GetAll())do
pos = v:GetPos()-PlayerPos
if(math.abs(pos.X)<500 && math.abs(pos.Y)<500)then
table.insert(tbl,v)
end
end
return tbl
end
[/LUA]

Then there’s no need to check positions, it will return only the entities you want and draw them however you want.

getAllEnT was ents.GetAll stored in a var, BTW what is PlayerPos?

Here’s everything you need.

[LUA]
local function getAllEntEntities(PlayerPos)
local tbl = {}
local pos
for k,v in pairs(ents.GetAll())do
pos = v:GetPos()-PlayerPos
if(math.abs(pos.X)<500 && math.abs(pos.Y)<500)then
table.insert(tbl,v)
end
end
return tbl
end

function drawEntText()
if !bool then return end

local noOutline = Color ( 255, 246, 10, 255 )
local Outline = Color ( 0, 0, 0, 255 )
local getEntPos
for _,v in pairs( getAllEntities(LocalPlayer():GetPos()) ) do
	getEntPos = v:GetPos():ToScreen()
	createText( v:GetClass(), "Default", getEntPos.x, getEntPos.y, noOutline, 0, 0, 1, Outline )         
end

end
[/LUA]

Edit*
Hmm actually it would be more efficient to just do this.

[LUA]
function drawEntText()
if !bool then return end

local noOutline = Color ( 255, 246, 10, 255 )
local Outline = Color ( 0, 0, 0, 255 )
local getEntPos
local plyPos = LocalPlayer():GetPos()
local entPos
for _,v in pairs( ents.GetAll() ) do
	entPos = v:GetPos() - plyPos
	if(math.abs(entPos.X)&lt;500 && math.abs(entPos.Y)&lt;500)then
		getEntPos = v:GetPos():ToScreen()
		createText( v:GetClass(), "Default", getEntPos.x, getEntPos.y, noOutline, 0, 0, 1, Outline )
	end
end

end
[/LUA]

That doesn’t work, trust me. I’ve tried that (it doesn’t calculate if it’s away only if it’s close?) - also your second example doesn’t even calculate the distance from the player?

Fixed the second script, that was just a mistake.

Either of these do the same thing. If the entity is less than 500 units away in the X-coordinant and it is less than 500 units away in the Y-coordinant then it will draw the text.
The second example just removes an unnecessary loop from the first. These will work if you just try them out, there may be errors but you can get the gist of it and fix them.

The main thing to point out here is the use of absolute values, but also the fact that instead of just changing the text for the entity’s alpha to 0 it just doesn’t draw them getting rid of your fps problem.

If this still doesn’t keep it from drawing, then there is something with your createText() function that is saving all of the entities and keeps drawing the text, but I can’t tell what the function does. Text doesn’t just hang around, it gets cleared every frame and you have to redraw it for it to reappear every frame.

-snip-