Jump to content

getNearestPlayer(player,float:maxdis)


Gothem

Recommended Posts

Then use a function table.sort(resultTable) and see which distance is the smallest.

and how get the distance?

im have that code but dont think it is good :?

function getNearestPlayer(player, maxdis) 
    local pos[6] 
    local players = getElementsByType ( "player" ) 
    local pos[0],pos[1],pos[2] = getElementPosition( player ) 
    for theKey,thePlayer in ipairs(players) do 
        local pos[3],pos[4],pos[5] = getElementPosition( thePlayer ) 
        if (pos[0] >= (pos[3] - maxdis) && pos[0] <= (pos[3] + maxdis) && pos[1] >= (pos[4] - maxdis) && pos[1] <= (pos[4] + maxdis) && pos[2] >= (pos[5] - maxdis) && pos[2] <= (pos[5] + maxdis)) then 
            local i ++ 
            local j = thePlayer 
        end 
    end 
    if(i > 1) then 
        i = 0 
        maxdis - 1 
        getNearestPlayer(player,maxdis) 
    else 
        return j 
    end 
end 

dont know where to apply the tabble.sort

Link to comment

uhh getDistanceBetweenPoints3D is better than that massive if statement... it does same thing then you can just check if its smaller than the max distance and smaller than the most recent minimum distance you store in a variable... if it isn't then set the players name to the variable and his distance then eventually you will end up with the smallest distance in one variable and his element in another could even use a table for storing the above data, just an example.

Link to comment

I use something like:

  
function tuFindNearestElement(to,elementType,maxDistance) 
    local x,y,z = getElementPosition(to)             
    local bestD = maxDistance + 1 
    local bv = nil 
     
    for _,av in pairs(getElementsByType(elementType)) do 
        if av ~= to then 
            local vx,vy,vz = getElementPosition(av) 
            local d = getDistanceBetweenPoints3D(x,y,z,vx,vy,vz) 
            if d < bestD then 
                bestD = d 
                bv = av 
            end 
        end 
    end 
  
    return bv,bestD  
end 
  

You call it like:

  
        local vehicleClosest,distance = tuFindNearestElement(player,"vehicle",100) 
  

This finds the closest "vehicle" to the "player" within a distance of 100 units.

Link to comment

thanks guys :D

i will test what us saying :)

____

EDIT \_________________________________________________________

it send me an error :(

in this line

i++ 

full code:

function getNearestPlayer(player, maxdis) 
    local i = 0 
    local players = getElementsByType ( "player" ) 
    local x,y,z = getElementPosition( player ) 
    for theKey,thePlayer in ipairs(players) do 
        local px,py,pz = getElementPosition( thePlayer ) 
        local dis = getDistanceBetweenPoints3D ( x, y, z, px, py, pz ) 
        if dis <= maxdis then 
            i++ 
            local j = thePlayer 
        end 
    end 
    if(i > 1) then 
        i = 0 
        maxdis - 1 
        getNearestPlayer(player,maxdis) 
    else 
        return j 
    end 
end 

Link to comment

it send me an error :(

No offence but why don't you just use my code above ? It does what you want and works. In your code you've way more errors than you think:

+ Yeah, that i++ should be i=i+1 (as has been pointed out)

+ You have the same problem with maxdis on the following line

+ You return "j" but j was set locally in an "if block" so it won't have a value to return.

+ You don't need recursion to do this - what you're going to end up with if you get it working is a check through all players at one distance. If that fails, you're going to keep calling it with every-decreasing values for "maxdis". Think about how much processing you're going to require just to find the closest. You only need to loop through the elements once to find the closest.

If you did use my code then to find the closest player to "player" you'd just use:

  
local playerClosest,distance = tuFindNearestElement(player,"player",100) 
  

This would set playerClosest to the player element that is closest to the first parameter player - our target (or nil if no other player is found).

Link to comment

it send me an error :(

No offence but why don't you just use my code above ? It does what you want and works. In your code you've way more errors than you think:

+ Yeah, that i++ should be i=i+1 (as has been pointed out)

+ You have the same problem with maxdis on the following line

+ You return "j" but j was set locally in an "if block" so it won't have a value to return. reason down

+ You don't need recursion to do this - what you're going to end up with if you get it working is a check through all players at one distance. If that fails, you're going to keep calling it with every-decreasing values for "maxdis". Think about how much processing you're going to require just to find the closest. You only need to loop through the elements once to find the closest.

If you did use my code then to find the closest player to "player" you'd just use:

  
local playerClosest,distance = tuFindNearestElement(player,"player",100) 
  

This would set playerClosest to the player element that is closest to the first parameter player - our target (or nil if no other player is found). i like it return THE CLOSEST player :wink:

the reason of i dont use your code is:

i like return only 1 player :)

Link to comment

In Lua if a function returns more than one value and you assign it to one variable only the first value will be assigned.

You can use:

local closestPlayer = tuFindNearestElement(player,"player",100) 

This will assign the closest player to the variable closestPlayer. The distance is also returned but it's not assigned as you use only 1 variable.

Link to comment

the reason of i dont use your code is:

i like return only 1 player :)

But it does return only one player ? The two variables being assigned on function return are the closest element (player if you want, of any other element type) and the distance it is from the source player.

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...