23.07.2012, 18:57
With the latest version, you can sort into other arrays (only for the comparator sorts atm, but that's just the opposite of a limitation).
First you'll need a comparator function:
Then you'll need a temporary array that you sort into:
There is, however, one little complication. Currently, md-sort has no idea what players are. It will blindly sort the whole array, regardless whether the player is connected.
So you won't be able to simply do sorted_racers[2] to get the 3rd top (as old values from disconnected players could be there). One workaround for this is when players disconnect, set the distance to like 9999.
First you'll need a comparator function:
pawn Код:
Comparator:CompareDistanceToFinish(left[E_RACE_DATA], right[E_RACE_DATA]) {
// Returning negative means "left goes above"
// Positive means "left goes below"
// floatcmp returns -1 if right > left, 0 if equal, and 1 if left > right
return floatcmp(left[DistanceToFinish], right[DistanceToFinish]);
}
pawn Код:
new sorted_racers[MAX_PLAYERS];
SortArrayUsingComparator(gPlayerRaceData, CompareDistanceToFinish) => sorted_racers;
for (new i = 0; i < sizeof(sorted_racers); i++) {
new playerid = sorted_racers[i];
if (!IsPlayerConnected(playerid))
continue;
// do stuff with "playerid"
}
So you won't be able to simply do sorted_racers[2] to get the 3rd top (as old values from disconnected players could be there). One workaround for this is when players disconnect, set the distance to like 9999.