Enumerate entities (tm.enumerateEntities)
Client side only. Lazy iterators over the game's entity pools. One handle is produced at a time, so finding the first match never builds a table of the whole pool.
lua
for ped in tm.enumerateEntities.peds() do
if not IsPedAPlayer(ped) then
-- an NPC
end
endFunctions
| Call | Iterates |
|---|---|
tm.enumerateEntities.peds() | Peds |
tm.enumerateEntities.vehicles() | Vehicles |
tm.enumerateEntities.objects() | Objects (props) |
tm.enumerateEntities.pickups() | Pickups |
tm.enumerateEntities.ofType(type) | One of the above by 'ped', 'vehicle', 'object', 'pickup', or the numbers 1 (ped), 2 (vehicle), 3 (object). Anything else yields nothing. |
Each call returns a fresh iterator, so use it directly in a generic for.
Cleanup
The native find handle is released when the loop finishes. If you break out early, the handle is released when the iterator is garbage collected, so an abandoned loop does not leak it.
Compared with GetGamePool
GetGamePool returns a pre-built Lua table of every handle. The iterators stop as soon as you stop asking, which is cheaper when you only need one or a few entities.
lua
local function findNearestVehicle(coords, maxDist)
local nearest, dist = nil, maxDist
for vehicle in tm.enumerateEntities.vehicles() do
local d = #(GetEntityCoords(vehicle) - coords)
if d < dist then nearest, dist = vehicle, d end
end
return nearest
end