Foreach - Checking for One Case -
TKZ227 - 27.05.2013
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!
Re: Foreach - Checking for One Case -
SilverKiller - 27.05.2013
foreach(Player, i)
if(Dead[i] == 1) SendClientMessageToAll(-1, "All players are dead");
AW: Foreach - Checking for One Case -
HurtLocker - 27.05.2013
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
Re: Foreach - Checking for One Case -
IstuntmanI - 27.05.2013
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.
Re: Foreach - Checking for One Case -
TKZ227 - 27.05.2013
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)
Re: Foreach - Checking for One Case -
Pottus - 27.05.2013
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!" );
}
Re: Foreach - Checking for One Case -
stabker - 27.05.2013
pawn Код:
new bool: someonealive;
foreach(new i : Player)
{
if(!YourVar[i])
{
someonealive = true;
break;
}
}
if(!someonealive) //This means that all dead
Re: Foreach - Checking for One Case -
Pottus - 27.05.2013
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.
Re: Foreach - Checking for One Case -
stabker - 27.05.2013
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).
Re: Foreach - Checking for One Case -
Pottus - 27.05.2013
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.