foreach (y_iterate) - select random player from the same team.
#1

Hi, I'm starting to use foreach. But I do not understand how to select a player so random, that is on the same team (GetPlayerTeam).

Is there an example?
Reply
#2

something like that?:

Code:
    if(strcmp(cmd, "/mission", true) == 0)
	{
        for(new i = 0; i < MAX_PLAYERS; ++i) {
            if(IsPlayerConnected(i)) {
                new RRandom = random(i);
            }
        } 
        new name[MAX_PLAYER_NAME];
        GetPlayerName(RRandom, name, sizeof(name));
        format(coordsstring, sizeof(coordsstring), "Player name is %s %i", name, RRandom);
        SendClientMessage(playerid, COLOR_WHITE,coordsstring);
        return 0x01;
    }
Reply
#3

Code:
new playerteam = GetPlayerTeam(playerid),
      teamarray[MAX_PLAYERS],
      index;
foreach(Player, targetid)
{ 
    if(GetPlayerTeam(targetid) == playerteam)
    {
         teamarray[index] = targetid;
         index++;
    }
}

new randomtarget = teamarray[random(index)]; // this variable now stores the random player.
Should do the work.
Reply
#4

TORKQ: That's really old foreach syntax, you'll get deprecated warning. So:
pawn Code:
foreach(new targetid:Player)
Besides that, it's good.

OP: You can go a step further, using y_groups it'd be as easy as
pawn Code:
Iter_Random(GroupPlayers[GroupOfPlayersOnYourTeam]);
Reply
#5

Quote:
Originally Posted by Misiur
View Post
TORKQ: That's really old foreach syntax, you'll get deprecated warning. So:
pawn Code:
foreach(new targetid:Player)
Besides that, it's good.

OP: You can go a step further, using y_groups it'd be as easy as
pawn Code:
Iter_Random(GroupPlayers[GroupOfPlayersOnYourTeam]);
It doesn't matter if it's old or not, it still usable and works fine in newest Y_ISI libraries (AFAIK)
Reply
#6

https://github.com/Misiur/YSI-Includ....inc#L939-L945

Emphasis on "may become an error later"
Reply
#7

Didn't know that, thanks.
Reply
#8

Thank you very much
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)