Garry's Mod Wiki

ents.FindInSphere

  table ents.FindInSphere( Vector origin, number radius )

Description

Gets all entities within the specified sphere.

This function internally calls util.IsBoxIntersectingSphere for every entity on the map based on their Orientated Bounding Box.

Arguments

1 Vector origin
Center of the sphere.
2 number radius
Radius of the sphere.

Returns

1 sequental table<Entity>
A table of all found Entitys.

Example

Visualises the FindInSphere function, ignoring entities attached to the player.

local entities = {} local entities_sv = {} local pos = Vector() local rad = 50 if ( SERVER ) then util.AddNetworkString( "send_sv_ents" ) end hook.Add( "Think", "VisualizeFindInSphere", function() local ply = Entity( 1 ) if ( IsValid( ply ) and ply:KeyDown( IN_USE ) ) then pos = ply:GetPos() entities = ents.FindInSphere( pos, rad ) local newTab = {} for id, ent in pairs( entities ) do if ( ent:GetParent() == ply || ent == ply || ent:GetClass() == "gmod_hands" ) then // Ignore this entity else table.insert( newTab, { ent = ent, pos = ent:GetPos(), ang = ent:GetAngles(), mins = ent:OBBMins(), maxs = ent:OBBMaxs(), class = ent:GetClass() } ) end end entities = newTab if ( SERVER ) then net.Start( "send_sv_ents" ) net.WriteTable( entities ) net.Broadcast() end end end ) net.Receive( "send_sv_ents",function( len, ply ) entities_sv = net.ReadTable() end ) local function RenderEnts( enti, clr, isCl ) for id, tabl in pairs( enti ) do local ent = tabl.ent if ( !IsValid( ent ) ) then continue end debugoverlay.EntityTextAtPosition( tabl.pos, isCl and 1 or 0, (isCl and "CL " or "SV ") .. "Entity " .. ent:EntIndex() .. " " .. tabl.class, FrameTime() ) render.DrawWireframeBox( tabl.pos, tabl.ang, tabl.mins, tabl.maxs, clr, true ) end end local mat = Material( "models/wireframe" ) hook.Add( "PostDrawTranslucentRenderables", "VisualizeFindInSphere", function() render.SetMaterial( mat ) render.DrawSphere( pos, rad, 8, 8 ) render.DrawSphere( pos, -rad, 8, 8 ) local clr = Color( 255, 128, 0 ) RenderEnts( entities, clr, true ) clr = Color( 0, 128, 255 ) RenderEnts( entities_sv, clr ) end )
Output:
image.png