Jump to content

Tutorial - Alegerea unui ID / Jucator random de pe server.


[RW]Robi

Recommended Posts

Salut, acesta este un tutorial despre creearea unui system care alege un ID / Jucator random dintre jucatori conectati pe server, deci sa incepem.

Quote

new jucatorionline = 0;

la OnPlayerConnect adaugam:

Quote

jucatorionline++;

la OnPlayerDisconnect adaugam

Quote

jucatorionline--;

creeam o comanda zcmd

CMD:randomid(playerid, params[])
{      
   new string[128];        
   o vom restrictiona, in cazul meu nu stiu ce aveti voi in gm deci...eu adaug o restrictionare 
   if(IsPlayerAdmin(playerid))        
   {              

       new online = 0 + random(jucatorionline);            
       format(string, sizeof(string) , "ID: %d a fost extras prin systemul random", online);            
       SendClientMessageToAll(-1, string);      

   }      
   return 1;

}

 

Edited by [RW]Robi

                        

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Este cam simplu, si cred ca ar avea o eroare, daca un player se deconecteaza idul lui ramane liber si ar putea sa cada la comanda,

Ar trebui sa faci sa aleaga un numar din MAX_PLAYERS dupa sa verifici daca acel numar este conectat IsPlayerConnected(randomonline) si daca nu e sa se repete functia pana gaseste un id online cea ce ai facut tu nu cred ca este corect

  • Upvote 2

 

    __  ____      __           
   /  |/  (_)____/ /____  _____
  / /|_/ / / ___/ __/ _ \/ ___/
 / /  / / (__  ) /_/  __/ /    
/_/  /_/_/____/\__/\___/_/     
SERVICII SCRIPTING DE CALITATE
Pagina     Scripting     pawn
Link to comment
Share on other sites

public ConnectedPlayers()
{
	new Connected; foreach(new i: Player) if(IsPlayerConnected(i)) Connected++; return Connected;
}
CMD:test(playerid)
{
   if(IsPlayerAdmin(playerid))        
   {              
       new ROnline = random(ConnectedPlayers()), Str[128];
       //---------------------------------------------------------------------------------
       format(Str, sizeof(Str) , "ID: %d a fost extras prin system-ul random!", ROnline);            
       SendClientMessageToAll(-1, Str);      
   }      
   else return 0;
}
//------------------------------------------------------------------------

Ceva de genul! 

Sper sa mearga! :)

350x20_FFFFFF_FFFFFF_000000_000000.png

Link to comment
Share on other sites

On 09.01.2016 at 16:22, Voller. said:

public ConnectedPlayers()
{
	new Connected; foreach(new i: Player) if(IsPlayerConnected(i)) Connected++; return Connected;
}
CMD:test(playerid)
{
   if(IsPlayerAdmin(playerid))        
   {              
       new ROnline = random(ConnectedPlayers()), Str[128];
       //---------------------------------------------------------------------------------
       format(Str, sizeof(Str) , "ID: %d a fost extras prin system-ul random!", ROnline);            
       SendClientMessageToAll(-1, Str);      
   }      
   else return 0;
}
//------------------------------------------------------------------------

Ceva de genul! 

Sper sa mearga! :)

Merge si nu prea. Chestia aia returneaza nr-ul de jucatori conectati. Daca tu ai 10 jucatori in momentul acela, va fi extras un id de la 1 la 10 chiar daca poate, sunt jucatori conectati peste ID 10 sau exista jucatori neconectati cu un ID mai mic de 10.

stock _GetRandomPlayer()
{
	new _players[MAX_PLAYERS], i, l, x = GetPlayerPoolSize();

	for(l = 0; l < x; l++) if(IsPlayerConnected(l)) _players[++i] = l;
	
	return _players[random(i)];
}

This should do the trick.

  • Upvote 1

3r4AlPA.png

Link to comment
Share on other sites

20 hours ago, [XSS]MaxXx said:

Merge si nu prea. Chestia aia returneaza nr-ul de jucatori conectati. Daca tu ai 10 jucatori in momentul acela, va fi extras un id de la 1 la 10 chiar daca poate, sunt jucatori conectati peste ID 10 sau exista jucatori neconectati cu un ID mai mic de 10.


stock _GetRandomPlayer()
{
	new _players[MAX_PLAYERS], i, l, x = GetPlayerPoolSize();

	for(l = 0; l < x; l++) if(IsPlayerConnected(l)) _players[++i] = l;
	
	return _players[random(i)];
}

This should do the trick.

Ok, boss! ;)

350x20_FFFFFF_FFFFFF_000000_000000.png

Link to comment
Share on other sites

  • 1 month later...

Scuze de bump, dar nu m-am putut abtine sa nu postez varianta mea :))

forward Chose_RandomPlayer();
public Choose_RandomPlayer()
{
        for(new i , L = GetPlayerPoolSize() ; i < L ; i++)
        {
            if(IsPlayerConnected(i)) break;
            if(i == MAX_PLAYERS - 1) { alesul = -1; return; }
        }
        new posibil_ales= -1;
        while(!IsPlayerConnected(posibil_ales))
        {
            posibil_ales = random(MAX_PLAYERS);
        }
        alesul = posibil_ales;
        printf("L-am ales pe %d",alesul);

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more details you can also review our Terms of Use and Privacy Policy.