05.10.2010, 21:19
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
new MyVar,mp = GetMaxPlayers();
start:
MyVar = random(mp);
if(IsPlayerConnected(MyVar))goto end;
goto start;
end:
//code here and use MyVar
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;
}
Here's getting a random player. It could probably be done a better way.
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
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);
}
[17:05:56] Loaded 3 filter scripts. [17:06:15] mine 18432 [17:06:45] thine 30513
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 |
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. |