Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

A global function that performs a rudimentary check to determine if a player is alive.

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
var alive = isPlayerAlive(playerID);

Parameters

ParameterTypeMandatoryNotesAPI Version
playerIDNumber(tick)The ID of the player that is to be checked.1.0

Return value

ValueTypeNotes
trueBooleanIndicates the player is alive
falseBooleanIndicates the player is dead
<error>ErrorIf an invalid player ID is specified, an error will be thrown.

Notes

The isPlayerAlive() function is not cached and does not check to see if a player is a spectator or empty slot. As such, it is recommended that you use Players Objects to determine if a player is alive as it will be quicker and more accurate:

Code Block
themeRDark
languagejavascript
titleBest way to check if a player is alive...
linenumberstrue
var alive = Players[playerID].isAlive;

Redefining isPlayerAlive()

You can define a custom isPlayerAlive() function if desired, using the following syntax:

Code Block
themeRDark
languagejavascript
titleDefining a custom isPlayerAlive() function
linenumberstrue
// must be *after* including Define API
 
Define("isPlayerAlive", function isPlayerAlive(playerID) {
  // handle scavenger id gracefully! (see notes below)
 
  // do not use Players API to find out if a player is alive...
  // ...because it will call this function to find out!
  
  // return true if alive, false if dead
});
 
// and *before* including Players API

The Players API will use your custom function instead of its own if the function is defined as specified in the code above. That means that the .isAlive property of players objects will call your function, so you can't use those (or Players.alive which relies on them) to find out if a player is alive, duh!

Because the Players API includes a players object for Scavengers, regardless of whether they are enabled or present on the map, your function must handle that scenario gracefully. If (playerID >= maxPlayers) then you're being asked if scavengers are alive.

If no custom function has been defined prior to including the Players API, a default definition is created in accordance with standard game rules: A player is deemed to be living if they have either a FACTORY or a CYBORG_FACTORY or a DROID.

For reference, the default definition uses code similar to that shown below to determine if a player is alive:

Code Block
themeRDark
languagejavascript
titleDefault definition
linenumberstrue
if (typeof countDroid == "function"((playerID >= maxPlayers) && (!scavengers || scavengerPlayer == -1)) {
  return false; // WZ says they are not on the map
}
if (Define.hasNative("countDroid")) { // WZ 3.2+
  return (countStruct(FACTORY, playerID)
       || countStruct(CYBORG_FACTORY, playerID)
       || countDroid(playerID));
} else { // WZ 3.1
  return (enumStruct(playerID, FACTORY).length
       || enumStruct(playerID, CYBORG_FACTORY).length
       || enumDroid(playerID).length);
}
Div
classbox

Availability

Requires:

  • Warzone 3.1 or above
  • Util API
  • Define API
  • Player API