Adding prefix to a const string[] ?
#1

Here are two functions I use frequently .. infact, to say the least I use them in all my Admin command .. and there's alot. So .. to make my life easier, I'd like to add a "prefix" of sorts to these that will be sent before the actual string.. here's the functions:

pawn Код:
stock MessageToMods(color,const string[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) == 1) if (PlayerInfo[i][Level] >= 2) SendClientMessage(i, color, string);
    }
    return 1;
}

stock MessageToAdmins(color,const string[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) == 1) if (PlayerInfo[i][Level] >= 3) SendClientMessage(i, color, string);
    }
    return 1;
}
Say I want to add the prefix "Admin Alert: " to them before the string itself .. is this how I would do it?

pawn Код:
stock MessageToAdmins(color,const string[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
      new str[128];
      format(str, sizeof(str), "Admin Message: %s", string);
        if(IsPlayerConnected(i) == 1) if (PlayerInfo[i][Level] >= 3) SendClientMessage(i, color, str);
    }
    return 1;
}
Reply
#2

const means It's a constant, which means that you cannot edit it. Just remove the 'const' out of the function header and you'll be able to edit it
Reply
#3

Just tested the function .. works perfectly lol. Thanks anyway
Reply
#4

Quote:
Originally Posted by dice7
const means It's a constant, which means that you cannot edit it. Just remove the 'const' out of the function header and you'll be able to edit it
Correct, but you don't want to edit the string itself, because that could mess up other functions that use that string.

Your method works _❼_ but creating a variable in a loop is tremendously inefficient.
pawn Код:
stock MessageToAdmins(color,const string[])
{
    new str[128];
    format(str, sizeof(str), "Admin Message: %s", string);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) == 1) if (PlayerInfo[i][Level] >= 3) SendClientMessage(i, color, str);
    }
    return 1;
}
Reply
#5

Quote:
Originally Posted by Joe Staff
Quote:
Originally Posted by dice7
const means It's a constant, which means that you cannot edit it. Just remove the 'const' out of the function header and you'll be able to edit it
Correct, but you don't want to edit the string itself, because that could mess up other functions that use that string.

Your method works _❼_ but creating a variable in a loop is tremendously inefficient.
pawn Код:
stock MessageToAdmins(color,const string[])
{
    new str[128];
    format(str, sizeof(str), "Admin Message: %s", string);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) == 1) if (PlayerInfo[i][Level] >= 3) SendClientMessage(i, color, str);
    }
    return 1;
}
Lol, I think that was a mistake from my earlier days :P There's probably a whole load more in my gamemode that I need to iron out

Thanks for telling me
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)