Random Player
#1

Hey, i want to make something that takes a random player, but, how can i make this AND check if the playerid is connected?
Reply
#2

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())
Reply
#3

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;
}
Reply
#4

Quote:
Originally Posted by Ricop522
View Post
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.
Reply
#5

Quote:
Originally Posted by iggy1
View Post
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.
Reply
#6

I suggest you to use foreach.
It have in-built random player.

EDIT: oh didn't notice iggy1 already mentioned that.
Reply
#7

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);
        }
    }
}
Reply
#8

Can you explain me what foreach dus, and how it works?
Maybe an example?
Reply
#9

By reading the topic you can understand what it do.
https://sampforum.blast.hk/showthread.php?tid=92679
Reply
#10

Why should i use foreach if it just does the same as now?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)