SA-MP Forums Archive
foreach (y_iterate) - select random player from the same team. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: foreach (y_iterate) - select random player from the same team. (/showthread.php?tid=616709)



foreach (y_iterate) - select random player from the same team. - Arthurillo - 09.09.2016

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?


Re: foreach (y_iterate) - select random player from the same team. - xAlecsu - 09.09.2016

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;
    }



Re: foreach (y_iterate) - select random player from the same team. - TORKQ - 09.09.2016

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.


Re: foreach (y_iterate) - select random player from the same team. - Misiur - 09.09.2016

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]);



Re: foreach (y_iterate) - select random player from the same team. - TORKQ - 09.09.2016

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)


Re: foreach (y_iterate) - select random player from the same team. - Misiur - 09.09.2016

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

Emphasis on "may become an error later"


Re: foreach (y_iterate) - select random player from the same team. - TORKQ - 09.09.2016

Didn't know that, thanks.


Re: foreach (y_iterate) - select random player from the same team. - Arthurillo - 10.09.2016

Thank you very much