random select player?
#1

somebody know how to do it?
if we just use random(sizeof(MAX_PLAYERS)) and player is not connect,it will loop don't get stop,and server will "freeze" lol
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
Iter_Random(Player);
No need for include.
PHP код:
random(GetPlayerPoolSize()); 
Reply
#3

Quote:
Originally Posted by GangstaSunny.
Посмотреть сообщение
No need for include.
PHP код:
random(GetPlayerPoolSize()); 
You didn't read the original post... did you...?

Obviously ******' suggestion is the most ethical solution.

The only way to make something equal in operation but not in efficiency is to create an array that stores current player IDs and loop through that instead.
Reply
#4

Quote:
Originally Posted by Threshold
Посмотреть сообщение
You didn't read the original post... did you...?

Obviously ******' suggestion is the most ethical solution.

The only way to make something equal in operation but not in efficiency is to create an array that stores current player IDs and loop through that instead.
I misunderstood, need to sorry for this.

PHP код:
for(new 0MAX_PLAYERSi++)
{
    if(!
IsPlayerConnected(i))continue;
    
//if connected "return 1;" or "break;" here

Would still be a non-include solution.
Reply
#5

Quote:
Originally Posted by 3417512908
Посмотреть сообщение
somebody know how to do it?
if we just use random(sizeof(MAX_PLAYERS)) and player is not connect,it will loop don't get stop,and server will "freeze" lol
Did u even use IsPlayerConnected?
Reply
#6

PHP код:
RandomPlayerID(){
    new 
ValidPlayers[MAX_PLAYERS],cnt;
    for(new 
i,i2=GetPlayerPoolSize(); i2i++)if(IsPlayerConnected(i))ValidPlayers[cnt++]=i;
    return 
cnt==0?-1:ValidPlayers[random(cnt)];

Reply
#7

Quote:
Originally Posted by g1venchy
Посмотреть сообщение
PHP код:
RandomPlayerID(){
    new 
ValidPlayers[MAX_PLAYERS],cnt;
    for(new 
i,i2=GetPlayerPoolSize(); i2i++)if(IsPlayerConnected(i))ValidPlayers[cnt++]=i;
    return 
cnt==0?-1:ValidPlayers[random(cnt)];

Made the same mistake.
GetPlayerPoolSize returns the amount of connected players. For example: 10.
If someone got the ID 11 cause he logged in after ID 10, but ID 10 has been disconnected, he won't be a part of the loop.
Reply
#8

but this codes performance isn't even so bad
and i wrote this code on my own
and GetPlayerPoolSize returns higest player id
Reply
#9

Quote:
Originally Posted by g1venchy
Посмотреть сообщение
but this codes performance isn't even so bad
and i wrote this code on my own
and GetPlayerPoolSize returns higest player id
This code is more unperformed as a Renault twingo.
Reply
#10

Quote:
Originally Posted by g1venchy
Посмотреть сообщение
GetPlayerPoolSize returns higest player id
What if there are ID 0-10 connected and then ID 30. ID between 11 and 30 are empty. Loop will run 30 times when it should only run 12 times.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)