10.12.2012, 12:42
EDIT: I found a bug in your code:
It was getting the highest distance, not the lowest lol, I lost a lot of time with my GM, I was trying to understand what made my bot not to work good. However that code will work if someone has only 1 CNPC.
The good code is:
And will work also with 10 npcs
pawn Code:
stock Npc: GetClosestNPC(playerid) // This function contributes to the above, as said, to chase a player :D
{
new
Float: fDistance,
Float: fHighest,
Npc: iPlayer = INVALID_CNPC
;
foreachcnpc(npcid)
{
fDistance = GetDistanceBetweenPlayerAndNPC(playerid, npcid);
if(fDistance > fHighest) fHighest = fDistance, iPlayer = Npc: npcid;
}
return iPlayer;
}
The good code is:
pawn Code:
new Float: fDistance,
Float: fLowest = 10000.0,
Npc: iPlayer = INVALID_CNPC
;
foreachcnpc(npcid)
{
fDistance = GetDistanceBetweenPlayerAndNPC(playerid, npcid);
if(fDistance < fLowest) fLowest = fDistance, iPlayer = Npc: npcid;
}
return iPlayer;