Foreach - Checking for One Case
#1

Let's say I have a value in my player enum named "Dead".

I want to check if everyone on the server has their dead value as "true". If they are all true, I would like to send a message that says "All players are dead"

Thanks in advance!
Reply
#2

foreach(Player, i)

if(Dead[i] == 1) SendClientMessageToAll(-1, "All players are dead");
Reply
#3

pawn Код:
new c=0;
foreach (new i : Player)
{
    if (dead[i]==true)
    {
        c++;
        if (c==GetPlayersOnServer())
        {
            SendClientMessageToAll();
        }
    }
}
I got the getplayersonline funtion from here: https://sampforum.blast.hk/showthread.php?tid=136750
Reply
#4

pawn Код:
new liCount = 0;
foreach(new i: Player)
    if( dead[ i ] == true )
        liCount ++;

if( liCount == Iter_Count(Player) )
{
    SendClientMessageToAll( -1, 0xFFFFFF, "All players are dead !" );
}
This is the best method if you use foreach.
Reply
#5

Okay, I've got that, but now I have a new question. Let's say I wanted the same thing to happen but I wanted it to check only if one team is dead (pInfo[i][team] == 1)
Reply
#6

No need to count players that is just silly....

pawn Код:
stock AllDead()
{
    foreach(new i : Player)
    {
        if(!PlayerData[i][Dead]) return 0;
    }
    return SendClientMessageToAll( -1, 0xFFFFFF, "All players are dead !" );
}
pawn Код:
stock AllDeadTeam(team)
{
    foreach(new i : Player)
    {
        if(!PlayerData[i][Dead] && GetPlayerTeam(playerid) == team) return 0;
    }
    return SendClientMessageToAll( -1, 0xFFFFFF, "All players are dead on this team!" );
}
Reply
#7

pawn Код:
new bool: someonealive;
foreach(new i : Player)
{
    if(!YourVar[i])
    {
        someonealive = true;
        break;
    }
}
if(!someonealive) //This means that all dead
Reply
#8

Quote:
Originally Posted by stabker
Посмотреть сообщение
pawn Код:
new bool: someonealive;
foreach(new i : Player)
{
    if(!YourVar[i])
    {
        someonealive = true;
        break;
    }
}
if(!someonealive) //This means that all dead
Did you read what I posted?

This will work but is not the best way.
Reply
#9

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Did you read what I posted?

This will work but is not the best way.
This variant is better, faster and more useful than yours, so I posted it. The author would be clearer. (Sorry for my bad English).
Reply
#10

It's not faster, but the difference would be too small to really measure anyways but definitely not faster you will always have two extra steps than my solution.

1). new bool: someonealive;
2). if(!someonealive)

Those two steps will always happen, so explain why your variant is better, faster and more useful.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)