Random Player - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Random Player (
/showthread.php?tid=269060)
Random Player -
geerdinho8 - 14.07.2011
Hey, i want to make something that takes a random player, but, how can i make this AND check if the playerid is connected?
Re: Random Player -
Ricop522 - 14.07.2011
pawn Code:
stock RandomID()
{
next:
new
playerid;
playerid = random(GetMaxPlayers());
if(!IsPlayerConnected(playerid))
goto next;
return playerid;
}
//or this
stock players() {
new c;
for(new i = 0; i < MAX_PLAYERS; ++i) {
if(IsPlayerConnected(i))
{
c++ ;
}
}
return c;
}
//using
//random(players())
Re: Random Player -
iggy1 - 14.07.2011
Here's my version. Returns a random player or INVALID_PLAYER_ID if no players are connected. Also its worth noting that foreach has an inbuilt random player function if you already use that library.
pawn Code:
GetRandomPlayer()
{
new
pCount,
rPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...};
for(new playerid; playerid < MAX_PLAYERS; playerid++)
{
if(IsPlayerConnected(playerid))
{
rPlayers[pCount] = playerid;
pCount++;
}
}
if(pCount)
return rPlayers[random(pCount)];
else
return INVALID_PLAYER_ID;
}
Re: Random Player -
Shadoww5 - 14.07.2011
Quote:
Originally Posted by Ricop522
pawn Code:
stock RandomID() { next: new playerid; playerid = random(GetMaxPlayers()); if(!IsPlayerConnected(playerid)) goto next; return playerid; }
//or this stock players() { new c; for(new i = 0; i < MAX_PLAYERS; ++i) { if(IsPlayerConnected(i)) { c++ ; } } return c; } //using //random(players())
|
This is not correct:
stock players().
Because if server has 10 players online and the highest id is 12 this one will not be included at random.
Re: Random Player -
geerdinho8 - 17.07.2011
Quote:
Originally Posted by iggy1
Here's my version. Returns a random player or INVALID_PLAYER_ID if no players are connected. Also its worth noting that foreach has an inbuilt random player function if you already use that library.
pawn Code:
GetRandomPlayer() { new pCount, rPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...}; for(new playerid; playerid < MAX_PLAYERS; playerid++) { if(IsPlayerConnected(playerid)) { rPlayers[pCount] = playerid; pCount++; } } if(pCount) return rPlayers[random(pCount)]; else return INVALID_PLAYER_ID; }
|
Thanks, but now it picks all the online players.
Re: Random Player -
=WoR=Varth - 17.07.2011
I suggest you to use foreach.
It have in-built random player.
EDIT: oh didn't notice iggy1 already mentioned that.
Re: Random Player -
CyNiC - 17.07.2011
You can use my
include.
Example:
pawn Code:
#include <IRC>
#include <a_samp>
new RandomList:PlayersRandomList;
public OnPlayerConnect(playerid)
{
AddItemsToRandomList(PlayersRandomList, playerid);
return 1;
}
public OnPlayerDisconnect(playerid)
{
RemoveItemsFromRandomList(PlayersRandomList, playerid);
return 1;
}
public OnPlayerSpawn(playerid)
{
new msg[128], randomhitforplayer = RandomNumberFromList(PlayersRandomList);
if(randomlistforplayer != INVALID_RANDOM_NUMBER)
{
GetPlayerName(randomhitforplayer, msg, MAX_PLAYER_NAME);
format(msg, sizeof msg, "You have a new hit! Kill {FF4040}%s{FFFFFF}!", msg);
SendClientMessage(playerid, -1, msg);
Hit[playerid] = randomhitforplayer;
}
return 1;
}
public OnPlayerDeath(playerid, killerid, reason)
{
if(IsPlayerConnected(killerid))
{
if(Hit[killerid] == playerid)
{
new msg[128], randomhitforplayer = RandomNumberFromList(PlayersRandomList);
Hit[killerid] = randomhitforplayer;
GetPlayerName(playerid, msg, MAX_PLAYER_NAME);
format(msg, sizeof msg, "You killed %s and earned 500$", msg);
SendClientMessage(killerid, -1, msg);
format(msg, sizeof msg, "You have a new hit to do! Kill {FF4040}%s{FFFFFF}!", msg);
SendClientMessage(killerid, -1, msg);
}
}
}
Re: Random Player -
geerdinho8 - 17.07.2011
Can you explain me what foreach dus, and how it works?
Maybe an example?
Re: Random Player -
=WoR=Varth - 17.07.2011
By reading the topic you can understand what it do.
https://sampforum.blast.hk/showthread.php?tid=92679
Re: Random Player -
geerdinho8 - 17.07.2011
Why should i use foreach if it just does the same as now?