Get Nearest - T0pAz - 27.02.2012
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.
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.
Re: Get Nearest -
tyler12 - 27.02.2012
nice
Re: Get Nearest -
Konstantinos - 27.02.2012
Very Nice!
Re: Get Nearest - T0pAz - 27.02.2012
Just fixed a return bug. Please re-download.
Re: Get Nearest -
rati555 - 27.02.2012
liked it rep +
can you make to detect nearest pickup?
Re: Get Nearest -
TheArcher - 27.02.2012
I've already seen this function somewhere.
Re: Get Nearest - T0pAz - 27.02.2012
Quote:
Originally Posted by rati555
liked it rep +
can you make to detect nearest pickup?
|
Okay. Added on my to/do list. Both dynamic and static pickup.
Re: Get Nearest -
asteroid - 27.02.2012
Well done
AW: Get Nearest -
kvn - 27.02.2012
Very nice keep it up
Re: Get Nearest -
wups - 27.02.2012
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 Get
DynamicObjectPos.
In conclusion: You should look in to the functions, before releasing them. Try to study, look for better methods.
Cheers.
Re: Get Nearest -
ang12123 - 27.02.2012
Slow Clap
Re: Get Nearest -
Lorenc_ - 27.02.2012
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...
Re: Get Nearest - T0pAz - 28.02.2012
Version updated. Please re-download after you see this.
Re: Get Nearest -
Niko_boy - 28.02.2012
Awesome i just created a command with this to remove nearest building kewl :

)
Respuesta: Get Nearest -
[Nikk] - 28.02.2012
exelent, good work,
Re: Get Nearest -
Not Applicable - 28.02.2012
Thanks for this. I will try it now.
Re: Get Nearest -
[HK]Ryder[AN] - 28.02.2012
Nice script.
Re: Get Nearest -
wups - 28.02.2012
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.
Re: Get Nearest - T0pAz - 28.02.2012
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.
Re: Get Nearest -
RyDeR` - 28.02.2012
Simple make use of the
GetPlayerPos' return value:
pawn Код:
if(GetPlayerPos(i, ox, oy, oz) && i != playerid) {
// Player is connected, do stuff..
}