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 Campaign | 
										 | 
								
| Loaded in Battle | 
										 | 
								
| Loaded in Frontend | 
										 | 
								
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 lunar_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.
- 
							
battle_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_junctionstable 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
 
- 
							
battle_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
 
- 
							
battle_unit:type() - 
							Returns the unit type of the unit, from the 
land_unitstable.Returns:
unit typestring
 
- 
							
battle_unit:army() - 
							Returns the army object to which the unit currently belongs.
							
Returns:
parent armybattle_army
 
- 
							
battle_unit:alliance_index() - 
							Returns the index number of the alliance the unit belongs to, allowing its 
battle_allianceto be determined or looked up from abattle_alliancesobject.Returns:
alliance indexnumber
 
- 
							
battle_unit:army_index() - 
							Returns the index number of the army the unit belongs to, allowing its 
battle_armyto be determined or looked up from abattle_armiesobject (which itself can be determined from the value returned bybattle_unit:alliance_index).Returns:
army indexnumber
 
- 
							
battle_unit:unit_class() - 
							Returns the class of the unit, from the 
land_unitstable.Returns:
unit classstring
 
- 
							
battle_unit:is_commanding_unit() - 
							Returns whether this is the commanding unit of the army.
							
Returns:
is commandingboolean
 
- 
							
battle_unit:is_infantry() - 
							Returns whether this is a unit of infantry.
							
Returns:
is infantryboolean
 
- 
							
battle_unit:is_pikemen() - 
							Returns whether this is a unit of pikemen.
							
Returns:
is pikemenboolean
 
- 
							
battle_unit:is_swordsmen() - 
							Returns whether this is a unit of swordsmen.
							
Returns:
is swordsmenboolean
 
- 
							
battle_unit:is_spearmen() - 
							Returns whether this is a unit of spearmen.
							
Returns:
is spearmenboolean
 
- 
							
battle_unit:is_axemen() - 
							Returns whether this is a unit of axemen.
							
Returns:
is axemenboolean
 
- 
							
battle_unit:is_infantry_ranged() - 
							Returns whether this is a unit of ranged infantry.
							
Returns:
is ranged infantryboolean
 
- 
							
battle_unit:is_anti_cavalry_infantry() - 
							Returns whether this is a unit of anti-cavalry infantry.
							
Returns:
is anti-cavalry infantryboolean
 
- 
							
battle_unit:is_cavalry() - 
							Returns whether this is a unit of cavalry.
							
Returns:
is cavalryboolean
 
- 
							
battle_unit:is_lancers() - 
							Returns whether this is a unit of lancers.
							
Returns:
is lancersboolean
 
- 
							
battle_unit:is_dismounted_cavalry() - 
							Returns whether this is a dismounted cavalry unit.
							
Returns:
is dismounted cavalryboolean
 
- 
							
battle_unit:is_chariot() - 
							Returns whether this is a chariot unit.
							
Returns:
is chariotboolean
 
- 
							
battle_unit:is_camels() - 
							Returns whether this unit fights on camels.
							
Returns:
is camelsboolean
 
- 
							
battle_unit:is_elephants() - 
							Returns whether this is a unit of elephants.
							
Returns:
is elephantsboolean
 
- 
							
battle_unit:is_war_beasts() - 
							Returns whether this is a unit of war beasts.
							
Returns:
is war beastsboolean
 
- 
							
battle_unit:is_artillery() - 
							Returns whether this is an artillery unit.
							
Returns:
is artilleryboolean
 
- 
							
battle_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
 
- 
							
battle_unit:is_unlimbered_artillery() - 
							Returns whether the unit is artillery in an unlimbered state (it can fire, but not move).
							
Returns:
is unlimbered artilleryboolean
 
- 
							
battle_unit:is_fixed_artillery() - 
							Returns whether the unit is fixed artillery. Fixed artillery can never move.
							
Returns:
is fixed artilleryboolean
 
- 
							
battle_unit:is_war_machine() - 
							Returns whether the unit is a war machine.
							
Returns:
is war machineboolean
 
- 
							
battle_unit:can_fly() - 
							Returns whether the unit can fly.
							
Returns:
can flyboolean
 
- 
							
battle_unit:is_currently_flying() - 
							Returns whether the unit can fly and is currently flying.
							
Returns:
is flyingboolean
 
- 
							
battle_unit:has_ships() - 
							Returns whether the unit is currently on a ship.
							
Returns:
is on a shipboolean
 
- 
							
battle_unit:is_dismounted_ships() - 
							Returns whether the unit has a ship but is not currently on it.
							
Returns:
dismounted from shipboolean
 
- 
							
battle_unit:position() - 
							Returns the centre position of the main squad of the unit.
							
Returns:
positionbattle_vector
 
- 
							
battle_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
 
- 
							
