Function isn't working -
Maxips2 - 09.02.2013
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.
Re: Function isn't working -
bensmart469 - 09.02.2013
Try the script below, just change it how you wish:
PHP код:
stock SendAdminMessage(color, lvl, string[])
{
foreach(Player, i)
{
if(IsPlayerConnected(i) && PlayerInfo[i][AdminRank] >= lvl)
{
SendClientMessage(i, color, string);
}
}
return 1;
}
Re: Function isn't working -
Maxips2 - 09.02.2013
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.
Re: Function isn't working -
FUNExtreme - 09.02.2013
Try compiling with the -d3 flag. It'll show you at which line it crashed in the crashdetect log
Re: Function isn't working -
Maxips2 - 09.02.2013
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.
Re: Function isn't working -
Zex Tan - 09.02.2013
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;
}
Re: Function isn't working -
FUNExtreme - 09.02.2013
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?
Re: Function isn't working -
Maxips2 - 09.02.2013
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]
Re: Function isn't working -
Misiur - 09.02.2013
Could you place for me printf("Currently at: %d", level) inside for, but before foreach?
Re: Function isn't working -
Maxips2 - 09.02.2013
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);
}
}
}