function Get nearest player?

Started by Kewun, Aug 18, 2016, 08:04 AM

Previous topic - Next topic

Kewun

In lu there is GetClosestPlayer() but in vcmp no? i want to add a bind key to my server if player presses X, tries to arrest the nearest  wanted player

how? i need getclosestplayer function

KAKAN

Use a loop and check each player's distance.
for ex:-
local cplr = 0;
for( local i =0; i< 50; i++ )
{
local plr = FindPlayer( i );
if( !plr ) continue;
local dis = DistanceFromPoint( player.Pos.x, player.Pos.y, plr.Pos.x, plr.Pos.y ); //Correct it, it might be wrong, or use player.Pos.Distance( plr.Pos ) if you want to add z.
if( dis < cplr ) cplr = dis;
}
player.Msg( cplr );
Correct it, it might be wrong. Just an example.
oh no

Kewun

hmhmhmmrhmhmhphm... okay ill test this thing out and mess with it.. ill mark this topic as solved if i will make it work..

DizzasTeR

function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}
// ----

local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m

Untested, if you want to get nearest player, specially inside cases that it calls the function everytime 'X' is pressed, then you should keep a max range, you don't want people in one corner of the map to arrest a guy in the opposite corner if that's the nearest to him.

Kewun

if i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller

Murdock

Quote from: Kewun on Aug 18, 2016, 01:02 PMif i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller

That sure must be hard to fix yourself..

Kewun

not hard, im trying to fix it with my self

DizzasTeR

Did you test it with multiple people on the server? I won't be surprised to hear a negative answer.

.

#8
Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.

It's actually what was supposed to happen because the source player is the closest to itself and there's no guard against that.

The following code:
if( player ) {
Could be changed into:
if( player != null && player != source ) {
Which can probably be collapsed into:
if( player != source ) {
Assuming that `source` is not null.
.

Kewun

Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.

tested with multiple players.


even if ( player != source ) { didnt work

ill try to fix..

Kewun

well after using if ( player != source ) { i could'nt arrest anyone

KAKAN

Quote from: Kewun on Aug 18, 2016, 01:22 PMwell after using if ( player != source ) { i could'nt arrest anyone
show us your updated code.
it should be:-
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player != null && player != source ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}
// ----

local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m
oh no

Kewun


Kewun

damn shit fixing it is hard for me a bit

EK.IceFlake

Use original code and compare their IDs. Comparing instances will not get you anywhere.