Little Help
#1

Hello I have trouble with foreach, I don`t know what is problem, but I want to make loop for everyplayer in my group. Here is the code:

pawn Код:
foreach(new i : GetPlayerUniqueSquadID(playerid))
Here is the error:
Код:
error 017: undefined symbol "GetPlayerUniqueSquadID@YSII_Ag"
Can anyone explain me why?
Reply
#2

That's just not how foreach works. It should be used like this:
pawn Код:
foreach(new i : Players)
"i" is where the playerid is stored and "Players" is an iterator. When a player connects they are added to the Players iterator and they are removed when they disconnect.

Here is a post about this in more detail. It also looks at writing your own iterator.
Reply
#3

Seems like you didnt make it as an iterator, you can make it an iterator or you can just use foreach for all of the players and check their squad id, if it matches do whatever you want there.
Reply
#4

Quote:
Originally Posted by R0
Посмотреть сообщение
Seems like you didnt make it as an iterator, you can make it an iterator or you can just use foreach for all of the players and check their squad id, if it matches do whatever you want there.
I get that, but still same bug, only one name is displeyed, but in squad there are two members.

pawn Код:
CMD:squadronmembers(playerid, params[])
{
    if(pInfo[playerid][pSquadID] == 0) return SendError(playerid, "You are not in a squadron!");
    new LongString[1024], ShortString[180], Count = 0;
   
    foreach(Player, i)
    {
        format(StringSquad,sizeof(StringSquad),"SELECT `UserName` FROM `users` WHERE `SquadID` = %d", pInfo[i][pSquadID]);
        mysql_query(StringSquad);
        mysql_store_result();

        new MemberName[MAX_PLAYER_NAME];
        while(mysql_retrieve_row())
        {
            mysql_get_field("UserName", MemberName);
        }

        Count++;
        format(ShortString, sizeof(ShortString), embed_orange"%s\n", MemberName);
        strcat(LongString,ShortString);

        mysql_free_result();
    }

    if(Count == 0) return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX, "{BCF562}Squadron Members", embed_red"There is no members", "Ok", "");
    else return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX ,"{BCF562}Squadron Members", LongString, "Ok", "");
}
Reply
#5

Anyone please, it`s urgent sorry for double posting
Reply
#6

TRY ADDING
PHP код:
new GetPlayerUniqueSquadID@YSII_Ag
At the top
Reply
#7

Quote:
Originally Posted by Trollerz
Посмотреть сообщение
TRY ADDING
PHP код:
new GetPlayerUniqueSquadID@YSII_Ag
At the top
GetPlayerUniqueSquadID is function which is getting unique squadron ID. So it`s can`t be used like variable.
Reply
#8

pawn Код:
CMD:squadronmembers(playerid, params[])
{
    if(pInfo[playerid][pSquadID] == 0) return SendError(playerid, "You are not in a squadron!");
    new LongString[1024], ShortString[180], Count = 0;
   
    foreach(Player, i)
    {
        format(StringSquad,sizeof(StringSquad),"SELECT `UserName` FROM `users` WHERE `SquadID` = %d", pInfo[i][pSquadID]);
        mysql_query(StringSquad);
        mysql_store_result();

        new MemberName[MAX_PLAYER_NAME];
        while(mysql_retrieve_row())
        {
            mysql_get_field("UserName", MemberName);
            Count++;
            format(ShortString, sizeof(ShortString), embed_orange"%s\n", MemberName);
            strcat(LongString,ShortString);
        }
        mysql_free_result();
    }

    if(Count == 0) return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX, "{BCF562}Squadron Members", embed_red"There is no members", "Ok", "");
    else return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX ,"{BCF562}Squadron Members", LongString, "Ok", "");
}
Try that
Reply
#9

Do you want to check online members from a squad?if yes then why bothering selecting from database and..
but If you want to make it show offline members, since there can be only two memebrs, then why not adding an enum for squads and putting SquadMember[MAX_PLAYER_NAME] And SquadMemebr2[MAX_PLAYER_NAME] or you can put them both in SquadMember[2][MAX_PLAYER_NAME], it doesnt matter,and when someone joins a squad place his name in SquadMember... and for checking the members just send a message of their names.
Reply
#10

Quote:
Originally Posted by Trollerz
Посмотреть сообщение
TRY ADDING
PHP код:
new GetPlayerUniqueSquadID@YSII_Ag
At the top
Simply said, no. That's not the fix.

Quote:
Originally Posted by DRIFT_HUNTER
Посмотреть сообщение
pawn Код:
CMD:squadronmembers(playerid, params[])
{
    if(pInfo[playerid][pSquadID] == 0) return SendError(playerid, "You are not in a squadron!");
    new LongString[1024], ShortString[180], Count = 0;
   
    foreach(Player, i)
    {
        format(StringSquad,sizeof(StringSquad),"SELECT `UserName` FROM `users` WHERE `SquadID` = %d", pInfo[i][pSquadID]);
        mysql_query(StringSquad);
        mysql_store_result();

        new MemberName[MAX_PLAYER_NAME];
        while(mysql_retrieve_row())
        {
            mysql_get_field("UserName", MemberName);
            Count++;
            format(ShortString, sizeof(ShortString), embed_orange"%s\n", MemberName);
            strcat(LongString,ShortString);
        }
        mysql_free_result();
    }

    if(Count == 0) return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX, "{BCF562}Squadron Members", embed_red"There is no members", "Ok", "");
    else return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX ,"{BCF562}Squadron Members", LongString, "Ok", "");
}
Try that
foreach being executed this way was deprecated in the latest version - executing your code would give you warnings like warning 219: local variable "using_deprecated_foreach_syntax"

You can not just loop around your group members only, you have to identify them first. Thus, you are going to have to loop through all connected players and selectively choosing the ones that belong to your group. Translating what I said to code would look something like:
pawn Код:
CMD:squadronmembers(playerid, params[])
{
    if(pInfo[playerid][pSquadID] == 0) return SendError(playerid, "You are not in a squadron!");
    new LongString[1024], ShortString[180], Count = 0;
   
    format(StringSquad,sizeof(StringSquad),"SELECT `UserName` FROM `users` WHERE `SquadID` = %d", pInfo[playerid][pSquadID]);
     mysql_query(StringSquad);
     mysql_store_result();

    new MemberName[MAX_PLAYER_NAME];
    while(mysql_retrieve_row())
    {
        mysql_get_field("UserName", MemberName);
        Count ++;
    }

    format(ShortString, sizeof(ShortString), embed_orange"%s\n", MemberName);
    strcat(LongString,ShortString);

    mysql_free_result();

    if(Count == 0) return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX, "{BCF562}Squadron Members", embed_red"There is no members", "Ok", "");
    else return Dialog_Show(playerid, SquadDialog, DIALOG_STYLE_MSGBOX ,"{BCF562}Squadron Members", LongString, "Ok", "");
}
You don't need foreach to loop around your MySQL entries. while() does it for you. Generally speaking, what I wrote looks around in your database for ALL members that belong to your group, whether online or offline. If you're only looking for those who are online, then you don't need to go through loading from the database, and that's when foreach comes in.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)