SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 09/09/2016, 12:58 PM   #1
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default foreach (y_iterate) - select random player from the same team.

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?
Arthurillo is offline   Reply With Quote
Old 09/09/2016, 01:03 PM   #2
xAlecsu
Big Clucker
 
Join Date: Aug 2013
Posts: 188
Reputation: 2
Default Re: foreach (y_iterate) - select random player from the same team.

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;
    }
xAlecsu is offline   Reply With Quote
Old 09/09/2016, 01:04 PM   #3
TORKQ
Little Clucker
 
TORKQ's Avatar
 
Join Date: Aug 2016
Location: Lithuania
Posts: 13
Reputation: 0
Default Re: foreach (y_iterate) - select random player from the same team.

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.
TORKQ is offline   Reply With Quote
Old 09/09/2016, 01:15 PM   #4
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,528
Reputation: 556
Default Re: foreach (y_iterate) - select random player from the same team.

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]);
Misiur is offline   Reply With Quote
Old 09/09/2016, 01:26 PM   #5
TORKQ
Little Clucker
 
TORKQ's Avatar
 
Join Date: Aug 2016
Location: Lithuania
Posts: 13
Reputation: 0
Default Re: foreach (y_iterate) - select random player from the same team.

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)
TORKQ is offline   Reply With Quote
Old 09/09/2016, 02:55 PM   #6
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,528
Reputation: 556
Default Re: foreach (y_iterate) - select random player from the same team.

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

Emphasis on "may become an error later"
Misiur is offline   Reply With Quote
Old 09/09/2016, 08:27 PM   #7
TORKQ
Little Clucker
 
TORKQ's Avatar
 
Join Date: Aug 2016
Location: Lithuania
Posts: 13
Reputation: 0
Default Re: foreach (y_iterate) - select random player from the same team.

Didn't know that, thanks.
TORKQ is offline   Reply With Quote
Old 10/09/2016, 01:48 AM   #8
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default Re: foreach (y_iterate) - select random player from the same team.

Thank you very much
Arthurillo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] y_iterate (foreach 3.0) Misiur Tutorials 25 19/09/2017 09:17 PM
Random player select VoViNaToR Help Archive 6 14/06/2011 07:22 PM
Random Player select Jay. Help Archive 13 17/04/2011 04:33 PM
random select player = <?> hipy Help Archive 6 22/07/2009 10:43 AM


All times are GMT. The time now is 02:29 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.