Return the 3 top scoring
#1

Hello i have this code to loop through and return the highest score but i want to return the 3 top scores for a 1st,2cnd,3rd

Code:
stock GetHighScorePlayer()
{
	new highScore =-1;
	new playerid = INVALID_PLAYER_ID;
	foreach(new i : Player)
	{
		if(PlayerInfo[i][pScore] > highScore)
		{
			playerid = i;
			highScore = PlayerInfo[i][pScore];
		}
	}
	return playerid;
}
Can Anyone help?
Reply
#2

try https://sampforum.blast.hk/showthread.php?tid=105683
Reply
#3

Anybody help? link doesent work.
Reply
#4

Use md-sort = Sort multi-dimensional arrays (enums supported)

You need to sort your array descending by score and than you can extract the first three
pawn Code:
GetHighScorePlayers(array[MAX_PLAYERS][2]) {
    // and we store the playerid and the score in the array
    foreach(new i : Player) {
        array[i][0] = i;
        array[i][1] = PlayerInfo[i][pScore];
    }
    // now we just sort the whole array descending by the value stored in [1] (the score)
    SortDeepArray(array, 1, .order = SORT_DESC);
}
Because you want to return more than one result we need to pass an array to it
pawn Code:
// example
    new
        tPlayer[MAX_PLAYERS][2]
    ;
    GetHighScorePlayers(tPlayer);

    printf("1. %d Score: %d", tPlayer[0][0], tPlayer[0][1]);
    printf("2. %d Score: %d", tPlayer[1][0], tPlayer[1][1]);
    printf("3. %d Score: %d", tPlayer[2][0], tPlayer[2][1]);
    printf("4. %d Score: %d", tPlayer[3][0], tPlayer[3][1]);
    printf("5. %d Score: %d", tPlayer[4][0], tPlayer[4][1]);
    // and so on
Reply
#5

Quote:
Originally Posted by Nero_3D
View Post
Use md-sort = Sort multi-dimensional arrays (enums supported)

You need to sort your array descending by score and than you can extract the first three
Thanks for the reply!
Mhm i dont understand how to use that, can somebody explain
Reply
#6

You can make it by an easy way,make it when it gets the highest one,do the same function,but add if i = playerid continue...
And when it gets the second one make it like playerid2 = i and same for the third one
Reply
#7

Thanks to nero for this code it works perfectly but no i need to update the textdraws any idea how i can do this? this is what i have at the minute

Code:
	GetPlayerName(WinningPlayers[0][0],PlayerInfo[WinningPlayers[0][0]][pName],MAX_PLAYER_NAME);
	format(string, sizeof(string), "%s(%d)", PlayerInfo[WinningPlayers[0][0]][pName],PlayerInfo[WinningPlayers[0][1]]);
	TextDrawSetString(FirstPlace,string);
	TextDrawShowForAll(FirstPlace);
Code:
GetHighScorePlayers(array[MAX_PLAYERS][2]) {
    // and we store the playerid and the score in the array
    foreach(new i : Player) {
        array[i][0] = i;
        array[i][1] = PlayerInfo[i][pScore];
    }
    // now we just sort the whole array descending by the value stored in [1] (the score)
    SortDeepArray(array, 1, .order = SORT_DESC);
}
Code:
    new
        tPlayer[MAX_PLAYERS][2]
    ;
    GetHighScorePlayers(tPlayer);

    printf("1. %d Score: %d", tPlayer[0][0], tPlayer[0][1]);
    printf("2. %d Score: %d", tPlayer[1][0], tPlayer[1][1]);
    printf("3. %d Score: %d", tPlayer[2][0], tPlayer[2][1]);
Reply
#8

That will format the top three into the textdraw "FirstPlace"
pawn Code:
//
    new
        wPlayers[MAX_PLAYERS][2]
    ;
    GetHighScorePlayers(wPlayers);
    // wPlayers[x][0] stores the playerid
    // wPlayers[x][1] stores the score
    new
        first[MAX_PLAYER_NAME],
        second[MAX_PLAYER_NAME],
        third[MAX_PLAYER_NAME],
        string[128]
    ;
    GetPlayerName(wPlayers[0][0], first, sizeof first);
    GetPlayerName(wPlayers[1][0], second, sizeof second);
    GetPlayerName(wPlayers[2][0], third, sizeof third);

    format(string, sizeof string,
        "\
            %s(%d)\n\
            %s(%d)\n\
            %s(%d)\
        "

        , first, wPlayers[0][1]
        , second, wPlayers[1][1]
        , third, wPlayers[2][1]
    );
    TextDrawSetString(FirstPlace, string);
    TextDrawShowForAll(FirstPlace);
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)