Author Topic: How to display how many current players alive?  (Read 1173 times)

Say if I wanted to make it so every 5 minutes it were to say how many people are still standing in a death-match or some sort of server, how would I do this?

Loop through the clients, and check if(isObject(%client.player)) to check if the player is alive, and increment a variable if so. Then announce that variable and restart a schedule loop.

Can't type code, on an iPad.

Code: [Select]
%count=0;
for(%i=0; %i<clientGroup.getCount(); %i++)
{
if(isObject(clientGroup.getObject(%i).player)
%count++;
}
messageAll('',%count SPC "players alive!");

EDIT:
Can't indent on the forums so I took it to notepad++ and fixed it.
And I thought he wanted the dead, why I added the !, my bad :P

That should work now
« Last Edit: June 27, 2012, 07:42:15 PM by DYLANzzz »

Code: [Select]
%count=0;
for(%i=0; %i<clientGroup.getCount(); %i++)
{
if(!(isObject(clientGroup.getObject(%i).player))
{
%count++;
}
}

messageAll('',%count SPC "players alive!");

I think this is right
Remove that ! In the if and properly indent it (._.), and it should work.
Ninja: you don't need that first line, it defaults to 0.

Code: [Select]
function getNumAlive()
{
    %count = 0;
    for(%i=0;%i<clientGroup.getCount();%i++)
    {
        if(isObject(clientGroup.getObject(%i).player))
            %count++;
    }
    return %count;
}

You don't need that first line. All variables default to 0.

You don't need that first line. All variables default to 0.

It's good practice.

You don't need that first line. All variables default to 0.
What if there were no clients alive? Adding that line ensures that it returns 0 instead of ""

The "correct" way to do it:

Code: [Select]
function getNumAlive()
{
%count = 0;
%players = clientGroup.getCount();

for(%i = 0; %i < %players; %i++) {
%player = clientGroup.getObject(%i).player;
%count += isObject(%player) && %player.getState() !$= "Dead";
}

return %count;
}

Code: [Select]
%count += isObject(%player) && %player.getState() !$= "Dead";
I don't understand how this line works

The "correct" way to do it:

Code: [Select]
function getNumAlive()
{
%count = 0;
%players = clientGroup.getCount();

for(%i = 0; %i < %players; %i++) {
%player = clientGroup.getObject(%i).player;
%count += isObject(%player) && %player.getState() !$= "Dead";
}

return %count;
}
An even better way to do it.
Code: [Select]
function getNumAlive()
{
for(%a = 0; %a < $Server::PlayerCount; %a++)
        {
%count += isObject(ClientGroup.getObject(%a).player);
}
return %count;
}
« Last Edit: June 27, 2012, 09:19:19 PM by Plexious »

I don't understand how this line works
Magic.
An even better way to do it.
Code: [Select]
function getNumAlive()
{
for(%a = 0; %a < $Server::PlayerCount; %a++)
        {
%count += isObject(ClientGroup.getObject(%a).player);
}
return %count;
}
No, I corrected the code on purpose to include the getState part. There are occasions where a player object can still exist but not actually be alive.

Thanks everyone for the help, but I do not see any code that displays the amount of players alive except for DYLAN's. Would I just have to take the variable being created (%a) and put it into a ChatMsgAll line?
« Last Edit: June 27, 2012, 10:35:02 PM by Pass »

No, I corrected the code on purpose to include the getState part. There are occasions where a player object can still exist but not actually be alive.
I messed around and couldn't get getState() to return anything other than "Move". Even if the client's player is dead, their player is "0". If they are loading, their player is "".

Thanks everyone for the help, but I do not see any code that displays the amount of players alive except for DYLAN's. Would I just have to take the variable being created (%a) and put it into a ChatMsgAll line?
messageAll('',"\c6"@ getNumAlive() @" players are alive.");

I don't understand how this line works

If that statement returns true its going to add 1 to itself. If that statement is false (0) its going to add 0 to itself.