Function isn't working
#1

Okay, so I made this function:

pawn Код:
stock SendAdminMessage(color, message[], level, va_args<>)
{
    new
        szFormattedMessage[256];
    va_format(szFormattedMessage, sizeof(szFormattedMessage), message, va_start<3>);
    if(level > MAX_ADMIN_LEVELS) level = MAX_ADMIN_LEVELS;
    else if(level < 1) level = 1;
    for(; level <= MAX_ADMIN_LEVELS; level++)
    {
        foreach(Group(gGroupAdminLevels[level-1]), playerid)
        {
            SendClientMessage(playerid, color, szFormattedMessage);
        }
    }
}
It compiles with no issues, however when I'm using it in-game, crashdetect gives me a run time error:

Run time error 4: "Array index out of bounds"
Accessing element at negative index -1

I've checked the function hundred of times and I can't understand why it would access a negative index.
Reply
#2

Try the script below, just change it how you wish:
PHP код:
stock SendAdminMessage(colorlvlstring[])
{
    foreach(
Playeri)
    {
        if(
IsPlayerConnected(i) && PlayerInfo[i][AdminRank] >= lvl)
        {
            
SendClientMessage(icolorstring);
        }
    }
    return 
1;

Reply
#3

This is less efficient than my code.
My code only iterates through admins who match the admin level specified.
Also it lets you format the message in the function itself, instead of doing it seperatley.

Thank's anyway.
Reply
#4

Try compiling with the -d3 flag. It'll show you at which line it crashed in the crashdetect log
Reply
#5

I added printing every line, and it seems like the foreach loop is causing troubles.
I tried to change the foreach line to this:

pawn Код:
foreach(new playerid : Group(gGroupAdminLevels[level-1]))
But there is no difference at all.
Reply
#6

Try this :

pawn Код:
stock SendMessageToAdmins(color, const string[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerInfo[i][Admin] >= 1)
        SendClientMessage(i, color, string);
    }
    return 1;
}
Reply
#7

Quote:
Originally Posted by Maxips2
Посмотреть сообщение
I added printing every line, and it seems like the foreach loop is causing troubles.
I tried to change the foreach line to this:

pawn Код:
foreach(new playerid : Group(gGroupAdminLevels[level-1]))
But there is no difference at all.
Is Group(gGroupAdminLevels[level-1])) an iterator?
Reply
#8

No, its a group, but it does't need to be an iterator, foreach supports groups.
http://forum.sa-mp.com/showthread.ph...light=y_groups

I declared it like that:

Group:gGroupAdminLevels[MAX_ADMIN_LEVELS]
Reply
#9

Could you place for me printf("Currently at: %d", level) inside for, but before foreach?
Reply
#10

pawn Код:
stock SendAdminMessage(color, message[], level, va_args<>)
{
    new
        szFormattedMessage[256];
    va_format(szFormattedMessage, sizeof(szFormattedMessage), message, va_start<3>);
    if(level > MAX_ADMIN_LEVELS) level = MAX_ADMIN_LEVELS;
    else if(level < 1) level = 1;
    for(; level <= MAX_ADMIN_LEVELS; level++)
    {
        printf("Level: %d", level);
        foreach(new playerid : Group(gGroupAdminLevels[level]))
        {
            SendClientMessage(playerid, color, szFormattedMessage);
        }
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)