how to get player's nearest Object id - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: how to get player's nearest Object id (
/showthread.php?tid=448640)
how to get player's nearest Object id -
Chrisli520 - 05.07.2013
for example,i used new xx = creatObject(987,x,y,z); to create the object, when a player get near to the xyz coordinate, and type in a command , the screen should appear a message saids your nearest object id is:blabla and the blabla should be xx
Re: how to get player's nearest Object id -
magnusburton - 05.07.2013
Easy as 1, 2, 3 to find. Just use search!
pawn Код:
stock GetPlayerClosestObject(playerid, exception = INVALID_OBJECT_ID)
{
new Float:Distance, target = -1;
for(new v; v < MAX_OBJECTS; v++) if(IsValidObject(v))
{
if(v != exception && (target < 0 || Distance > GetDistancePlayerObject(playerid, v)))
{
target = v;
Distance = GetDistancePlayerObject(playerid, v);
}
}
return target;
}
stock GetDistancePlayerObject(playerid, objectid)
{
new Float:Floats[7];
GetPlayerPos(playerid, Floats[0], Floats[1], Floats[2]);
GetObjectPos(objectid, Floats[3], Floats[4], Floats[5]);
Floats[6] = floatsqroot((Floats[3]-Floats[0])*(Floats[3]-Floats[0])+(Floats[4]-Floats[1])*(Floats[4]-Floats[1])+(Floats[5]-Floats[2])*(Floats[5]-Floats[2]));
return floatround(Floats[6]);
}