SA-MP Forums Archive
Random not working properly - 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 not working properly (/showthread.php?tid=521632)



Random not working properly - Dziugsas - 23.06.2014

Good evening!

I have 5 beds that player spawn in when he dies ,but when i die and for instance my friends we all spawn in same bed..How to make it random for everyone?Bed variables like occupied? D

pawn Код:
new Float:LigoninesLovos[][] =
{
    {-197.1666,-1759.9664,676.4661,272.1911},
    {-200.7865,-1763.9344,676.4661,271.3739},
    {-196.8234,-1771.7848,676.4661,357.3743},
    {-197.1552,-1767.9449,676.4661,4.5810},
    {-197.2890,-1763.8800,676.4661,357.3743}
};

new Random = random(sizeof(LigoninesLovos));
        SetPlayerPos(playerid, LigoninesLovos[Random][0], LigoninesLovos[Random][1], LigoninesLovos[Random][2]);
        SetPlayerFacingAngle(playerid, LigoninesLovos[Random][3]);



Re: Random not working properly - GeekSiMo - 23.06.2014

It's Random! You can't control it


Re: Random not working properly - Dziugsas - 23.06.2014

i know its random but why for not every player?


Re: Random not working properly - Konstantinos - 23.06.2014

pawn Код:
enum e_LigoninesLovos
{
    bool: LL_taken,
    Float: LL_pos[4]
};

new LigoninesLovos[][e_LigoninesLovos] =
{
    {false, {-197.1666,-1759.9664,676.4661,272.1911}},
    {false, {-200.7865,-1763.9344,676.4661,271.3739}},
    {false, {-196.8234,-1771.7848,676.4661,357.3743}},
    {false, {-197.1552,-1767.9449,676.4661,4.5810}},
    {false, {-197.2890,-1763.8800,676.4661,357.3743}}
};

// wherever you want to set the player's position:
new bool: full = true;
for (new i; i != sizeof (LigoninesLovos); ++i)
{
    if (!LigoninesLovos[i][LL_taken])
    {
        full = false;
        break;
    }
}

if (full)
{
    // all beds are taken..
}
else
{
    new Random, count;
    for (;;)
    {
        Random = random(sizeof (LigoninesLovos));
        if (LigoninesLovos[Random][LL_taken]) continue;
        SetPlayerPos(playerid, LigoninesLovos[Random][LL_pos][0], LigoninesLovos[Random][LL_pos][1], LigoninesLovos[Random][LL_pos][2]);
        SetPlayerFacingAngle(playerid, LigoninesLovos[Random][LL_pos][3]);
        LigoninesLovos[Random][LL_taken] = true;
        if (++count == sizeof (LigoninesLovos)) break;
    }
}
Don't forget to reset it (to false) when the bed is empty. If the bed is taken, it finds an empty one. In case all of them are taken, do whatever you want (in "if (full)" statement).


Re: Random not working properly - Dziugsas - 23.06.2014

It spawns me there where i should be spawned after healing...


Re: Random not working properly - Dziugsas - 23.06.2014

Here is full code:

pawn Код:
enum e_LigoninesLovos
{
    bool: LL_taken,
    Float: LL_pos[4]
};

new LigoninesLovos[][e_LigoninesLovos] =
{
    {false, {-197.1666,-1759.9664,676.4661,272.1911}},
    {false, {-200.7865,-1763.9344,676.4661,271.3739}},
    {false, {-196.8234,-1771.7848,676.4661,357.3743}},
    {false, {-197.1552,-1767.9449,676.4661,4.5810}},
    {false, {-197.2890,-1763.8800,676.4661,357.3743}}
};


if(PlayerInfo[playerid][Dead] == true) //On player spawn
    {
        TogglePlayerControllable(playerid, 0);
        TogglePlayerSpectating(playerid, 0);
        SetPlayerCameraPos(playerid,-198.4837,-1759.2366,676.7687);
        SetPlayerCameraLookAt(playerid, -198.4377,-1765.7754,675.7687);
        SetPlayerInterior(playerid,3);
        new bool: full = true;
        for (new i; i != sizeof (LigoninesLovos); ++i)
        {
            if (!LigoninesLovos[i][LL_taken])
            {
                full = false;
                break;
            }
        }
       
        if (full)
        {
           
        }
        else
        {
            new Random, count;
            for (;;)
            {
                Random = random(sizeof (LigoninesLovos));
                if (LigoninesLovos[Random][LL_taken]) continue;
                SetPlayerPos(playerid, LigoninesLovos[Random][LL_pos][0], LigoninesLovos[Random][LL_pos][1], LigoninesLovos[Random][LL_pos][2]);
                SetPlayerFacingAngle(playerid, LigoninesLovos[Random][LL_pos][3]);
                LigoninesLovos[Random][LL_taken] = true;
                if (++count == sizeof (LigoninesLovos)) break;
            }
        }
        GameTextForPlayer(playerid,"~w~Gydomas",30000,6);
        SetTimerEx("HospitalHealing",30000,false,"i",playerid);
        ApplyAnimation(playerid,"CRACK","crckdeth2",4.0, 1, 1, 1, 1, 1);
    }


forward HospitalHealing(playerid);
public HospitalHealing(playerid)
{
    PlayerInfo[playerid][Dead] = false;
    SetPlayerInterior(playerid,3);
    SetPlayerPos(playerid,-202.3200,-1761.7898,675.7687);
    GameTextForPlayer(playerid,"~w~Pagydytas",3000,6);
    TogglePlayerControllable(playerid,1);
    TogglePlayerSpectating(playerid,0);
    SetCameraBehindPlayer(playerid);
    ApplyAnimation(playerid, "CARRY", "crry_prtial", 4.0, 0, 0, 0, 0, 0);
    return 1;
}