Loop Help?
#1

Hi guys, how to loop an enum??
i mean
i have an enum like
pawn Код:
enum RankInfo
{
    RankNumber,
    RankName[128],
    RankXP
}

new Ranks [] [RankInfo] =
{
    // code
}
Now i want to loop through RankXP and get the Rank Number And Name.
i mean:
if the XP or Score of player is in between the Rank XP i mentioned, i want to get the Rank Number of that Rank and also the Rank Name of it.
Reply
#2

make a stock for the names and use a switch to get the exp and set the rank
Reply
#3

Can't understand what u are saying, but its 70 ranks. Can u give me an example Script?
Reply
#4

pawn Код:
//on top of your script
forward rankcheck(playerid);

//under your RankInfo enum
new pInfo[MAX_PLAYERS][RankInfo];

//somewhere under in your script
//(the case : 500 etc means 500 is the amount of exp you need for the rank up)
public rankcheck(playerid)
{
    switch([pInfo][playerid][RankXP])
    {
        case 0: pInfo[playerid][RankNumber] = 1);
        case 500: pInfor[playerid][RankNumber] = 2);
        case 1500: pInfo[playerid][RankNumber] = 3);
    }
    return 1;
}
//when the player updates check for his rank
public OnPlayerUpdate(playerid)
{
    rankcheck(playerid);
}
//check the killerids rank
public OnPlayerDeath(playerid, killerid, reason)
{
    rankcheck(killerid);
}
//check the rank when the player spawns
public OnPlayerSpawn(playerid)
{
    rankcheck(playerid);
}

//under the public rankcheck (or at your other stocks)
stock GetRankName(playerid)
{
    new rankname[128];
    switch(pInfo[playerid][RankNumber])
    {
        case 0: adminname = "Nooby"; //rank 0 etc
        case 1: adminname = "Rank 1";
        case 2: adminname = "Rank 2";
    }
    return rankname;
}
Reply
#5

pawn Код:
public rankcheck(playerid)
{
    switch([pInfo][playerid][RankXP])
    {
        case 0: pInfo[playerid][RankNumber] = 1);
        case 500: pInfor[playerid][RankNumber] = 2);
        case 1500: pInfo[playerid][RankNumber] = 3);
    }
    return 1;
}
if i do like that, it will cee if the player score is 1500 not between it. Ty for help.
Reply
#6

Glad i could help !
Reply
#7

Just loop through it and stop if the xp required is lower or equal to the player exp
pawn Код:
GetRank(exp, rank = 0) {
    if(exp < Ranks[0][RankXP]) {
        return 0;
    }
    if(!(0 <= rank < sizeof Ranks) || (exp < Ranks[rank][RankXP])) {
        rank = 0;
    }
    while((rank < sizeof Ranks) && (Ranks[rank][RankXP] <= exp)) rank++;

    return rank - 1;
}
Using a switch is only possible if you check each time the player gets experience (you should do that anyways)
And the important fact, it will only work if you only add constant numbers which will match the values in the switch
Reply
#8

Will those code work for me?
Reply
#9

Код:
new pRank =0;
for(new i=0; i<sizeof(Ranks); i++)
{
     if(GetPlayerScore(playerid) >= Ranks[i][RankXP])
     {
         pRank =i;
     }
}
That will give you the players rank, as it will stop when the right score is reached. Then you can use Ranks[pRank][RankName] etc
Reply
#10

pawn Код:
stock GetPlayerRankNumber(playerid, exp)
{
    new
        i = -1,
        exp = GetPlayerScore(playerid);
    ;
    while((++i < sizeof Ranks) && (exp < Ranks[i][RankXP]))
    {
        if( exp > Ranks[i][RankXP])
        {
            i++;
            if(exp < Ranks[i][RankXP])
            {
                Ranks[playerid][RankNumber] = Ranks[i-1][RankXP];
            }
        }
        return Ranks[playerid][RankNumber];
    }
    if(i == sizeof Ranks)
    {
        return printf("Warning: Experience must match at least the lowest rank (%d / %d)", exp, Rank[0][RankXp]);
    }
}
Would This Work?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)