Jump to content

loop question


Xwad

Recommended Posts

I want to check which ped is the closest to the wx,wy,wz position. Is there any easier way do it then this method?

local wx = 0
local wy = 0
local wz = 0

function test()
  for i,v in ipairs(getElementsByType("ped")) do
    local x,y,z = getElementPosition(v)
    local distance = getDistanceBetweenPoints3D(x,y,z,wx,wy,wz)
      for i,v2 in ipairs(getElementsByType("ped")) do
        local x,y,z = getElementPosition(v)
        local distance2 = getDistanceBetweenPoints3D(x,y,z,wx,wy,wz)

          if distance < distance2 then
              ---
          end
	end
end
Link to comment

That's not exactly what i need. I want to loop throught all peds and get the one that is the closest to a given position. Its not that difficult, i just wanna know if theres any simpler way to do it, so i dont have to loop twice

Edited by Xwad
Link to comment

try it

local wx , wy , wz  = 0 , 0 , 0

function test(  )
  	local peds , positions , ped = getElementsByType( "ped" ) , { } , { };
	for index = 1 , #peds do
    	local distance = getDistanceBetween3DPoints( wx , wy , wz , getElementPosition( peds[ index ] ) );
    	table.insert( positions , distance ) ; ped[ distance ] = peds[ index ]
    end
  	return ped[ math.min( unpack( positions ) ) ];
end

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...