How to replace a unit and transfer health and veterancy:
Give it this to the unit you wish to change as a Primary weapon
Code:
Weapon DeployWeapon
LeechRangeWeapon = Yes
AttackRange = 1.0
PrimaryDamage = 999999.0
PrimaryDamageRadius = 1.0
DamageDealtAtSelfPosition = Yes ; this is a suicide bomber... remember?
RadiusDamageAffects = SELF SUICIDE
DamageType = EXPLOSION
DeathType = EXTRA_2 ; Very important for this to work
WeaponSpeed = 99999.0
DelayBetweenShots = 0
ClipSize = 1
ClipReloadTime = 0
AutoReloadsClip = No
End
Add this module to the unit
Code:
Behavior = CreateObjectDie ModuleTag_99
DeathTypes = EXTRA_2
CreationList = OCL_NewUnit
TransferPreviousHealth = Yes ; to transfer health
End
Make sure dying by deathtype EXTRA_2 has no visual or sound effects
Code:
Behavior = SlowDeathBehavior ModuleTag_98
DeathTypes = NONE +EXTRA_2
End
The new unit OCL
Code:
ObjectCreationList OCL_NewUnit
CreateObject
ObjectNames = NewUnit
Offset = X:0.0 Y:0.0 Z:0.0
Count = 1
Disposition = LIKE_EXISTING ; to make it seem like unit changes
InheritsVeterancy = Yes ; so as veterancy is transfered
End
Lastly, make sure your unit's commandset has this button..
Code:
CommandButton Command_DeployUnit
Command = FIRE_WEAPON
WeaponSlot = PRIMARY ; Best if this matches up with an AutoChoose listing that forbids this slot normally
Options = OK_FOR_MULTI_SELECT
TextLabel = CONTROLBAR:whatever
ButtonImage = whatever
ButtonBorderType = ACTION
End
NOTES:
The energy transfered from the old unit is the energy the unit had just before the last shot that killed it.
Side Effects: there are two,
1. this method actually transfers the old unit's DAMAGE, not its health, if both the old and the new unit have the same max energy this is fine, however if for instance the new unit has more max energy then the old, the unit will gain energy after it changes.
Solution: Set both units to the same max energy, if you want to make one weaker (for example half the energy of the other) create a special armour for the weaker one which has double the percentages of the other's armour.
2. When a unit is 'deployed' or 'changed' it will be counted as a unit loss.
Improvements:
If you are a good coder you can modify this code to create all sorts of interesting features, for example you could modify this code so that a unit changes in upgrade form rather then an instant button press, simply create an OCL upgrade which OCLs a weapon with the EXTRA_2 deathtype, it would be wise to make the radius damage as small as possible though so that it effects only the right unit.