Forum

> > CS2D > Scripts > How to get the closest player ID?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch How to get the closest player ID?

2 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt How to get the closest player ID?

Mami Tomoe
User Off Offline

Zitieren
Hello, I would like to be able to find the closest player to the given ID.
For example using a function like this:

closestTarget(1)

Will return the ID of the player closest to the player with the ID of 1.

Thanks in advance.

alt Re: How to get the closest player ID?

TrialAndError
User Off Offline

Zitieren
Check the distance between all living players and the id, get the smallest distance and return the ID of the player. It will return nil if there are no living players.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function distance(x1, y1, x2, y2)
    return (math.sqrt((x2-x1)^2 + (y2-y1)^2))
end

function closestTarget(id)
    local closest;
    for _, target in ipairs(player(0,"tableliving")) do
        if id ~= target then
            local d = distance(player(id, "x"), player(id, "y"), player(target, "x"), player(target, "y"))
            if closest == nil or d < closest.dist then
                closest = {id = target, dist = d}
            end
        end
    end
    return closest ~= nil and closest.id or nil
end
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht