SA-MP Forums Archive
need help in iter_random :( - 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: need help in iter_random :( (/showthread.php?tid=520047)



need help in iter_random :( - Champ - 17.06.2014

I am trying to make a function in which the system chooses on player from each team at a time. But it is not working perfectly. It only chooses one player from all player online. Not each player from one team.
I mean it chooses one player from all players whereas it has to chooses one player from each team randomly.

Here is the code :

pawn Код:
public RandomBomb(playerid)
{
    if(gTeam[playerid] == Terrorists)
    {
        new GangLeader[MAX_PLAYERS];
        new randPlayer = Iter_Random(Player);


        GangLeader[Terrorists] = randPlayer;
        SetPlayerAttachedObject(GangLeader[Terrorists], 0, 1252, 1, 0.039000, -0.192999, 0.000000, 0.000000, 87.299972, 1.000000, 1.000000, 1.000000, 1.000000, 0, 0);
        SendClientMessage(GangLeader[Terrorists], -1, " You have been chosen to Plant the bomb.");
        tChosen[playerid] = 1;
    }
    return 1;
}
   
public RandomDefuse(playerid)
{
    if(gTeam[playerid] == Counter_T)
    {
        new CGangLeader[MAX_PLAYERS];
        new CrandPlayer = Iter_Random(Player);


        CGangLeader[Counter_T] = CrandPlayer;
        SetPlayerAttachedObject(CGangLeader[Counter_T], 0, 1719, 1, 0.000000, -0.140999, -0.004999, 88.899978, 0.599999, 0.299999, 1.000000, 1.000000, 1.000000, 0, 0);
        SendClientMessage(CGangLeader[Counter_T], -1, " You have been chosen to Defuse the bomb.");
        cChosen[playerid] = 1;
    }
    return 1;
}
Here, I am calling this function.
pawn Код:
if (ServerMinutes == 3 && ServerSeconds == 54)
        {
            RandomBomb(playerid);
            RandomDefuse(playerid);
           
        }
hope you understood what my problem is.

Please Help


Re: need help in iter_random :( - Vince - 17.06.2014

Since you are already using foreach, why not use two more iterators - one for each team - instead of gTeam? Wherever you use "gTeam[playerid] =", you instead use Iter_Add and Iter_Remove.


Re: need help in iter_random :( - Champ - 17.06.2014

Sir, Can you give me an example. I really don't know about Iter function


Re: need help in iter_random :( - Vince - 17.06.2014

pawn Код:
new Iterator:Terrorists<MAX_PLAYERS>;
pawn Код:
Iter_Add(Terrorists, playerid);
// instead of:
gTeam[playerid] = Terrorists;
pawn Код:
Iter_Remove(Terrorists, playerid);
// instead of:
gTeam[playerid] = 0; // (or NO_TEAM)
pawn Код:
foreach(Terrorists, i)
{
    SendClientMessage(i, -1, "You are a terrorist.");
}
pawn Код:
new random_terrorist = Iter_Random(Terrorists);
I'm not sure if foreach guards against duplicate values, but if you're uncomfortable you can check with Iter_Contains before adding the player.


Re: need help in iter_random :( - Champ - 17.06.2014

thanks sir.
You are GREAT

thank you ****** and Vince once again


Re: need help in iter_random :( - Champ - 17.06.2014

******, Is it correct?
pawn Код:
public OnPlayerSpawn(playerid)
{
if(gTeam[playerid] == Terrorists)Iter_Add(Terrorists, playerid);
}



Re: need help in iter_random :( - Champ - 17.06.2014

Sir, How exactly does Iter_Add work ? will it work for adding(+1) or as setting(==) ?


Re: need help in iter_random :( - Champ - 17.06.2014

Sir, As you said.. foreach gaurds adding multiple values same time.

can i do this ?
pawn Код:
if(Iter_Contains(Counter_T, playerid)){Iter_Remove(Counter_T, playerid);}
Iter_Add(Terrorists, playerid);



Re: need help in iter_random :( - Konstantinos - 17.06.2014

You can but it's not really needed because as ****** has said in foreach's thread: If the value is invalid or not present it is simply ignored.


Re: need help in iter_random :( - Champ - 17.06.2014

Oh ok.. Thanks