Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Excerpt |
---|
This event is called when your droids or structures are attacked. |
Syntax
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
function eventAttacked(victim[, attacker]) |
...
An event that is run when an object belonging to the script’s controlling player is attacked. The attacker parameter may be either a structure or a droid.
...
{
// do stuff
} |
Parameters
Parameter | Type | Mandatory | Description | Game version |
---|---|---|---|---|
victim | Which of my things did the enemy attack? | 3.1 Beta 1 | ||
attacker | Undefined | What did the enemy use to attack me? Note: Sometimes this parameter is undefined. | 3.1 Beta 1 |
Return value
Warzone does not process the event handler's return value.
Notes
This event is useful for retreating damaged units, or repairing damage buildings, after an enemy attacks them.
The event is "throttled", which means that it won't be called for every single attack. Instead, it will only be triggered when a cluster (group of game objects in close proximity to one another) of game objects is attacked. The victim parameter is the first object in the cluster. There is also a short delay before subsequent attacks on the same cluster will trigger the event again:
Warzone 3.1 | Warzone 3.2 |
---|---|
100ms | 1000ms |
If a burning unit flees the cluster, it will start triggering the event at regular intervals until it stops burning – bear this in mind if you're using the event for situation reporting.
If you want to micromanage units during combat consider using eventAttackedUnthrottled() instead, as it gets triggered every time a game object takes damage.
Example
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// make an array to count attacks by each player
var attackers = [];
for (var i=0; i<maxPlayers; i++) attackers[i] = 0;
// now monitor attacks
function eventAttacked(victim, attacker) {
// record this attack
attackers[attacker.player] += 1;
// find player who has attacked us most
var numAttacks = 1;
var mainEnemy = attacker.player;
attackers.forEach( function count(attacks, fromPlayer) {
if (attacks > numAttacks) {
numAttacks = attacks;
mainEnemy = fromPlayer;
}
} );
console("Player #"+mainEnemy+" attacked us "+numAttacks+" times");
} |
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// 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
break;
}
case DORDER_REARM: {
// VTOL is returning for repairs/rearming
break;
}
}
}
} |
Div | ||||||
---|---|---|---|---|---|---|
| ||||||
Availability
Requires:
|
Div | ||||
---|---|---|---|---|
| ||||
ContentsJump to:
|
Div | ||
---|---|---|
| ||
See alsoRelated articles:
|