Random Player from a 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: Random Player from a team (
/showthread.php?tid=626659)
Random Player from a team -
xTURBOx - 18.01.2017
PHP код:
GetRandTeamPlayer(playerid)
{
new targetid = INVALID_PLAYER_ID;
for(new i=0;i<MAX_PLAYERS;i++)
{
if(i == playerid) continue;
if(team[i] == TEAM1)
{
targetid = i;
break;
}
}
return targetid;
}
It sometimes return invalid player id, i want it to return a random player from TEAM1
Re: Random Player from a team -
Vince - 18.01.2017
There's nothing random about it. It just picks the first player in that team all the time. I will suggest using foreach and custom iterators; one for each team. Then you can use Iter_Rand(). Otherwise you can get by with a do-while loop but you must be cautious to not let it run infinitely.
PHP код:
GetRandomPlayerInTeam(teamdef)
{
new
randomPlayer = INVALID_PLAYER_ID,
maxId = GetPlayerPoolSize(),
loopGuard = 1000;
do
{
randomPlayer = random(maxId);
}
while(team[randomPlayer] != teamdef && --loopGuard > 0);
return (loopGuard <= 0) ? INVALID_PLAYER_ID : randomPlayer;
}
Again, foreach is probably the better option.