There is no possible way to transfer unit health and veterancy through an upgrade of any sort, at least not in the direct sense. However, a little module called CreateObjectDie lets us to transfer a unit"â„¢s last known energy before it is killed to a unit created through an OCL.
The even better point about this method is you can specify an OCL to retain the veterancy of its creator object (see pilot OCL).
So basically we will have to kill a unit to change it, you"â„¢re probably thinking this will look insanely stupid and what would happen if the unit gets killed normally?
Simple, a weapon specifies something called a DeathType, and we can tell how a unit to die according to that specific death type, if we use a unique unused deathtype such as EXTRA_2 we can then specify the unit to create the deathobject unit and not to show any death effects if it is killed by a weapon with that deathtype.
All we have to do then is create a suicide button effect like on the bombtruck which will work as the "Ëœdeploy"â„¢ button.
How to do it:
Give it this to the unit you wish to change as a Primary weapon, this is the suicide weapon with DeathType EXTRA_2
LeechRangeWeapon = Yes
AttackRange = 1.0
PrimaryDamage = 999999.0
PrimaryDamageRadius = 1.0
DamageDealtAtSelfPosition = Yes
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
Add this module to the unit, this specifies the unit to create the OCL of the new unit when it dies by a weapon with deathtype EXTRA_2, also this is where we specify the old unit to transfer energy to the new one by the TransferPreviousHealth = Yes tag.
Behavior = CreateObjectDie ModuleTag_99
DeathTypes = ALL +EXTRA_2
CreationList = OCL_NewUnit
TransferPreviousHealth = Yes ; to transfer health
Make sure dying by deathtype EXTRA_2 has no visual or sound effects
Behavior = SlowDeathBehavior ModuleTag_98
DeathTypes = NONE +EXTRA_2
The new unit OCL, here we specify it to be created in the exact same place as the old unit using the Disposition = LIKE_EXISTING tag and veterancy is also transferred here using the InheritsVeterancy = Yes tag.
ObjectNames = NewUnit
Offset = X:0.0 Y:0.0 Z:0.0
Count = 1
Disposition = LIKE_EXISTING
InheritsVeterancy = Yes
Lastly, make sure your unit's commandset has this button..
The energy transfered from the old unit is the energy the unit had just before the last shot that killed it.
Two known side effects:
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.
Potential 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.