PAWNO crashes/lags
#1

I have no idea why does this happen...

As soon as I add this specific piece of code PAWNO starts lagging hardly and crashing...

Код:
stock SetPlayerRank(playerid)
{
	new score = GetPlayerScore(playerid);
	switch(score)
	{
	    case 0..499: PlayerRank[playerid] = 1;
	    case 500..1999: PlayerRank[playerid] = 2;
	    case 2000..3999: PlayerRank[playerid] = 3;
        case 4000..7999: PlayerRank[playerid] = 4;
        case 8000..9999: PlayerRank[playerid] = 5;
        case 10000..14999: PlayerRank[playerid] = 6;
        case 15000..24999: PlayerRank[playerid] = 7;
        case 25000..29999: PlayerRank[playerid] = 8;
        case 30000..39999: PlayerRank[playerid] = 9;
        case 40000..99999: PlayerRank[playerid] = 10;
	}
	return 1;
}
I've tried various ways, like instead of switch with if(GetPlayerScore...) without new score = GetPlayerScore and everything I know...

How can I fix that? Is there any other way to set rank of player?
Reply
#2

I believe this happens due to lots of case statements(because it stops to crash for me when u comment some of them). I advise you to use binary search which is a very fast way to set rank especialy when you have a big number of ranks.

pawn Код:
static const Ranks[10] = {0, 400, 2000, 4000, 8000, 10000, 15000, 25000, 30000, 40000};
stock SetPlayerRank(playerid)
{
    new score = GetPlayerScore(playerid);
    new l, r, m;
    l = 0; r = sizeof(Ranks);
    while(r > l)
    {
        m = (r+l) >> 1;
        if(Ranks[m] <= score) l = m+1;
        else r = m;
    }
    PlayerRank[playerid] = l;
    return l;
}
Reply
#3

A bit simplified version:
pawn Код:
static const Ranks[10] = {0, 500, 2000, 4000, 8000, 10000, 15000, 25000, 30000, 40000};

stock SetPlayerRank(playerid)
{
    new score = GetPlayerScore(playerid);

    // First, set playerrank to 0
    PlayerRank[playerid] = 0;
    // Loop through the Ranks array
    for (new i; i < sizeof(Ranks); i++)
    {
        // If your score is higher than the value stored in the current index of Ranks
        if (score >= Ranks[i])
            // Add 1 to the playerrank
            PlayerRank[playerid]++;
        else // If the score is lower than the value in the current index of Ranks
            break; // Exit the loop
    }

    return 1;
}
Your pawno was crashing due to the amount of cases.
You could see it as if you were using multiple if-statements:
pawn Код:
if (score == 0) PlayerRank[playerid] = 1;
if (score == 1) PlayerRank[playerid] = 1;
if (score == 2) PlayerRank[playerid] = 1;
if (score == 3) PlayerRank[playerid] = 1;
if (score == 4) PlayerRank[playerid] = 1;
if (score == 5) PlayerRank[playerid] = 1;
if (score == 6) PlayerRank[playerid] = 1;
if (score == 7) PlayerRank[playerid] = 1;
if (score == 8) PlayerRank[playerid] = 1;
if (score == 9) PlayerRank[playerid] = 1;
.
.
.
if (score == 99997) PlayerRank[playerid] = 10;
if (score == 99998) PlayerRank[playerid] = 10;
if (score == 99999) PlayerRank[playerid] = 10;
This would be 100,000 if-statements long, which are compiled one by one.
Reply
#4

ah, thank you both for helping ;P I thought it was because of high values but I didn't know how to do it otherway, thank you very much. Solved.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)