cross posted as well @ https://www.matrixgames.com/forums/tm.asp?m=4908508
The attached gKH_Side_ReplaceUnitsByDBID_v2.zip contains a more full featured script for this.
I extended what I already had for your use case, and hopefully what I added extends it use for a wider number of cases where people need to mass-replace, but not have to re-do as much after the mass-replace.
I tested it against some USA and PLA aircraft\ship\sub and radar facilities in your scene posted previously in other thread.
Here are the release notes for v2:
The two files Localized and Global are 99.999% the same the only difference is that for Localized_..
The gKH namespace table is defined as local, this is meant for lua console only use.
Such as during scene development process.
The global version once run once will stay in memory, so you don't need to run it again.
It's meant more for use when wanting to use these function during the game play.
ReplaceUnitsByDBID() takes 6 parameters, first 5 are required.
theside: the string side name to operate on, only validation is that it's a string and non-nil. special side "_ALL_" not implimented yet.
thetype: the string unit type code 'Aircraft','Submarine','Ship','Facility' etc. Validation is only it non-nil and string.
thedbid: the database id# for the unit being replaced. must exist and be a number.
theloadoutid: the loadout dbid id to further restrict the replacement on, OR 0 to match all (ie only on unit dbid).
tParams: the table of parameters that you would normally feed to ScenEdit_addunit(), only thing required is {dbid=###,loadoutid=###}
themode: optional;either nil\empty or one of "Normal" or "KeepGUIDS" Not fully tested yet, exists for future enhancement purposes.
themode: default if left empty or is anything else is "Normal", replacement units get new guids, keepguids attempts to reuse old unit guids.
THINGS TO BE AWARE OF:
-Replacement units will by default get new GUIDS, if you want to try reusing old guid, set themode to "KeepGUIDs", however this comes with risk.
The risk is that the unit will be removed before an attempt to create the replacement, and if that fails for some reason the original is now gone.
-Replacement units that were members of groups will be put back into group the original was a members of, however leads may change.
-Replacement units that were members of missions will be assigned to the original units mission. HOWEVER if they were flagged as escorts
they will not get re-flagged as escorts as there is at present no way to detect this in LUA that I found.
-Replacement units that were parked\docked inside bases will be generated and placed in the originally hosting parent. HOWEVER they will
NOT be placed in the same parking spots. Those in th air or at sea,etc will be placed in the same location as the original.
-Replacement units will be assigned the course that the original unit had, if they had a course.
-Replacement units in most cases MAY NOT RECEIVE WRA\DOCTRINE\EMCON settings that the original had. However they should get group\mission\etc
inheritence, such that when you are done making changes, and you goto the various higher level settings screens and press REAPPLY to units
affected it should take care of things. If you had non-iherited specific settings on the original you will have to manually adjust again.
-Replacement units in most cases may not get the same speed\throttle setting that the original had, though some attempt has been made to
preserve altitude\depth, but wide testing has not been done (ie it generally works but current vs desired may be screwed if not on mission).
-Replacing units such as a carrier or airbase\port dbid component - keep in mind any hosted aircraft\ships will be removed.
While it's possible to modify the script to support auto re-generation of hosted units at present it does not support this.
For the above reason I DO NOT recommend using this on DBID's that are part of a port\air-base\carrier, but you can provides you don't mind
losing the contents.
-Remember that once you run a change, if you want to reverse it, the reverse application may affect more units than original if the side
originally had some existing units of what you changed certain things too. ie if side had 5 su-27's and you swap 15 mig21 to su27's, then try
swapping back all su-27's to mig-21's then 20 units will be affected on the swap back. Should be obvious but it's worth keeping in mind.
-You must specificy both the dbid and loadoutid, if you feed it a loadoutid# that is 0, it will ignore loadoutid matching and just use dbid.
EXAMPLE USAGE (happens to be base on Red Dragon Desecnds 2022 scene, but is illustrative of any)
If using in console place these after the end of all the function related code...ie bottom.
-- ** Aircraft swap examples
-- swap j-8f 2473 with loadoutid 9626 to j-11b 4641 loadout: 25278
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 2473, 9626,{dbid=4641,loadoutid=25278});
-- swap j-11b 4641 with loadoutid 25278 to j-8 4273 loadout: 9626
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 4641, 25275,{dbid=2473,loadoutid=9626});
-- swap ALL j-8f 2473 to j-11b 4641 loadout: 25278
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 2473, 0,{dbid=4641,loadoutid=25278});
-- swap ALL j-11b 4641 to j-8 4273 loadout: 9626
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 4641, 0,{dbid=2473,loadoutid=9626});
-- ** Submarine swap examples (note the last param false here is not needed, just shows that sending invaild entry is same as it not being there)
-- swap S-368 296 to 093B Shang 665 0
--gKH.Side:ReplaceUnitsByDBID('PLA','Submarine', 296, 0,{dbid=665,loadoutid=0}, false);
-- swap 093B Shang 665 to j-8 S-296 0
--gKH.Side:ReplaceUnitsByDBID('PLA','Submarine', 665, 0,{dbid=296,loadoutid=0}, false);
-- ** Ship swap examples
-- swap Type 037-IG Houxin 349 to 056A Jiangdao 2703 0
--gKH.Side:ReplaceUnitsByDBID('PLA','Ship', 349, 0, {dbid=2703,loadoutid=0}, false)
-- swap Type 056A Jiangdao 2703 to type 037-IG Houxi 349 0
--gKH.Side:ReplaceUnitsByDBID('PLA','Ship', 2703, 0, {dbid=349,loadoutid=0}, false);
-- ** Facility swap examples
-- swap jy-27 Wide mat 545 to BigBird D 2444
--gKH.Side:ReplaceUnitsByDBID('PLA','Facility', 545, 0,{dbid=2444,loadoutid=0}, false);
-- swap BigBird D 2444 to jy-27 Wide mat 545
--gKH.Side:ReplaceUnitsByDBID('PLA','Facility', 2444,0, {dbid=545,loadoutid=0}, false);