Finding all NPCs

I want to draw a box around NPCs but I’m not sure how to find all NPCs easily. I have tried
[lua]local function EnemyBoxes()

for _,npc in pairs(ents.FindByClass("npc_*")) do
	
	local enemyPos = npc:LocalToWorld(npc:OBBCenter()):ToScreen()
	
	draw.RoundedBox(2,enemyPos.x-5,enemyPos.y-5,10,10,Color(240,20,20,200))
	
end

end
hook.Add(“HUDPaint”,“EnemyBoxes”,EnemyBoxes)[/lua]
But nothing draws.
So I tried replacing it with ents.FindByClass(“npc_*”) with ents.FindByClass(“npc_antlion”) and it did draw a box at all antlion positions.

Wildcards don’t work with that function. You have to manually select which entities are npcs…

[lua]
for _, entity in pairs( ents.GetAll( ) ) do

if( entity:IsNPC( ) ) then
	
end

end
[/lua]

That’s really wierd, because I use this exact code:

[lua]
for k,v in pairs(ents.FindByClass(“npc_*”)) do
if v:GetClass() != “npc_rollermine” && v:GetClass() != “npc_turret_floor” then
turret:AddEntityRelationship(v, D_HT, 999)
end
end
[/lua]

And it works perfectly.

I got it from that Useful Code Snippets thing.

Nevecs code did work, thank you. However I would like to know now, whats the best way of drawing a box around an NPC and have it scale with the distance? I did try using OBBMins and OBBMaxs, but the box would skew oddly and from behind wouldn’t work right.

From behind you need to flip the X values, but yes it doesn’t behave correctly.

They DO work, but only if you use the function serverside.