Other great resources: Official JS API docs, Scripting Forum

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

Throttled event

The event is "throttled", which means that it won't be called for every single attack.

If I understand the C++ source code correctly, Warzone has it's own clustering algorithm that groups your droids and structures in to clusters. When something in a cluster is attacked, the event will be triggered once for that attack (not once for each unit/structure that was damaged) and then there is a short delay before any further attacks on that cluster will trigger the event again.

This means that you can't use eventAttacked() to micromanage units in combat - instead use eventAttackedUnthrottled() which gets called on every single attack.

Overview

This event is called when your droids or structures are attacked.

This event is useful for retreating damaged units, or repairing damage buildings, after an enemy attacks them.

Availability

Warzone 3.1 Beta 1 and above.

Syntax

function eventAttacked(victim, attacker) {
  // do stuff
}

Parameters

ParameterTypeMandatoryDescriptionGame version
victim

Structure object

Droid object

(tick)Which of my things did the enemy attack?3.1 Beta 1
attacker

Structure object

Droid object

(tick)What did the enemy use to attack me?3.1 Beta 1

Return value

Warzone does not process the event handler's return value.

Example

Work out who our main aggressors are
// make an array to count attacks by each player
var attackers = new Array(playerData.length);
// initialise values in array
for (var i=0;i<attackers.length;i++) attackers[i] = 0;
 
// now monitor attacks
function eventAttacked(victim,attacker) {
  // increment counter for the attacking player
  attackers[attacker.player] += 1;
  // find player who has attacked us most
  var maxAttacks = 0;
  var mainAggressor = attacker.player;
  for (var i=0;i<attackers.length;i++) {
    if (attackers[i]>maxAttacks) {
      maxAttacks = attackers[i];
      mainAggressor = i;
    }
  }
  console("Attacked "+maxAttacks+" time(s) by player #"+mainAggressor+" ("+playerData[mainAggressor].colour+")");
}
Is attacked unit retreating?
// useful if you want to play audio messages based on what attacked droid is doing
function eventAttacked(victim) {
  if (victim.type == DROID) {
    switch (victim.order) {
      case DORDER_RTB: {
        // droid is returning to base (retreating)
        break;
      }
      case DORDER_RTR:
      case DORDER_RTR_SPECIFIED: {
        // droid is returning for repairs
      }
      case DORDER_REARM: {
        // VTOL is returning for repairs/rearming
      }
    }
  }
}

 

See also

  • bind() – monitor when stuff that doesn't belong to you gets destroyed
  • Droids – summary of all API features relating to droids
  • eventAttackedUnthrottled() – unthrottled version of eventAttacked()
  • eventDestroyed() – monitor when your stuff gets destroyed
  • Structures – summary of all API features relating to structures
  • No labels