(info) Other great resources: Official JS API docs, Scripting Forum

countDroid()

Count the number of droids of a given type for a specific player.

 

Syntax

var numDroids = countDroid([droidType[, player]]);

Parameters

ParameterTypeMandatoryNotesWarzone Version
droidTypeConstant(error)

What sort of droid should be counted?

Note that other .droidType constants cannot currently be used and will throw an error if you try to use them.

Default: DROID_ANY

3.1.0
player

Number

Constant

(error)

Which player(s) should the droids be counted for?

You can specify a specific player ID, or one of the following constants:

  • ALLIESSend a chat message to your allies.
  • ALL_PLAYERSSend a chat message to all players.
  • ENEMIESFilter enumeration to enemy objects.

Default: me

3.1.0

Return value

ValueTypeNotesWarzone Version
<number>NumberThe number of droids of the specified type for the specified player(s).3.1.0
<error>ErrorAn error occurred, most likely you used an unsupported droidType or the specified player does not exist.3.1.0

Example

How many trucks do I have?
myTruckCount = countDroid(DROID_CONSTRUCT);
 
// much faster than:
mySlowTruckCount = enumDroid(me, DROID_CONSTRUCT).length;
Roughly how many enemy weapon droids are there?
// we can't count DROID_WEAPON so have to approximate...
 
// start by getting count of all enemy droids
var totalEnemies = countDroid(DROID_ANY, ENEMIES);
 
// now remove trucks and commanders
var approxWeapons = totalEnemies
                    - countDroid(DROID_COMMAND, ENEMIES)
                    - countDroid(DROID_CONSTRUCT, ENEMIES);
 
// there may be some sensors/ECMs, transports and repair droids in there,
// but not that many so the figure will be pretty accurate
 
// if you need to make it more accurate...
var actualWeapons = approxWeapons;
// count weapons for each enemy
playerData.forEach(function(player, id) {
  if (!allianceExistsBetween(me, id)) { // enemy
    actualWeapons -= enumDroid(id, DROID_ECM).length;
    actualWeapons -= enumDroid(id, DROID_REPAIR).length;
    actualWeapons -= enumDroid(id, DROID_SENSOR).length;
    actualWeapons -= enumDroid(id, DROID_SUPERTRANSPORTER).length;
    actualWeapons -= enumDroid(id, DROID_TRANSPORTER).length;
  }
});
// ...but is the extra accuracy worth it?
 
// alternatively, you could do this...
var actualWeapons = 0;
playerData.forEach(function(player, id) {
  if (!allianceExistsBetween(me, id)) { // enemy
    actualWeapons += enumDroid(id, DROID_WEAPON).length;
    actualWeapons += enumDroid(id, DROID_CYBORG).length;
  }
});
// ...but in late game there will be so many weapon droids that doing it
// this way will actually be slower than the more bloaty approach shown
// earlier (there will be very few ecm/repair/sensor/transport droids...)
This function is significantly faster than using enumDroid() and inspecting the .length property of the returned array, because countDroid() is referencing an existing value within the game engine and just returning that value.
Availability 3.1.0+

Requires:

  • Warzone 3.1.0 or above
Contents

Jump to:

See also

Related articles: