Versions Compared

Key

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

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

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
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:

Child pages (Children Display)
depthall
pagePlayer Scope
excerpttrue

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

Code Block
themeRDark
languagejavascript
titleHow many trucks do I have?
linenumberstrue
myTruckCount = countDroid(DROID_CONSTRUCT);
 
// much faster than:
mySlowTruckCount = enumDroid(me, DROID_CONSTRUCT).length;
Code Block
themeRDark
languagejavascript
titleRoughly how many enemy weapon droids are there?
linenumberstrue
// 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...)
Div
classbox suggest

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.

Div
classbox
Availability
Status
colourGreen
title
Stable
3.1.0+

Requires:

  • Warzone 3.1.0 or above
Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

See also

Related articles: