Unit
A unit
object may be created in script to represent a logical unit of soldiers taking part in the battle. The unit object interface permits querying of the state of the unit - to issue orders to the unit a battle_unitcontroller
object must be created to which the unit object may then be associated.
Loaded in Battle |
Unit objects are created from the battle_hierarchy
. They appear at the bottom of this hierarchy - see the battle_hierarchy
page for more information on how unit
objects relate to (and are created from) armies and alliances. Alternatively, and preferably, see the section on scriptunits below for a shorthand method of unit object creation.
Once a handle to a sound effect object is obtained, functions may be called on it to query or modify its state in the following form.
Example - Specification:
<object_name>:<function_name>(<args>)
Example - Creation and Usage:
local sfx_01 = battle_sound_effect:new()
sfx_01:load("test_sound_event") -- calling a function on the object once created
It is highly recommended for client scripts to work with script_unit
objects through the interface provided by the script libraries rather than attempting to manage unit
objects individually. By creating a script_unit
object, a unit
object is automatically created and can be accessed at <script_unit>.unit
. See the script_unit
documentation for more information.
Alternatively the generated_battle
script framework can be used. This automatically creates script_unit
objects (with associated unit objects) and negates the need to set up references to anything more granular than an army.
-
unit:name()
-
Returns the script name for the unit, if any has been set. A script name for the unit may be set in the battle setup, either in the battle xml or on the appropriate record in the
battle_set_piece_armies_units_junctions
table if setting up a set-piece/quest battle. It is valid for multiple units in a given army to have the same script name.Returns:
script namestring
-
unit:unique_ui_id()
-
Returns the unique ui id for the unit, which should match the name of the corresponding unit card.
Returns:
ui udnumber
-
unit:type()
-
Returns the unit type of the unit, from the
land_units
table.Returns:
unit typestring
-
unit:alliance_index()
-
Returns the index number of the alliance the unit belongs to, allowing its
battle_alliance
to be determined or looked up from abattle_alliances
object.Returns:
alliance indexnumber
-
unit:army_index()
-
Returns the index number of the army the unit belongs to, allowing its
battle_army
to be determined or looked up from abattle_armies
object (which itself can be determined from the value returned byunit:alliance_index
).Returns:
army indexnumber
-
unit:unit_class()
-
Returns the class of the unit, from the
land_units
table.Returns:
unit classstring
-
unit:is_cavalry()
-
Returns whether the unit is cavalry.
Returns:
is cavalryboolean
-
unit:is_infantry()
-
Returns whether the unit is infantry.
Returns:
is infantryboolean
-
unit:is_artillery()
-
Returns whether the unit is artillery.
Returns:
is artilleryboolean
-
unit:is_limbered_artillery()
-
Returns whether the unit is artillery in a limbered state (i.e. it can move but not fire).
Returns:
is limbered artilleryboolean
-
unit:is_commanding_unit()
-
Returns whether this is the commanding unit of the army.
Returns:
is commandingboolean
-
unit:position()
-
Returns the centre position of the main squad of the unit.
Returns:
positionbattle_vector
-
unit:ordered_position()
-
Returns the position to which the unit has been ordered to move to. Note that if the unit is attacking, as opposed to moving, then this will return the current position of the unit.
Returns:
ordered positionbattle_vector
-
unit:can_reach_position(
positionbattle_vector
)
-
Returns whether the unit can reach the supplied position.
Parameters:
1
position
Returns:
can reachboolean
-
unit:bearing()
-
Returns the current orientation of the main squad of the unit in degrees.
Returns:
orientationnumber
-
unit:ordered_bearing()
-
Returns the orientation of the position to which the unit has been ordered in degrees.
Returns:
orientationnumber
-
unit:ordered_width()
-
Returns the width of the position to which the unit has been ordered in metres.
Returns:
widthnumber
-
unit:position_of_officer()
-
Returns the position of the commanding officer of the unit.
Returns:
officer positionbattle_vector
-
unit:unit_in_range(
target unitbattle_unit
)
-
Returns whether the supplied unit is in missile range of this unit.
Parameters:
1
target unit
Returns:
is in rangeboolean
-
unit:unit_distance(
target unitbattle_unit
)
-
Returns the shortest distance in metres between this unit and a supplied unit, taking into account their bounding box rather than just working out the centre-to-centre distance.
Parameters:
1
target unit
Returns:
shortest distancenumber
-
unit:is_moving()
-
Returns whether the unit is moving.
Returns:
is movingboolean
-
unit:is_moving_fast()
-
Returns whether the unit is moving fast (running/charging).
Returns:
is moving fastboolean
-
unit:is_idle()
-
Returns whether the unit is currently idle.
Returns:
is idleboolean
-
unit:is_leaving_battle()
-
Returns whether the unit is currently leaving the battlefield.
Returns:
is leaving battleboolean
-
unit:is_valid_target()
-
Returns whether this unit is currently a valid target for enemies to attack. A unit might be invalid if they are entering or leaving the battlefield, hidden, or dead.
Returns:
is valid targetboolean
-
unit:is_currently_garrisoned()
-
Returns whether the unit is currently garrisoned in a building.
Returns:
is garrisonedboolean
-
unit:is_currently_flying()
-
Returns whether the unit can fly and is currently flying.
Returns:
is flyingboolean
-
unit:can_fly()
-
Returns whether the unit can fly.
Returns:
can flyboolean
-
unit:has_ships()
-
Returns whether the unit is currently on a ship.
Returns:
is on a shipboolean
-
unit:is_dismounted_ships()
-
Returns whether the unit has a ship but is not currently on it.
Returns:
dismounted from shipboolean
-
unit:is_hidden()
-
Returns whether the unit is currently hidden in tall grass/trees. Note that this is a separate test from the unit being obscured from the enemy behind hills or other terrain features.
Returns:
is hiddenboolean
-
unit:is_visible_to_alliance(
alliancebattle_alliance
)
-
Returns whether the unit is visible to a supplied alliance. This returns the last calculated result of the line-of-sight test that determines whether a unit is obscured from its enemy by terrain features.
Parameters:
1
alliance
Returns:
is visible to allianceboolean
-
unit:is_under_missile_attack()
-
Returns whether the unit is currently taking fire.
Returns:
taking fireboolean
-
unit:is_in_melee()
-
Returns whether the unit is currently in melee combat.
Returns:
in meleeboolean
-
unit:number_of_enemies_killed()
-
Returns the number of enemy combatants this unit has killed in this battle.
Returns:
number killednumber
-
unit:initial_number_of_men()
-
Returns the number of soldiers the unit started the battle with.
Returns:
initial unit sizenumber
-
unit:number_of_men_alive()
-
Returns the number of soldiers currently alive in the unit.
Returns:
current unit sizenumber
-
unit:unary_of_men_alive()
-
Returns the number of soldiers currently alive in the unit, expressed as a unary fraction of the the initial size of the unit. A returned value of 0.75 would indicate that 25% of the soldiers in the unit that started the battle have died.
Returns:
unary proportionnumber
-
unit:kill_number_of_men(
soldiers to killnumber
, [
hide bodiesboolean
])
-
Kills a specified number of soldiers in the unit. The optional second argument allows their bodies to be hidden - this is useful if it's desired that the unit 'disappear'.
Parameters:
1
soldiers to kill
2
optional, default value=false
hide bodies
Returns:
nil
-
unit:unary_hitpoints()
-
Returns the hitpoint strength of all soldiers in the unit, expressed as a unary fraction of the the initial hitpoint strength of the unit. A returned value of 0.75 would indicate that the unit has lost 25% of the hitpoints it started the battle with.
Returns:
unary proportionnumber
-
unit:is_wavering()
-
Returns whether the morale of the unit is currently wavering.
Returns:
is waveringboolean
-
unit:is_routing()
-
Returns whether the unit is currently routing.
Returns:
is routingboolean
-
unit:is_rampaging()
-
Returns whether the unit is currently rampaging.
Returns:
is rampagingboolean
-
unit:is_shattered()
-
Returns whether the unit is currently shattered (routing without possibility of return).
Returns:
is shatteredboolean
-
unit:is_left_flank_threatened()
-
Returns whether the left flank of the unit is currently threatened by the enemy.
Returns:
left flank threatenedboolean
-
unit:is_right_flank_threatened()
-
Returns whether the right flank of the unit is currently threatened by the enemy.
Returns:
left right threatenedboolean
-
unit:is_rear_flank_threatened()
-
Returns whether the rear of the unit is currently threatened by the enemy.
Returns:
rear threatenedboolean
-
unit:fatigue_state()
-
Returns the fatigue state of the unit as a string. Possible values are
threshold_fresh
,threshold_active
,threshold_winded
,threshold_tired
,threshold_very_tired
andthreshold_exhausted
.Returns:
fatigue statestring
-
unit:ammo_left()
-
Returns the remaining ammunition count of this unit.
Returns:
current ammunitionnumber
-
unit:starting_ammo()
-
Returns the amount of ammunition the unit started the battle with.
Returns:
starting ammunitionnumber
-
unit:set_current_ammo_unary(
unary ammunition levelnumber
)
-
Sets the current ammunition level of the unit to the supplied unary value. Setting a value of 0 would mean the unit would have no ammunition, whereas setting a value of 1 would mean the unit would have the amount of ammunition they started the battle with. It is permitted to set a value greater than 1, but the unit will appear to have full ammunition on the UI until its ammunition level drops back below 100%.
Parameters:
1
unary ammunition level
Returns:
nil
-
unit:missile_range()
-
Returns the missile range of this unit in metres.
Returns:
missile rangenumber
-
unit:can_perform_special_ability(
special ability keystring
)
-
Returns whether the unit can perform the specified special ability. Valid special abilities are defined in the
special_abilities
table.Parameters:
1
special ability key
Returns:
can perform abilityboolean
-
unit:can_use_magic()
-
Returns whether the unit can use any special abilities that cost magic.
Returns:
can use magicboolean
-
unit:can_use_behaviour(
behaviourstring
)
-
Returns whether the unit can use a particular behaviour this battle. Valid behaviours are currently as follows:
defend
,drop_siege_equipment
,abandon_artillery_engines
,change_formation_spacing
,dismantle_artillery_piece
,dismount
,fire_at_will
,skirmish
,release_animals
,unlimber
,board_ship
, andformed_attack
.Parameters:
1
behaviour
Returns:
behaviour can be usedboolean
-
unit:is_behaviour_active(
behaviourstring
)
-
Returns whether a particular behaviour is currently active. Valid behaviours are currently as follows:
defend
,drop_siege_equipment
,abandon_artillery_engines
,change_formation_spacing
,dismantle_artillery_piece
,dismount
,fire_at_will
,skirmish
,release_animals
,unlimber
,board_ship
, andformed_attack
.Parameters:
1
behaviour
Returns:
behaviour is activeboolean
Unit attributes define certain intrinsic properties of a unit in battle. Attributes are generally assigned to a unit type in the game database, but may be enabled or disabled per-unit in script using the unit:set_stat_attribute
function. Valid attribute keys are shown in the following list:
Attribute Key | Description |
---|---|
all_ranks_fire | All ranks can fire if using a weapon that only allows one rank. |
berserk | This unit will occasionally go berserk. |
cant_run | This unit cannot run - generally for limbered artillery. |
campaign_stealth | This unit is able to move around the campaign map without being seen. |
charge_reflector | Charge reflection vs all. |
charge_reflector_vs_large | Charge reflection vs large units only. |
causes_fear | This unit frightens all enemy units, reducing their morale when nearby. It is also immune to fear. |
causes_terror | The unit can cause terror, making enemies rout for a short time but without morale penalties. It is also immune to terror and fear. |
commander_hunter | This unit is changed in various ways to make them better at killing commanders. |
disciplined | This unit doesn't suffer morale penalty from general's death. |
encourages | This unit provides a morale bonus to nearby allies. |
fatigue_res | Fatigue doesn't have as much of an effect. |
fatigue_immune | Fatigue doesn't an effect. |
fire_while_moving | This infantry unit can fire while moving. |
formed_attack | This unit will try to stay in formation when in melee. |
guerrilla_deploy | Can deploy outside deployment zone (vanguard deployment). |
hide_forest | Can hide in forest. |
hide_grass | Can hide in grass. |
hide_scrub_and_forest | Can hide in scrubland. |
hide_building | Can hide in buildings. |
ignore_ground_move_mod | Doesn't get slowed down by terrain. |
strider | Strider attribute. |
immune_to_psychology | The unit is immune to psychological attacks (fear and terror). |
impetuous | This unit is impetuous and can be hard to withdraw from battle. |
mighty_knockback | This unit causes extra damage when they knock back the enemy. |
mounted_fire | The unit can fire while on a mount. |
mounted_fire_move | The unit can fire while on a moving mount. |
mounted_fire_parthian | The unit can fire while on a moving mount in a Parthian fashion. |
rampage | This unit may rampage if put under pressure. |
resist_cold | This unit doesn't take extra casualties from cold climates in campaign. |
resist_heat | This unit doesn't take extra casualties from hot climates in campaign. |
run_amok | This unit may run amok if put under pressure. |
scare_horse | This unit scares horses. |
scare_immune | This unit is immune to fear effects. |
snipe | The unit remains hidden while firing. |
stalk | This unit can remain hidden while moving. |
unbreakable | This unit's morale cannot be broken. |
undead | The unit is undead (does not rout, immune to terror, receives crumble damage when morale is low). |
unspottable | If this unit is able to hide in its current location, it will not be spotted by enemies until they are very close. |
expendable | A normal (non-expendable) unit's morale does not suffer from witnessing an expendable unit routing. |
construct | The unit is a statue. |
wallbreaker | The unit can attack walls physically. |
ignore_trees | The unit ignores trees. |
moulder_monster | The unit is a moulder monster that can be targeted by Throt's abilities. |
ogre_charge | The unit has ogre charge. |
Not all attributes will be supported by each game.
-
unit:set_stat_attribute(
attribute keystring
,
enable/disableboolean
)
-
Enables or disables a global attribute on the unit. Attribute keys are listed above, in the
Unit Attributes
section description.Parameters:
1
attribute key
2
enable/disable
Returns:
nil
-
unit:trigger_sound_charge()
-
Compels the unit to make a charge sound effect.
Returns:
nil
-
unit:trigger_sound_taunt()
-
Compels the unit to make a taunt sound effect.
Returns:
nil
-
unit:play_anim_for_captain(
animation keystring
,
loop animationboolean
)
-
Compels the unit leader to play an animation.
Parameters:
1
animation key
2
loop animation
Returns:
nil
-
unit:deploy_reinforcement(
can deployboolean
)
-
Prevents or allows the unit to deploy as a reinforcement. If set to
false
, and the unit has not yet entered the battlefield, then the unit will not be able to deploy until the function is called again with an argument oftrue
.
Calling this command has no effect if the unit is not part of a reinforcement army.Parameters:
1
can deploy
Returns:
nil
-
unit:select_in_ui()
-
Selects the unit in the user interface.
Returns:
nil
-
unit:mark_as_ally(
mark as allyboolean
)
-
Makes a player unit look like it is allied, rather than part of the controlled army. This is useful for specific scripted situations where it's intended to grant allied units to player control.
Call this function withtrue
as a single argument to mark the controlled unit as an ally, and then again supplyingfalse
to mark the unit as part of the player's army again.Parameters:
1
mark as ally
Returns:
nil