Random Player from a team
#1

PHP код:
GetRandTeamPlayer(playerid)
{
    new 
targetid INVALID_PLAYER_ID;
    for(new 
i=0;i<MAX_PLAYERS;i++)
    {
        if(
== 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
Reply
#2

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)