[Include] Get Nearest
#1

Introduction
Get Nearest is a small library which get's the nearest object, player, vehicle and dynamic object.







Features

  • Object
  • Player
  • Vehicle
  • Dynamic Object









Installation

  • Download the file.
  • Copy it on your includes directory.
  • Include it on your script using this.
    pawn Code:
    #include <getnearest>











How to use

You can use it like this.

pawn Code:
#include <a_samp>
#include <others/streamer>
#include <others/getnearest>

new Text:SomeTd;
new str[64];

public OnFilterScriptInit()
{
    InitTextDraw();
    return 1;
}

stock InitTextDraw()
{
    SomeTd = TextDrawCreate(44 ,286 , "Object: N/A");
    TextDrawFont(SomeTd , 1);
    TextDrawLetterSize(SomeTd , 0.8, 5.6000000000000005);
    TextDrawColor(SomeTd , 0xa84343FF);
    TextDrawSetOutline(SomeTd , false);
    TextDrawSetProportional(SomeTd , true);
    TextDrawSetShadow(SomeTd , 1);
}

public OnPlayerSpawn(playerid)
{
    TextDrawShowForPlayer(playerid, SomeTd);
    return 1;
}

public OnPlayerUpdate(playerid)
{
    new nearobj = GetNearest(playerid, OBJECT, 1000.0);
    if(nearobj)
    {
        format(str, sizeof(str), "Object: %d", nearobj);
        TextDrawSetString(SomeTd, str);
    }
    return 1;
}








Documentation

pawn Code:
GetNearest(playerid, type, distance);

/*
playerid: The id of the playerid whom you want to get the nearest of.
type: OBJECT | PLAYER | VEHICLE | DYNAMIC_OBJECT.
distance: The highest distance to check.

Returns: Returns the thing's id and if not exist on the distance, returns -1.
*/









Change Log
Code:
v1.1
++
Optimized Script(Thanks to wups and Lorenc for some tips).
Added Distance as parameter(argument).
Fixed Static Object Bug(Forgot to get the correct object coordinates).

v1.0
++
Initial Release







Download

v1.1 Pastebin.
v1.0 Pastebin.
Reply
#2

nice
Reply
#3

Very Nice!
Reply
#4

Just fixed a return bug. Please re-download.
Reply
#5

liked it rep +
can you make to detect nearest pickup?
Reply
#6

I've already seen this function somewhere.
Reply
#7

Quote:
Originally Posted by rati555
View Post
liked it rep +
can you make to detect nearest pickup?
Okay. Added on my to/do list. Both dynamic and static pickup.
Reply
#8

Well done
Reply
#9

Very nice keep it up
Reply
#10

Change
pawn Code:
for(new i = 0; i < CountDynamicObjects(); i++)
to
pawn Code:
for(new i = 0,j = CountDynamicObjects(); ; i < j; i++)
This way you gain efficiency.

Variable names are very common
pawn Code:
new
        Float:px,
        Float:py,
        Float:pz,
        Float:ox,
        Float:oy,
        Float:oz,
        Float:distance = MAX_DISTANCE
;
You should consider something less common, and use them locally. You don't need them global.
+ distance is global. Let's say I call GetClosest(playerid,OBJECT) and it is 2.0 units away. Okay, the next time I call the function, it won't check for anything further than 2.0 units! Test your code next time.



pawn Code:
new Float:odist = floatsqroot(
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
        );
Why all this?
pawn Code:
dist = GetPlayerDistanceFromPoint(playerid,x,y,z);
pawn Code:
stock GetNearest_Object(playerid)
{
        new currentobject = -1;
    GetPlayerPos(playerid, px, py, pz);
    for(new i = 0; i < MAX_OBJECTS; i++)
    {
                if(!IsValidObject(i)) continue;
        GetDynamicObjectPos(i, ox, oy, oz);
It's not GetDynamicObjectPos.

In conclusion: You should look in to the functions, before releasing them. Try to study, look for better methods.
Cheers.
Reply
#11

Slow Clap
Reply
#12

There's a lot of flaws with your code: possible memory consumer =

pawn Код:
stock GetNearest_Player(playerid)
{
        new currentplayer = -1;
    GetPlayerPos(playerid, px, py, pz);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
                if(i == playerid) continue;
        GetPlayerPos(i, ox, oy, oz);
        new Float:odist = floatsqroot(
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
        );
                if (currentplayer == -1)
        {
            currentplayer = i;
            distance = odist;
        }
                else if (odist < distance)
        {
            currentplayer = i;
            distance = odist;
        }
    }
    return currentplayer;
}
No check if the player is connected...
Reply
#13

Version updated. Please re-download after you see this.
Reply
#14

Awesome i just created a command with this to remove nearest building kewl :)
Reply
#15

exelent, good work,
Reply
#16

Thanks for this. I will try it now.
Reply
#17

Nice script.
Reply
#18

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
    {
                if((i == playerid) || (i == INVALID_PLAYER_ID)) continue;
You must check if the player is connected.
i will never be INVALID_PLAYER_ID, i will be from 0 to 500.
Reply
#19

Quote:
Originally Posted by wups
Посмотреть сообщение
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
    {
                if((i == playerid) || (i == INVALID_PLAYER_ID)) continue;
You must check if the player is connected.
i will never be INVALID_PLAYER_ID, i will be from 0 to 500.
Yeah just tried it. It uses much more memory so I've revert back my changes.
Reply
#20

Simple make use of the GetPlayerPos' return value:
pawn Код:
if(GetPlayerPos(i, ox, oy, oz) && i != playerid) {
    // Player is connected, do stuff..
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)