Script Unit
Scriptunits (or sunits for short) are an encapsulation of the tendency of battle scripts to want to set up handles to units, along with a single unitcontroller that controls that unit. It provides a shorthand method of setting those handles up, along with a raft of additional functionality that can only be provided when a unit and unitcontroller are grouped together (e.g. "walk forward x metres").
Scriptunits provide much unique functionality (documented below) but also access to the unit object and unitcontroller object they contain. These can be accessed at the unit (<sunit_obj>.unit) and uc (<sunit_obj>.uc) elements.
Scriptunits are best grouped together in a specialised container, the script_units object. This provides yet further functionality that can be offered when a collection of script units are assembled together.
| Loaded in Battle |
|
-
script_unit:new(armyarmy, objectreference) -
Creates a new script unit.
Parameters:
1
armyArmy the target unit is a member of.
2
objectReference for the unit. Can be an integer number representing the unit's index within the army ('1' is generally the general, for example) or a string name. If a string name, this must match the script name given to the unit in either the battle xml or in the battle_set_piece_armies_units_junctions table.
Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 49
Variables in this section:
| unit | unit | unit Unit object representing the subject unit. |
| uc | unitcontroller | Unitcontroller object that controls the unit. |
These variables are also listed in the global variables list for this class or module.
-
script_unit:goto_start_location([booleanshould run]) -
Instructs the scriptunit to move to its start location.
Parameters:
1
booleanoptional, default value=false
should run
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 194
-
script_unit:teleport_to_start_location() -
Teleports the scriptunit to the position/bearing/width it started the battle at.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 202
-
script_unit:teleport_to_start_location_offset(x offset
number,z offset
number,bearing override
[number],release control
[boolean]
) -
Teleports the scriptunit to an offset from the position/bearing/width it started the battle at. This offset will be calculated from the scriptunit's start bearing, so an offset of [0, -10] will teleport it 10m behind its starting position. The function also optionally takes an override bearing to alter the position calculation, as well as a flag to release the unit to script control afterwards.
Parameters:
1
numberx offset in m
2
numberz offset in m
3
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
4
booleanoptional, default value=false
Release control after teleporting. Set this to true if you want the player (or AI, if it's not player-controlled) to be able to control this unit immediately after teleporting.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 209
-
script_unit:goto_start_location_offset(x offset
number,z offset
number,should run
[boolean],bearing override
[number],release control
[boolean]
) -
Instructs the scriptunit to move to an offset from the position/bearing/width it started the battle at. This offset will be calculated from the scriptunit's start bearing, so an offset of [0, -10] will send it 10m behind its starting position. The function also optionally takes a flag to instruct the unit to run, an override bearing to alter the position calculation, as well as a flag to release the unit to script control afterwards.
Parameters:
1
numberx offset in m
2
numberz offset in m
3
booleanoptional, default value=false
Set to true to instruct the unit to move quickly to its destination.
4
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
5
booleanoptional, default value=false
Release control after the movement order is issued. Set this to true if you want the player (or AI, if it's not player-controlled) to be able to control this unit immediately after moving.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 241
Variables in this section:
| start_position | vector | Vector position the unit started the battle at. |
| start_bearing | number | Bearing in degrees the unit started the battle at. |
| start_width | number | Width in m of the unit at the start of the battle. |
These variables are also listed in the global variables list for this class or module.
-
script_unit:position_offset(numberx offset, numbery offset, numberz offset, [numberbearing override]) -
Returns the position of the scriptunit, offset from its current position. This offset will be calculated from the scriptunit's current bearing, so an offset of [0, 0, -10] will report a position 10m behind its current position. An override bearing to alter the position calculation may optionally be supplied.
Parameters:
1
numberx offset in m
2
numbery offset (height) in m
3
numberz offset in m
4
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
Returns:
offset positionbattle_vector
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 286
-
script_unit:goto_location_offset(x offset
number,z offset
number,should run
[boolean],bearing override
[number],release control
[boolean]
) -
Instructs the scriptunit to move to an offset from its current position. This offset will be calculated from the scriptunit's current bearing, so an offset of [0, -10] will send it 10m behind its current position. The function also optionally takes a flag to instruct the unit to run, an override bearing to alter the position calculation, as well as a flag to release the unit to script control afterwards.
Parameters:
1
numberx offset in m
2
numberz offset in m
3
booleanoptional, default value=false
Set to true to instruct the unit to move quickly to its destination.
4
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
5
booleanoptional, default value=false
Release control after the movement order is issued. Set this to true if you want the player (or AI, if it's not player-controlled) to be able to control this unit immediately after moving.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 323
-
script_unit:teleport_to_location_offset(x offset
number,z offset
number,bearing override
[number],release control
[boolean]
) -
Instructs the scriptunit to teleport to an offset from its current position. This offset will be calculated from the scriptunit's current bearing, so an offset of [0, -10] will teleport it 10m behind its current position. The function also optionally takes an override bearing to alter the position calculation, as well as a flag to release the unit to script control afterwards.
Parameters:
1
numberx offset in m
2
numberz offset in m
3
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
4
booleanoptional, default value=false
Release control after the teleport order is issued. Set this to true if you want the player (or AI, if it's not player-controlled) to be able to control this unit immediately after teleporting.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 360
-
script_unit:goto_location_offset_when_deployed(x offset
number,z offset
number,should run
[boolean],bearing override
[number],release control
[boolean]
) -
Instructs the scriptunit to goto an offset from its location when it deploys. This offset will be calculated from the scriptunit's bearing at the time it finds itself on the battlefield, so an offset of [0, -5] will move it 50m forward. The function also optionally takes a flag instructing it to run, an override bearing to alter the position calculation, as well as a flag to release the unit to script control afterwards.
Parameters:
1
numberx offset in m
2
numberz offset in m
3
booleanoptional, default value=false
Set to true to instruct the unit to move quickly to its destination.
4
numberoptional, default value=nil
Bearing override in degrees. Supply a value here to override the bearing used to make the offset calculation.
5
booleanoptional, default value=false
Release control after the movement order is issued. Set this to true if you want the player (or AI, if it's not player-controlled) to be able to control this unit immediately after moving.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 398
-
script_unit:stop_goto_location_offset_when_deployed() -
Stops a running
script_unit:goto_location_offset_when_deployedlistener on the current unit. Call this after callingscript_unit:goto_location_offset_when_deployedto stop the listener for any reason.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 419
-
script_unit:turn_to_face(vectorposition) -
Instructs the scriptunit to turn to face a position vector.
Parameters:
1
vectorposition
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 426
-
script_unit:teleport_to_location(vectorposition, numberbearing, numberwidth) -
Instructs the scriptunit to teleport to a location.
Parameters:
1
vectorPosition to teleport to.
2
numberBearing to face at the target position in degrees.
3
numberWidth in m of formation at target position.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 462
-
script_unit:cache_location() -
Caches the units current position, bearing and width.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 486
-
script_unit:get_cached_position() -
Returns the vector position of the unit last time it was cached with
script_unit:cache_location.Returns:
vectorposition (or nil, if never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 495
-
script_unit:get_cached_bearing() -
Returns the bearing of the unit in degrees last time it was cached with
script_unit:cache_location.Returns:
numberbearing (or nil, if never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 503
-
script_unit:get_cached_width() -
Returns the width of the unit in m last time it was cached with
script_unit:cache_location.Returns:
numberwidth (or nil, if never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 511
-
script_unit:goto_cached_location([booleanshould run]) -
Instructs the scriptunit to move to the location the unit occupied the last time it was cached with
script_unit:cache_location.Parameters:
1
booleanoptional, default value=false
Instructs the unit to move fast.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 519
-
script_unit:teleport_to_cached_location() -
Teleports the scriptunit to the location the unit occupied the last time it was cached with
script_unit:cache_location.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 530
-
script_unit:has_moved([vectorposition override], [numberthreshold distance]) -
Returns true if the scriptunit has moved from the last cached position, or an optional supplied position. The movement threshold can also be overriden (by default it's 1m).
Parameters:
1
vectoroptional, default value=nil
position override
2
numberoptional, default value=1
threshold distance
Returns:
booleanhas moved
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 539
-
script_unit:cache_destination() -
Caches the current destination position of the scriptunit. Note that the if the subject unit is attacking rather than moving to a position it will have no valid destination. The function also caches whether the unit is running.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 575
-
script_unit:cache_destination_and_halt() -
Caches the current destination position of the scriptunit with
script_unit:cache_destination, and then orders the unit to halt.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 589
-
script_unit:get_cached_destination_position() -
Returns the vector destination last cached by
script_unit:cache_destination.Returns:
vectordestination position (or nil if destination never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 597
-
script_unit:get_cached_destination_bearing() -
Returns the ordered bearing in degrees that was last cached by
script_unit:cache_destination.Returns:
numberbearing in degrees (or nil if destination never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 605
-
script_unit:get_cached_destination_width() -
Returns the ordered width in m that was last cached by
script_unit:cache_destination.Returns:
numberwidth in m (or nil if destination never cached).
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 613
-
script_unit:goto_cached_destination([booleanshould release]) -
Instructs the unit to move to the location last cached by
script_unit:cache_destination. If the unit was moving quickly when its destination was last cached, than it will resume moving quickly when this function is called.Parameters:
1
booleanoptional, default value=false
Set to true to release script control of the unit after calling this function. Set this if it's desirable for the player (or AI, if it's not a player-controlled unit) to be able to control this unit after the order has been issued.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 621
-
script_unit:cache_health() -
Caches the proportion of the scriptunit still alive, so that it can be queried later with
script_unit:has_taken_casualties.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 651
-
script_unit:has_taken_casualties([numbertolerance]) -
Compares the scriptunits current casualties with that when it was previously cached with
script_unit:cache_health, and returns true if they're different.Parameters:
1
numberoptional, default value=0
Tolerance value. The casualties values is expressed internally as a unary value (between 0 and 1), so this should be expressed in a similar manner. Therefore a tolerance of 0.1 would allow for 10% casualties (of the original number of soldiers in the unit) more than when previously cached before returning true.
Returns:
booleanhas taken casualties
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 662
-
script_unit:is_under_attack() -
Returns true if the unit is under missile attack, in melee, or has casualties since the last time
is_under_attackwas called. Designed to be called repeatedly.Returns:
booleanis under attack
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 692
-
script_unit:is_in_melee() -
Returns true if the unit is in melee. This is an unembellished wrapper for an underlying code function.
Returns:
booleanis in melee
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 702
Many of the functions in this section just wrap the function provided by code on the unitcontroller interface with no embellishments - by wrapping it here, however, access to it can be provided at the script_units level, so a scriptunits collection can be told to stop with one call to script_unit:halt, for example.
-
script_unit:halt() -
Instructs the scriptunit to halt. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 749
-
script_unit:celebrate() -
Instructs the scriptunit to celebrate. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 756
-
script_unit:taunt() -
Instructs the scriptunit to taunt. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 762
-
script_unit:play_sound_charge() -
Instructs the scriptunit to play a charge sound. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 769
-
script_unit:play_sound_taunt() -
Instructs the scriptunit to play a taunt sound. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 776
-
script_unit:deploy_reinforcement([booleanshould deploy]) -
Instructs the scriptunit to deploy or not. If supplied with
falseas a single argument, the instruction prevents the unit from deploying until the function is called again withtrue.Parameters:
1
booleanoptional, default value=true
should deploy
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 793
-
script_unit:change_behaviour_active(stringbehaviour name, booleanactivate) -
Sets a supplied behaviour active on the unit or not. This is an unembellished wrapper for an underlying code function. This is the main mechanism for turning on/turning off certain unit behaviours such as fire at will, skirmish etc. Current list of behaviours includes, but may not be limited to the following:
"defend", "dismount", "fire_at_will", "skirmish", "change_formation_spacing", "drop_siege_equipment", "release_animals", "disembark_from_ship", "board_ship"Parameters:
1
stringbehaviour name
2
booleanactivate
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 805
-
script_unit:withdraw(booleanshould run) -
Instructs the specified unit to withdraw. This is an unembellished wrapper for an underlying code function.
Parameters:
1
booleanshould run
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 818
-
script_unit:set_melee_mode([booleanactivate], [booleanrelease control]) -
Activates or deactivates melee mode.
Parameters:
1
booleanoptional, default value=true
Should activate melee mode.
2
booleanoptional, default value=false
Release script control after setting melee mode. Set this to true if the unit is under player control.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 826
-
script_unit:set_stat_attribute(attribute keystring,enable/disableboolean) -
Enables or disables a unit attribute for the unit. Valid attribute keys are listed in the
Unit Attributessection of this documentation.Parameters:
1
attribute key
2
enable/disable
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 838
-
script_unit:set_enabled([booleanenabled]) -
Sets the unit to be enabled and visible if
trueis supplied as an argument, or disabled and invisible iffalseis supplied.Parameters:
1
booleanoptional, default value=true
enabled
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 865
-
script_unit:set_always_visible([booleanalways visible]) -
Sets the unit to be always visible, according to the rules of the terrain visibility system.
Parameters:
1
booleanoptional, default value=true
always visible
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 879
-
script_unit:mark_as_ally(booleanmark as ally) -
Makes player unit look like ally (for script where gradually give units in tutorials). This is an unembellished wrapper for an underlying code function.
Parameters:
1
booleanmark as ally
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 891
-
script_unit:is_hidden() -
Returns true if the unit is hidden in grass/forests etc. This is an unembellished wrapper for an underlying code function. Returned result is not related to the terrain visibility system, which came later than this code function, so a unit may not be hidden (according to this result returned by this function) but may still not be visible to its enemy - check also
script_unit:is_visible_to_enemy.Returns:
booleanis hidden
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 899
-
script_unit:set_invisible_to_all(booleanis hidden) -
Makes the unit invisible. This is an unembellished wrapper for an underlying code function.
Parameters:
1
booleanis hidden
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 907
-
script_unit:is_visible_to_enemy(booleanis visible) -
Returns true if this unit is visible to its enemy, by the rules of the visibility system. Note that the unit may be visible according to the visibility system, but may still be hidden in forests or tall grass - check
script_unit:is_hidden.Parameters:
1
booleanis visible
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 915
These functions explicitly instruct script to take or release control of the subject unit. A unit under script control is removed from the player's control or from general AI control, but may still be controlled by an ai_planner (or script_ai_planner). Note also that giving orders to a unit with a unitcontroller will usually also take control of that unit.
-
script_unit:take_control() -
Takes script control of this unit.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 935
-
script_unit:release_control() -
Releases script control of the subject unit.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 942
-
script_unit:modify_ammo(numberproportion) -
Modifies this unit's ammo to a specified unary proportion of its starting value.
Parameters:
1
numberDesired proportion of starting ammo. Supply 1 to set the ammo back to its starting value, 0.5 to half etc. Values of greater than one are semi-supported - the command will work, but any ammo bars on the UI will show as full until the unit's ammo value drops below its starting value.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 962
-
script_unit:refill_ammo(numberproportion) -
Modifies this unit's ammo to a specified unary proportion of its starting value, but only if it has less than the specified amount.
Parameters:
1
numberDesired proportion of starting ammo. Supply 1 to set the ammo back to its starting value, 0.5 to half etc. Values of greater than one are semi-supported - the command will work, but any ammo bars on the UI will show as full until the unit's ammo value drops below its starting value.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 975
-
script_unit:grant_infinite_ammo() -
Grants this unit infinite ammo by refilling ammo every 5 seconds.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 992
-
script_unit:refresh([booleanshould release]) -
Sets this unit to 1/10th its current fatigue level, and tops its ammo back to full.
Parameters:
1
booleanoptional, default value=false
Release script control of the unit after refreshing. Set this to
trueif the unit is under player control.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1009
-
script_unit:cache_ammo() -
Caches this unit's current ammunition level.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1022
-
script_unit:restore_cached_ammo() -
Restores this unit's ammunition level to the value previously cached with
script_unit:cache_ammo.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1029
-
script_unit:unary_hitpoints() -
Returns this unit's hitpoints as a unary of its initial value. This is an unembellished wrapper for an underlying code function.
Returns:
numberunary hitpoints
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1059
-
script_unit:max_casualties(unary proportion
number,should release
[boolean],exception sunit(s)
[object],silent mode
[boolean]
) -
Sets the maximum number of casualties that this unit can take. If the unit's hitpoints drop below the specified unary value, the unit is made invincible so that it can no longer take casualties (note that it may still rout). Exception scriptunits can be set, so that proximity to those
script_unitorscript_unitsdisables this invincibility (allow certain units to be perceived to charge in and 'save the day').Parameters:
1
numberUnary proportion. A value of 0.5 would allow the unit to take 50% casualties before becoming invincible (note that in extreme scenarios the unit may still die, if one-shotted by something for example).
Call
max_casualtieswith a proportion value of 0 to disables any previousmax_casualtiesmonitor on this unit.2
booleanoptional, default value=false
Set to true to release script control of this unit after
max_casualtiesmakes a change. Set this primarily if the unit is under player control.3
objectoptional, default value=nil
Exception
script_unitorscript_unitscollection. If this unit comes within 30m of any unit specified here, themax_casualtiesmonitor will temporarily cease and they will become vulnerable to casualties.The monitor will resume and this unit will (potentially) regain invincibility when this unit moves more than 40m away from any unit specified as an exception.
4
booleanoptional, default value=false
Activate silent mode.
max_casualtieswrites output by default, supplytruehere to suppress this.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1067
-
script_unit:fearless_until_casualties(unaryproportion, [booleanshould release]) -
Prevents this unit from routing until it takes a certain proportion of casualties. If a proportion of 0 is supplied then any previous monitor set up with
fearless_until_casualtiesis cancelled and the unit is allowed to rout.Parameters:
1
unaryProportion of unit at which it may rout. Value should be expressed as a unary e.g. 0.5 = 50% casualties.
2
booleanoptional, default value=false
Set to true to release script control of this unit after
fearless_until_casualtiesmakes a change. Set this primarily if the unit is under player control.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1162
-
script_unit:rout_on_casualties(unaryproportion) -
Forces this unit to rout when it reaches a certain proportion of casualties. If a proportion of 0 is supplied then any previous monitor set up with
rout_on_casualtiesis cancelled.Parameters:
1
unaryProportion of unit at which it routs. Value should be expressed as a unary e.g. 0.5 = 50% casualties.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1206
-
script_unit:invincible_if_standing([booleanshould release]) -
Makes the subject unit invincible/fearless if it's not already routing.
Parameters:
1
booleanoptional, default value=false
Set to true to release script control of this unit after
invincible_if_standingmakes a change. Set this primarily if the unit is under player control.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1244
-
script_unit:set_invincible(booleanset invincible) -
Makes the subject unit invincible. This is an unembellished wrapper for an underlying code function.
Parameters:
1
booleanset invincible
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1259
-
script_unit:prevent_rallying_if_routing([booleancheck perpetually]) -
Prevents the subject unit from rallying if it's already routing. Supply
trueas a single argument to start a monitor that will continually poll whether this unit is routing, and prevent itParameters:
1
booleanoptional, default value=false
Check perpetually. If false or no value is supplied here, the function will only check the unit's routing state once, at the time the function is called. If the unit is not routing at this time, it may later rout and subsequently rally.
If true is supplied instead, the function sets up a monitor that will continually poll the unit's routing status and, when found to be routing, prevents it from rallying at that time.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1267
-
script_unit:stop_prevent_rallying_if_routing() -
Stops any running monitor started by
script_unit:prevent_rallying_if_routing.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1297
-
script_unit:morale_behavior_fearless() -
Sets this unit to be fearless/unroutable. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1304
-
script_unit:morale_behavior_rout() -
Causes this unit to instantly rout. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1311
-
script_unit:morale_behavior_default() -
Causes this unit to be subject to normal morale. This is an unembellished wrapper for an underlying code function.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1318
-
script_unit:morale_behavior_fearless_if_standing([booleanshould release]) -
Makes the subject unit fearless if it's not already routing.
Parameters:
1
booleanoptional, default value=false
Set to true to release script control of this unit after
morale_behavior_fearless_if_standingmakes a change. Set this primarily if the unit is under player control.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1325
-
script_unit:morale_behavior_default_if_standing([booleanshould release]) -
Makes the subject unit fearless if it's not already routing.
Parameters:
1
booleanoptional, default value=false
Set to true to release script control of this unit after
morale_behavior_default_if_standingmakes a change. Set this primarily if the unit is under player control.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1339
-
script_unit:kill([booleanshould disappear]) -
Instantly kills this unit.
Parameters:
1
booleanoptional, default value=false
Set to true to make the unit instantly disappear, instead of appearing to drop dead on the spot.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1367
-
script_unit:kill_proportion(numberproportion, [numberpreserve proportion], [booleanhide bodies]) -
Instantly kills a unary proportion of this unit.
Parameters:
1
numberProportion to kill, expressed as a unary value (e.g. 0.5 = 50% of the unit's starting number of soldiers die).
2
numberoptional, default value=0
Prevents kill_proportion from reducing the strength of the unit below a specified unary proportion.
3
booleanoptional, default value=false
Hides the bodies.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1378
-
script_unit:kill_proportion_over_time(numberproportion, numberduration, [booleanstop on rout]) -
Kills a unary proportion of this unit over a specified time period in ms.
Parameters:
1
numberProportion to kill, expressed as a unary value (e.g. 0.5 = 50% of the unit's starting number of soldiers die).
2
numberDuration in ms over which to kill soldiers.
3
booleanoptional, default value=false
Stops the function from killing any more soldiers if the unit routs during the process.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1405
-
script_unit:stop_kill_proportion_over_time() -
Stops a running process started by
script_unit:kill_proportion_over_time.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1458
-
script_unit:get_enemy_alliance_num() -
Gets the enemy alliance number.
Returns:
enemy alliance numbernumber
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1477
-
script_unit:get_enemy_alliance() -
Gets the enemy alliance object.
Returns:
enemy alliancebattle_alliance
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1485
-
script_unit:highlight_unit_card(booleanshould highlight, [numberpulse strength], [booleanforce highlight]) -
Pulses a highlight effect on the unit card associated with this scriptunit.
Parameters:
1
booleanSet to
trueto turn the highlight effect on,falseto turn it off.2
numberoptional, default value=5
Sets the strength of the pulse effect. A higher supplied value leads to a more pronounced pulse effect. The default value is 5.
3
booleanoptional, default value=false
Overrides the disabling of help page highlighting with
battle_ui_manager:set_help_page_link_highlighting_permitted. Set this to true if the script explicitly wants to highlight the UI cards when help page link highlighting is disabled (useful in tutorials).Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1505
-
script_unit:add_ping_icon(icon_type Type of icon to add. This is a numeric index.
[number],duration
[number]
) -
Adds a ping icon above the unit, optionally for a duration.
Parameters:
1
numberoptional, default value=8
icon_type Type of icon to add. This is a numeric index.
2
numberoptional, default value=nil
Duration in ms.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1572
-
script_unit:remove_ping_icon() -
Removes a ping icon from above the unit added with
script_unit:add_ping_icon.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1612
All variables provided by script_unit
| unit | unit | unit Unit object representing the subject unit. |
| uc | unitcontroller | Unitcontroller object that controls the unit. |
| start_position | vector | Vector position the unit started the battle at. |
| start_bearing | number | Bearing in degrees the unit started the battle at. |
| start_width | number | Width in m of the unit at the start of the battle. |
Scriptunits
A Scriptunits collection is a container for script_unit objects. There are a couple of advantages to assembling a script_units collection object instead of just a throwing them into a single table:
- The collection object provides additional functionality that can only be provided on a number of scriptunits (e.g.
script_units:rout_over_time). - Calls made to the scriptunits collection that are not recognised as function calls on the collection object are instead passed through and that same call is made on each script unit the collection contains. For example, setting a behaviour active on a scriptunits collection sets it active on all the scriptunits within that collection.
-
script_units:new(stringname, ...scriptunits) -
Creates a new script units collection.
Parameters:
1
stringName for this script_units collection. Will be used for debug output.
2
...One or more
script_unitobjects to add to the collection. Note that a scriptunit can be a member of multiple script_units collections at the same time.Returns:
script_units
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1759
-
script_units:set_debug([booleandebug mode]) -
Sets the scriptunits collection into debug mode, for more output.
Parameters:
1
booleanoptional, default value=true
debug mode
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1803
-
script_units:add_sunits(...additional script units) -
Adds one or more supplied
script_unitobjects to this scriptunits collection.Parameters:
1
...additional script units
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1826
-
script_units:remove_sunit(script_unitscriptunit to remove) -
Removes a supplied
script_unitobject from this scriptunits collection.Parameters:
1
script_unitscriptunit to remove
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1870
-
script_units:remove_sunits(...script_unit list) -
Removes one or more supplied
script_unitobjects from this scriptunits collection.Parameters:
1
...scriptunits to remove
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1890
-
script_units:get_sunit_by_name(valuereference) -
Returns a
script_unitfrom this script_units collection that matches the supplied referencenumberorstring.nilis returned if no matching script_unit is found.Parameters:
1
valueReturns:
matching name, orscript_unitnil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1939
-
script_units:get_sunit_for_unit(unitbattle_unit) -
Returns a
script_unitfrom this script_units collection that contains the suppliedbattle_unit.nilis returned if no matching script_unit is found.Parameters:
1
unit
Returns:
matching unit, orscript_unitnil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1953
-
script_units:get_sunit_by_type(unit typestring) -
Returns the
script_unitin this script_units collection that contains abattle_unitof the supplied type.nilis returned if no matching script_unit is found.Parameters:
1
Unit type, from the
main_unitstable.Returns:
matching unit, orscript_unitnil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1967
-
script_units:contains_sunit(script_unitto test) -
Returns true if this script_units collection contains the supplied
script_unit, false otherwise.Parameters:
1
script_unitto test
Returns:
booleancollection contains sunit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1981
-
script_units:contains_type(stringunit type) -
Returns whether this script_units collection contains a
script_unitof the supplied type, from themain_unitstable.Parameters:
1
stringunit type
Returns:
booleancollection contains type
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 1996
-
script_units:count() -
Returns the number of
script_unitobjects in this script_units collection.Returns:
numbernumber of units
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2011
-
script_units:item(integerindex value) -
Returns the
script_unitin this collection at the supplied index.Parameters:
1
integerindex value
Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2019
-
script_units:get_sunit_table() -
Returns the internal table containing all the
script_unitobjects in thisscript_unitscollection. Be aware that modifications to this table will also modify this script_units object.Returns:
tableof script_unit objects
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2035
-
script_units:filter(stringname, functiontest, [booleanassert if empty]) -
Returns another scriptunits collection containing all
script_unitobjects from the subject collection that pass the supplied test. The test should be supplied as a function that takes a scriptunit parameter and returns a boolean value. Should a call to the function with a specific scriptunit return true, that scriptunit will be added to the returned collection.Parameters:
1
stringName for the returned scriptunits collection
2
functionFunction that takes a scriptunit parameter and returns a boolean value.
3
booleanoptional, default value=false
If set to true,
filtertriggers a script assert if the returned collection is empty.Returns:
script_units
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2043
-
script_units:out() -
Prints the list of
script_unitobjects this collection contains to the debug console spool.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2080
-
script_units:duplicate(stringname) -
Returns a duplicate
script_unitscollection with the supplied name.Parameters:
1
stringname
Returns:
script_unitsduplicate collection
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2095
-
script_units:get_unitcontroller([army,If an army is supplied here]) -
Returns a unitcontroller with control over all the units this
script_unitscollection contains.Parameters:
1
army,optional, default value=nil
the function will only add units that belong to that army and ignore any that don't.
If no army is supplied, the function assumes that all units in the scriptunits collection are in the same army, and will assert if any aren't.
Returns:
unitcontroller
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2119
-
script_units:num_generals() -
Returns the number of commanding general units the scriptunits collection contains.
Returns:
number of generalsnumber
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2156
-
script_units:get_general_sunit([general indexnumber]) -
Returns a commanding general scriptunit from the scriptunits collection. By default the first general scriptunit is returned. An index value may be given to specify a particular general to return.
Parameters:
1
optional, default value=1
general index
Returns:
number of generalsscript_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2170
-
script_units:centre_point() -
Returns a vector corresponding to the mean centre position of all the
script_unitobjects in the collection.Returns:
vectorcentre position
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2210
-
script_units:radius() -
Returns the distance from the furthest unit from the centre, to the centre, which is an indication of how spread out the units in this collection are.
Returns:
numberradius
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2218
-
script_units:get_northernmost() -
Returns the northern-most
script_unit.Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2238
-
script_units:get_southernmost() -
Returns the southern-most
script_unit.Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2257
-
script_units:get_westernmost() -
Returns the western-most
script_unit.Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2276
-
script_units:get_easternmost() -
Returns the eastern-most
script_unit.Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2295
-
script_units:get_closest(objectposition collection, [2donly]) -
Returns the closest
script_unitin this collection to the supplied collection of units/positions, as well as the distance in m.Parameters:
1
objectPosition object or collection. Supported object types are vector, unit,
script_unit, units,script_units, army, armies, alliance or table.2
2doptional, default value=false
consider 2D distance only (disregarding altitude)
Returns:
script_unitclosest scriptunitdistancedistance of closest scriptunit in mobjectclosest foreign object from the supplied object - either abattle_vector, abattle_unitor ascript_unit, depending upon the container type.
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2314
-
script_units:get_outlying() -
Returns the furthest
script_unitin this collection from the mean centre.Returns:
script_unit
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2410
-
script_units:rout_over_time(numberperiod in ms) -
Prevents routing units within the collection from rallying, and routs all non-routing units over the specified period in ms so that all units are eventually routing.
Parameters:
1
numberTime in ms over which the units are routed. Must be positive.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2465
-
script_units:have_any_deployed() -
Have any of the
script_unitobjects in the collection deployed onto the battlefield.Returns:
booleanany deployed
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2520
-
script_units:have_all_deployed() -
Have all of the
script_unitobjects in the collection deployed onto the battlefield.Returns:
booleanall deployed
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2536
-
script_units:are_any_active_on_battlefield() -
Returns true if any
script_unitin this collection is deployed on the battlefield and not routing or dead.Returns:
booleanany active
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2552
-
script_units:have_any_moved([vectorposition], [distancethreshold distance]) -
Returns true if
script_unit:has_movedreturns true for any unit in this collection. Call<script_units_obj>:script_unit:cache_location() first.Parameters:
1
vectoroptional, default value=nil
Position to test against. May be of limited usefulness when testing multiple units like this.
2
distanceoptional, default value=0
Threshold distance in m.
Returns:
booleanhave any moved
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2582
-
script_units:have_all_moved([vectorposition], [distancethreshold distance]) -
Returns true if
script_unit:has_movedreturns true for all units in this collection. Call<script_units_obj>:script_unit:cache_location() first.Parameters:
1
vectoroptional, default value=nil
Position to test against. May be of limited usefulness when testing multiple units like this.
2
distanceoptional, default value=0
Threshold distance in m.
Returns:
booleanhave all moved
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2600
-
script_units:are_any_running() -
Returns true if any
script_unitin this collection is moving fast.Returns:
booleanany running
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2618
-
script_units:are_all_running() -
Returns true if all
script_unitobjects in this collection are moving fast.Returns:
booleanany running
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2634
-
script_units:is_under_attack() -
Returns true if any
script_unitin this collection is under attack (usesscript_unit:is_under_attack)Returns:
booleanany under attack
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2650
-
script_units:is_in_melee() -
Returns true if any
script_unitin this collection is in melee combat (usesscript_unit:is_in_melee).Returns:
booleanany in melee
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2666
-
script_units:unary_hitpoints() -
Returns the average unary hitpoints of all
script_unitobjects in this collection.Returns:
numberaverage hitpoints
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2682
-
script_units:number_of_enemies_killed() -
Returns the total number of enemies killed by all units in this collection.
Returns:
total enemies killednumber
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2701
Changing formation won't work unless all script_unit objects in the collection are in the same army (which is recommended anyway).
-
script_units:change_formation(stringgroup formation name) -
Sets all
script_unitobjects in the collection into a group formation.
A list of formations can be found in raw data. Valid entries at time of writing are:
- flanking
- generic_directfire_defence
- generic_directfire_attack
- generic
- generic_ranged_protected
- generic_melee_heavy
- generic_melee_super_heavy
- assault_gates_formation
- assault_reserves_formation
- Multiple Selection Drag Out Land
- Multiple Selection Deployable Drag Out Land
- single_line
- Multiple Selection Naval
- Ambush Defence Block
- test_melee_forward_simple
- test_missile_forward_simple
- river_ai_attack
- river_ai_attack_narrow
- river_ai_stop_and_shoot
- river_ai_defend
- stop_and_shoot_artillery
- stop_and_shoot_ranged_direct
Parameters:
1
stringgroup formation name
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2726
-
script_units:is_hidden([booleanall units]) -
Returns true if any
script_unitin this collection is hidden in long grass or trees. Iftrueis supplied as a single argument, then all units in the collection must be hidden for the function to return true.Parameters:
1
booleanoptional, default value=false
all units
Returns:
booleanis hidden
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2774
-
script_units:is_visible_to_enemy() -
Returns true if any
script_unitin this collection is visible to the enemy, by the rules of the terrain visibility system. Note that a unit can be considered visible by this system but still be hidden in forests.Returns:
booleanis visible
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2803
-
script_units:deploy_at_random_intervals(min units
number,max units
number,min period
number,max period
number,debug out
[boolean],spawn immediately
[boolean]
) -
Deploys the units in this collection onto the battlefield in randomly-sized, randomly-timed batches. Units must have been scripted to not deploy automatically before this is called. Arguments can be used to influence the size and timing of the batches of units.
Parameters:
1
numberMinimum size of random unit batch. Must be postive.
2
numberMaximum size of random unit batch. Must be positive, and not less than the supplied min units value.
3
numberMinimum time period between the arrival of batches in ms. Must be positive.
4
numberMaximum time period between the arrival of batches in ms. Must be positive, and not less than the supplied minimum period.
5
booleanoptional, default value=false
Supply true to turn on debug output.
6
booleanoptional, default value=false
Spawns the first wave immediately.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2827
-
script_units:cancel_deploy_at_random_intervals() -
Cancels/stops a running process started with
script_units:deploy_at_random_intervals.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2962
-
script_units:start_kill_aura(script_unitstarget sunits, numberrange, [numbercasualties proportion]) -
Activates a kill aura around these units that cause a specified enemy/other
script_unitsto take casualties when they come within a specified range. Only one kill aura process may be active on a scriptunit at a time.Parameters:
1
script_unitsTarget
script_unitscollection.2
numberRange in m at which the enemy
script_unitsbegin to take casualties.3
numberoptional, default value=0.02
Proportion of casualties taken per second. This should be specified as a unary proportion of the unit's initial strength, so the default value of 0.02 represents 2% of the initial strength per second.
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 2981
-
script_units:stop_kill_aura() -
Stops the kill aura started on this collection with
script_units:start_kill_aura.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 3026
-
script_units:attack_enemy_scriptunits(script_unitsenemy script_units, [booleanshould run]) -
Instructs this scriptunits collection to attack another, acting entirely under script control. This is best used for a close-quarters scripted engagement where no AI randomness or maneouvring is desired.
Parameters:
1
script_unitsenemy script_units
2
booleanoptional, default value=false
should run
Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 3045
-
script_units:stop_attack_enemy_scriptunits() -
Stops an attack process started with
script_units:attack_enemy_scriptunits.Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 3199
-
script_units:highlight_unit_cards(should highlight
boolean,pulse strength
[number],force highlight
[boolean]
) -
Pulses a highlight effect on all the unit cards associated with this scriptunits collection, using
script_unit:highlight_unit_card.Parameters:
1
booleanSet to
trueto turn the highlight effect on,falseto turn it off.2
numberoptional, default value=5
Sets the strength of the pulse effect. A higher supplied value leads to a more pronounced pulse effect. The default value is 5.
3
booleanoptional, default value=false
Overrides the disabling of help page highlighting with
battle_ui_manager:set_help_page_link_highlighting_permitted. Set this to true if the script explicitly wants to highlight the UI cards when help page link highlighting is disabled (useful in tutorials).Returns:
nil
defined in ../working_data/script/_lib/lib_battle_script_unit.lua, line 3221