How to check if all players are dead? -
BlackWolf120 - 01.04.2011
hi,
how would i do this?
How can i check if all players are dead except one?
Cause this only player that stays alive untill time is up shall be the winner.
All other cant respawn if once killed.
I just toggle them spectating after death using a varible: cantspawn[playerid]=1;
They cant spawn if the variable value is 1.
So how could i check if theres is only one sigle survivor that has the variable value 0?
This would be the winner then.
regards...
Re: How to check if all players are dead? -
JamesC - 01.04.2011
pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
new j;
for(new i; i < MAX_PLAYERS; i++) // Loop through players (foreach Recommended)
{
if(cantspawn[i] == 0) j++; // If someone is alive, increase j by 1.
if(j > 1) break; // If j is more than 1, break loop (for efficiency)
}
if(j == 1) // Check that there is only 1 player alive
{
// killerid is the winner.
}
return 1;
}
Untested - Probably not the most efficient way.
EDIT: This will only work if the second last player is killed by the last player. If he died by falling etc, it wouldn't work. For that you would need to use a timer.
Re: How to check if all players are dead? -
BlackWolf120 - 01.04.2011
thx alot.
I tried it and it didnt work.
So i just have to put ur code into a timer checking every 2 seconds?
Re: How to check if all players are dead? -
BlackWolf120 - 01.04.2011
sorry for the douple post but also if i use a timer it does not work.
Anyone got an idea?
This is very important pls help
AW: Re: How to check if all players are dead? -
Manuel1948 - 01.04.2011
Its important that you set all cantspawn[playerid]=1 on playerconnect if the connecting players shouldn't spawn and play, if you don't want this forgott it. A must-do is to set cantspawn[playerid]=1 on playerdisconnect and on gamemodeinit
[QUOTE=JamesC;1147535]
pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
new j,klid;
for(new i=0; i < MAX_PLAYERS; i++) // Loop through players (foreach Recommended)
{
if(cantspawn[i] == 0) {j++; klid=i;} // If someone is alive, increase j by 1; Save playerid in klid.
if(j > 1) break; // If j is more than 1, break loop (for efficiency)
}
if(j == 1) // Check that there is only 1 player alive
{
// klid is the winner.
}
return 1;
}
Re: How to check if all players are dead? -
BlackWolf120 - 02.04.2011
thx a lot for ur answer.
I didnt forget to set it to 1 on onplayerconnect.
But i cant set it to 1 under ongamemodeinit cause u cant use playerid there.
Do i have to use this code in a timer?
Cause in my timer it does not work.
Pls someone help this is very important!
regards
Re: How to check if all players are dead? -
coole210 - 02.04.2011
Well obviously you have to add
Code:
new cantspawn[MAX_PLAYERS];
to the top of the script and if you want a timer just try this (untested ofc):
Code:
forward DeadTimer();
public DeadTimer()
{
for(new i=0;i < MAX_PLAYERS; i++)
{
if(GetPlayerHealth(i) < 1 && IsPlayerConnected(i) == 1)
{
cantspawn[i] = 1;
}
}
}
Didn't indent.
Re: How to check if all players are dead? -
BlackWolf120 - 02.04.2011
thx.
Ofc i did define it as a [MAX_PLAYERS] variable.
And would this really work? To check if the player has a health amont of 0 ??