battle_unit:can_reach_position(positionbattle_vector) - 
							Returns whether the unit can reach the supplied position.
							
Parameters:
1
position
Returns:
can reachboolean
 
- 
							
battle_unit:bearing() - 
							Returns the current orientation of the main squad of the unit in degrees.
							
Returns:
orientationnumber
 
- 
							
battle_unit:ordered_bearing() - 
							Returns the orientation of the position to which the unit has been ordered in degrees.
							
Returns:
orientationnumber
 
- 
							
battle_unit:ordered_width() - 
							Returns the width of the position to which the unit has been ordered in metres.
							
Returns:
widthnumber
 
- 
							
battle_unit:position_of_officer() - 
							Returns the position of the commanding officer of the unit.
							
Returns:
officer positionbattle_vector
 
- 
							
battle_unit:is_ai_controlled() - 
							Returns whether the supplied unit is currently under AI control.
							
Returns:
controlled by AIboolean
 
- 
							
battle_unit:is_script_controlled() - 
							Returns whether the supplied unit is currently under script control.
							
Returns:
controlled by scriptboolean
 
- 
							
battle_unit:is_player_controlled() - 
							Returns whether the supplied unit is currently under script control.
							
Returns:
controlled by scriptboolean
 
- 
							
battle_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
 
- 
							
battle_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
 
- 
							
battle_unit:strategic_value() - 
							Returns an arbitrary numeric indicator of this unit's current strength. Better units, or units in a better current condition, will return higher values that less capable units.
							
Returns:
strategic valuenumber
 
- 
							
battle_unit:is_moving() - 
							Returns whether the unit is moving.
							
Returns:
is movingboolean
 
- 
							
battle_unit:is_moving_fast() - 
							Returns whether the unit is moving fast (running/charging).
							
Returns:
is moving fastboolean
 
- 
							
battle_unit:slow_speed() - 
							Returns the unit's slow/walking movement speed in m/s.
							
Returns:
movement speednumber
 
- 
							
battle_unit:fast_speed() - 
							Returns the unit's fast/running movement speed in m/s.
							
Returns:
movement speednumber
 
- 
							
battle_unit:is_idle() - 
							Returns whether the unit is currently idle.
							
Returns:
is idleboolean
 
- 
							
battle_unit:is_leaving_battle() - 
							Returns whether the unit is currently leaving the battlefield.
							
Returns:
is leaving battleboolean
 
- 
							
battle_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
 
- 
							
battle_unit:is_controllable() - 
							Returns whether the unit is currently controllable.
							
Returns:
is controllableboolean
 
- 
							
battle_unit:is_currently_garrisoned() - 
							Returns whether the unit is currently garrisoned in a building.
							
Returns:
is garrisonedboolean
 
- 
							
battle_unit:is_in_group() - 
							Returns whether the unit is currently in a player-created unit group.
							
Returns:
is in groupboolean
 
- 
							
battle_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
 
- 
							
battle_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
 
- 
							
battle_unit:is_under_missile_attack() - 
							Returns whether the unit is currently taking fire.
							
Returns:
taking fireboolean
 
- 
							
battle_unit:is_in_melee() - 
							Returns whether the unit is currently in melee combat.
							
Returns:
in meleeboolean
 
- 
							
battle_unit:is_firing_missiles() - 
							Returns whether the unit is currently firing missiles.
							
Returns:
is_firing_missilesboolean
 
- 
							
battle_unit:number_of_enemies_killed() - 
							Returns the number of enemy combatants this unit has killed in this battle.
							
Returns:
number killednumber
 
- 
							
battle_unit:initial_number_of_men() - 
							Returns the number of soldiers the unit started the battle with.
							
Returns:
initial unit sizenumber
 
- 
							
battle_unit:number_of_men_alive() - 
							Returns the number of soldiers currently alive in the unit.
							
Returns:
current unit sizenumber
 
- 
							
battle_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
 
- 
							
battle_unit:kill_number_of_men(soldiers to killnumber, [hide bodiesboolean], [ignore_pauseboolean]) - 
							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
3
optional, default value=true
ignore_pause
Returns:
nil
 
- 
							
battle_unit:reduce_hitpoints_unary(unary hitpointsnumber, [hide bodiesboolean]) - 
							Reduces hitpoints of 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
percentage of full hit points of unit to reduce
2
optional, default value=false
hide bodies
Returns:
nil
 
- 
							
battle_unit:heal_hitpoints_unary(unary hitpointsnumber, [allow resurrectionboolean]) - 
							Heals the unit to the specified unary proportion of full strength i.e. 1.0 = full strength. If the allow-resurrection flag is set then dead entities may be resurrected, otherwise the function heals where it can. The function does not work in all circumstances, healing animals + handlers are not currently supported.
							
Parameters:
1
percentage of full hit points to heal to.
2
optional, default value=true
Allow resurrection of dead entities.
Returns:
nil
 
