Vice City: Multiplayer

Server Development => Scripting and Server Management => Script and Content Requests => Topic started by: Pejson on Dec 22, 2016, 09:24 AM

Title: Random player from table
Post by: Pejson on Dec 22, 2016, 09:24 AM
How to? I need a example.
Title: Re: Random player from table
Post by: jWeb on Dec 22, 2016, 11:29 AM
Search the forum, there's plenty of information.

http://forum.vc-mp.org/?topic=180.msg921#msg921

Also, you can't select random stuff from tables because that's not how tables work.
Title: Re: Random player from table
Post by: EK.IceFlake on Dec 22, 2016, 11:49 AM
Untested
RandomFromTable(tbl)
{
    local tarr;
    foreach (item in tbl) tarr.push(item);
    return tarr[Random(0, tarr.len() - 1)];
}
Use this: http://forum.vc-mp.org/?topic=246.0
Title: Re: Random player from table
Post by: jWeb on Dec 22, 2016, 12:00 PM
No need to create a copy of the table just to select a random value from it:
::srand((::GetTickCount() % ::time()) / 3);

RandomFromTable(tbl)
{
    local ridx = ::rand() % tbl.len(), idx = 0;

    foreach (k, v in tbl)
    {
        if (idx == ridx)
        {
            return v;
        }

        ++idx;
    }
}