getting a random player
#1

can someone give me some codr where it picks s random person from th server and creates s map icon that follows them? i would try myself but im on my phone atm
Reply
#2

Quote:
Originally Posted by thomas..
Посмотреть сообщение
i would try myself but im on my phone atm
Then you can try doing so when you're not using a phone. If you aren't sure, I'll tell you that you will need a timer.
Reply
#3

if u want a following map icon you have to make a timer and callback for it as mapicons don't have a dynamic placement function (attachTO).
Then u need to make something like this:
pawn Код:
new MyVar,mp = GetMaxPlayers();
start:
MyVar = random(mp);
if(IsPlayerConnected(MyVar))goto end;
goto start;
end:
//code here and use MyVar
NOTE: Labels can only be used in local functions, global jumping is NOT POSSIBLE!
Reply
#4

Here's getting a random player. It could probably be done a better way.

pawn Код:
GetRandomPlayer()
{
    new
        pmax,
        rand;
    for (new i; i < MAX_PLAYERS; ++i) if (IsPlayerConnected(i)) ++pmax;
    while (pmax > 0)
    {
        rand = random(pmax);
        if (IsPlayerConnected(rand)) return rand;
    }
    return INVALID_PLAYER_ID;
}
Work out the markers. To the guy above, using 'goto' is a bad idea.
Reply
#5

Quote:
Originally Posted by bigcomfycouch
Посмотреть сообщение
Here's getting a random player. It could probably be done a better way.

pawn Код:
GetRandomPlayer()
{
    new
        pmax,
        rand;
    for (new i; i < MAX_PLAYERS; ++i) if (IsPlayerConnected(i)) ++pmax;
    while (pmax > 0)
    {
        rand = random(pmax);
        if (IsPlayerConnected(rand)) return rand;
    }
    return INVALID_PLAYER_ID;
}
Work out the markers. To the guy above, using 'goto' is a bad idea.
I think my code is faster.. but we let him choose ;d
1 problem is that if no players are connected it goes into an infite loop.

but here's a fix:
pawn Код:
new MyVar,mp = GetMaxPlayers();
for (new i; i < mp; ++i) if (IsPlayerConnected(i)) goto start; else goto end;
start:
MyVar = random(mp);
if(IsPlayerConnected(MyVar))goto end;
goto start;
end:
//code here and use MyVar
Reply
#6

Here:
pawn Код:
stock RandomPlayerID()
{
    new
       pIDs[MAX_PLAYERS],
       it[2];
       
    for(it[0] = 0, it[1] = 0; it[0] < MAX_PLAYERS; ++it[0])
        if(IsPlayerConnected(it[0]) && !IsPlayerNPC(it[0]))
        {
            pIDs[it[1]] = it[0];
            it[1]++;
        }
       
    return (it[1] != 0) ? (pIDs[random(it[1])]) : (INVALID_PLAYER_ID);
}
As gamer_z said, the loops would take awhile. Even if there was a 200 max player limit, and 1 player was connected with the ID of 79, random might not return 79 for quite some time. With the above code there's not a need to worry about that problem.
Reply
#7

The problem with gamer_z's code is that if any player is connected whilst looping, it immediately starts randomising the chosen ID.

Let's say there are 20 people online, IDs 0-19. The loop starts and sees that a player is connected, so it immediately goes to randomising the value. If they're not connected, it immediately ends the entire function.

Also, you're using GetMaxPlayers() no matter what the highest ID of a connected person happens to be.


As to your code, Tann0rz, I ran a few tests.

http://pastebin.com/2g35BnMr

Код:
[17:05:56]   Loaded 3 filter scripts.
[17:06:15] mine 18432
[17:06:45] thine 30513
Reply
#8

Quote:
Originally Posted by bigcomfycouch
Посмотреть сообщение
As to your code, Tann0rz, I ran a few tests.

http://pastebin.com/2g35BnMr

Код:
[17:05:56]   Loaded 3 filter scripts.
[17:06:15] mine 18432
[17:06:45] thine 30513
Doesn't necessarily mean that your results will always come out that way for you're constantly testing against a random value. Like I said before, if there's 1 player connected and are testing against 200 possible ids, it's highly likely that it will take more time depending on what random returns. As for mine, it is essentially the same as ******' Iter_Random() function (except the iterator is created each time, which makes his more efficient in any case).

Or to sum things up: how long mine takes is dependent on player numbers and how long yours takes is dependent on whatever number random decides to throw at you.



If anything, I'd advise you just use "Iter_Random(Player)", OP.
Reply
#9

My results won't always be faster, no, and in fact are slower at very low IDs. After reviewing your code though, I think the following scenario could result in an invalid ID.

There are three players connected, ID 0, 1, and 3. Because the max player ID is three, the random call could return ID 2, which isn't even connected.

I'd also say it's safe to assume using Iter_Random would be a good option.
Reply
#10

Quote:
Originally Posted by bigcomfycouch
Посмотреть сообщение
My results won't always be faster, no, and in fact are slower at very low IDs. After reviewing your code though, I think the following scenario could result in an invalid ID.

There are three players connected, ID 0, 1, and 3. Because the max player ID is three, the random call could return ID 2, which isn't even connected.

I'd also say it's safe to assume using Iter_Random would be a good option.
If MAX_PLAYERS is set to 3, then the max player id possible would be 2, not 3. Random(max) returns a value between 0 and max - 1. Notice that I increment the second iterator variable after each player found. If there are 3 total players online, it would return one of the three player ids in the array accordingly. I suggest you actually give the code a test drive, it works fine.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)