- 
							
battle_unit:respawn() - 
							Respawns the unit in a supplied position on the battlefield.
							
Returns:
nil
 
- 
							
battle_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
 
- 
							
battle_unit:is_wavering() - 
							Returns whether the morale of the unit is currently wavering.
							
Returns:
is waveringboolean
 
- 
							
battle_unit:is_routing() - 
							Returns whether the unit is currently routing.
							
Returns:
is routingboolean
 
- 
							
battle_unit:is_rampaging() - 
							Returns whether the unit is currently rampaging.
							
Returns:
is rampagingboolean
 
- 
							
battle_unit:is_shattered() - 
							Returns whether the unit is currently shattered (routing without possibility of return).
							
Returns:
is shatteredboolean
 
- 
							
battle_unit:is_dueling() - 
							Returns whether the unit is currently involved in a duel.
							
Returns:
is duellingboolean
 
- 
							
battle_unit:is_left_flank_threatened() - 
							Returns whether the left flank of the unit is currently threatened by the enemy.
							
Returns:
left flank threatenedboolean
 
- 
							
battle_unit:is_right_flank_threatened() - 
							Returns whether the right flank of the unit is currently threatened by the enemy.
							
Returns:
left right threatenedboolean
 
- 
							
battle_unit:is_rear_flank_threatened() - 
							Returns whether the rear of the unit is currently threatened by the enemy.
							
Returns:
rear threatenedboolean
 
- 
							
battle_unit:left_flank_threat() - 
							Returns the 
battle_unitthreatening this unit's left flank. If no such unit is threatening then no value is returned.Returns:
threatening unitbattle_unit
 
- 
							
battle_unit:right_flank_threat() - 
							Returns the 
battle_unitthreatening this unit's right flank. If no such unit is threatening then no value is returned.Returns:
threatening unitbattle_unit
 
- 
							
battle_unit:rear_threat() - 
							Returns the 
battle_unitthreatening this unit's rear. If no such unit is threatening then no value is returned.Returns:
threatening unitbattle_unit
 
- 
							
battle_unit:current_target() - 
							Returns the 
battle_unitthis unit is targeting. If no unit is being targeted then no value is returned.Returns:
target unitbattle_unit
 
- 
							
battle_unit:is_on_top_of_platform() - 
							Returns whether the unit is currently on the top of a platform, such as a wall.
							
Returns:
is on platformboolean
 
- 
							
battle_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_tiredandthreshold_exhausted.Returns:
fatigue statestring
 
- 
							
battle_unit:ammo_left() - 
							Returns the remaining ammunition count of this unit.
							
Returns:
current ammunitionnumber
 
- 
							
battle_unit:starting_ammo() - 
							Returns the amount of ammunition the unit started the battle with.
							
Returns:
starting ammunitionnumber
 
- 
							
battle_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
 
- 
							
battle_unit:missile_range() - 
							Returns the missile range of this unit in metres.
							
Returns:
missile rangenumber
 
- 
							
battle_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_abilitiestable.Parameters:
1
special ability key
Returns:
can perform abilityboolean
 
- 
							
battle_unit:num_special_abilities() - 
							Returns the number of special abilities this unit has.
							
Returns:
number of special abilitiesnumber
 
- 
							
battle_unit:rage_unary() - 
							Returns the current rage level of this unit as a unary value.
							
Returns:
rage levelnumber
 
- 
							
battle_unit:aristeia_unary() - 
							Returns the current aristeia level of this unit as a unary value.
							
Returns:
aristeia levelnumber
 
- 
							
battle_unit:can_guerrilla_deploy() - 
							Returns whether this unit is a vanguard unit that can deploy outside of the deployment area before the battle begins.
							
Returns:
can guerrilla deployboolean
 
- 
							
battle_unit:can_use_magic() - 
							Returns whether the unit can use any special abilities that cost magic.
							
Returns:
can use magicboolean
 
- 
							
battle_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
 
- 
							
battle_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
 
- 
							
battle_unit:trigger_sound_charge() - 
							Compels the unit to make a charge sound effect.
							
Returns:
nil
 
- 
							
battle_unit:trigger_sound_taunt() - 
							Compels the unit to make a taunt sound effect.
							
Returns:
nil
 
- 
							
battle_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
 
- 
							
battle_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
 
- 
							
battle_unit:select_in_ui() - 
							Selects the unit in the user interface.
							
Returns:
nil
 
- 
							
battle_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 withtrueas a single argument to mark the controlled unit as an ally, and then again supplyingfalseto mark the unit as part of the player's army again.Parameters:
1
mark as ally
Returns:
nil
 
- 
							
battle_unit:highlight([highlightboolean]) - 
							Activates or deactivates a highlight on the unit.
							
Parameters:
1
optional, default value=true
highlight
Returns:
nil