Converting a Single Unit Airfield to a multi-unit Inst file based airfield

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by Rory Noonan »

This script will take all the aircraft from a single-unit airfield and move them to a loaded .inst multi-unit airfield, then delete the single unit airfield.

Useful to keep AU count down in large scenarios, you can either script this in for the AI to attack a field (before strike planning), or you could for example tie it to a special action for the player if they want to knock out an airfield.

Below is commented code and attached is a .scen file ready for you to go ahead and paste it in to test.

Code: Select all

 local oldAirbase = ScenEdit_GetUnit({side='Cuba',name='Holguin Airfield'}) --Get the data of the single airbase unit
 local side = VP_GetSide({side='Cuba'}) --create a list of all units and items associated with that side
 local counter, aircraftTable = 0, {} --initialise a counter and prepare a table
 for k,v in ipairs(side.units) do --go through the list of all units and items created above
     local aircraft = ScenEdit_GetUnit({guid=v.guid}) --get the unit info of each unit on that side
     if aircraft.type == 'Aircraft' and aircraft.base.name == oldAirbase.name then --pick out aircraft that have the old base as their designated base (can be in the air or on the ground)
         counter = counter + 1 --increase counter if a unit matches the criteria
         aircraftTable[counter] = aircraft --add that unit to the table in a sequential order
     end
 end
 
 local oldAirbase = ScenEdit_SetUnit({guid=oldAirbase.guid,newname=oldAirbase.name..' Old'}) --change the name of the old airbase 
 ScenEdit_ImportInst('Cuba', '/CWDB Cuba/Holguin AB 1960.inst') --import the inst file unit
 local newAirbase = ScenEdit_GetUnit({side='Cuba',name='Holguin Airfield'}) ---get the data from the generated group (make sure you know the name of the group, for best effect make sure it is named the same as the single unit airport)
 for k,v in ipairs(aircraftTable) do --go through the list of aircraft that matched criteria
     local aircraft = ScenEdit_SetUnit({guid=v.guid,base=newAirbase.guid}) --get each aircrafts individual data
     if aircraft.altitude ~= oldAirbase.altitude then --if the aircraft isn't at the same altitude as its old base (then it's probably flying)
         ScenEdit_SetUnit({guid=v.guid,base=newAirbase.guid})--assign it to the new base without teleporting it there. When it hits RTB is will land at the new base.
     else --if the aircraft IS at the altitude of its old base, it's probably on the ground
         ScenEdit_HostUnitToParent({HostedUnitNameOrID=v.guid,SelectedHostNameOrID=newAirbase.guid})--teleport it to the new base
     end
 end
 
 ScenEdit_DeleteUnit({guid=oldAirbase.guid}) --delete the old base.
 --by apache85; feel free to use, pick apart, whatever. Enjoy!
 
Attachments
Single Uni..stration.zip
(8.57 KiB) Downloaded 40 times
Image
fatgreta1066
Posts: 513
Joined: Sun Dec 30, 2012 2:37 am

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by fatgreta1066 »

When I try to do this in your test scenario I get this result every time. I've tried it in purely original form and also tried deleting the descriptive text in case that was a problem.

Image
Attachments
AirfieldC..ertTest.jpg
AirfieldC..ertTest.jpg (571.44 KiB) Viewed 654 times
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by Rory Noonan »

Just tried it and it worked perfectly; I can see from the console that you've run it a few times and then perhaps by mistake added some extra code in there (GetScenarioTitle()).

I suspect that it is working the first time you run it, which (because there's no error) looks like it does nothing until you check the actual airfield.

Try appending this to the code to let you know when it's finished.
ScenEdit_MsgBox('Ta-da!',0)

Image
Attachments
Capture.jpg
Capture.jpg (251.35 KiB) Viewed 652 times
Image
fatgreta1066
Posts: 513
Joined: Sun Dec 30, 2012 2:37 am

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by fatgreta1066 »

I closed the game and restarted it because I couldn't figure out another way to clear out the text in the top box of the script console.

I started the editor, loaded the .scen file, copied and pasted the text, added the last line you suggested, and ended up with the same result. The airfield is still a single unit airfield on the map, although it's name now has "Old" at the end.

Image
Attachments
AirfieldC..tTest2.jpg
AirfieldC..tTest2.jpg (610.55 KiB) Viewed 652 times
fatgreta1066
Posts: 513
Joined: Sun Dec 30, 2012 2:37 am

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by fatgreta1066 »

I don't have my game installed to the default location on my computer, could that make it so the script can't find the file to import?
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by Rory Noonan »

Nope, I just replicated your issue using the retail version.

Works fine in the beta branch; seems like we found a bug. I'll look into it.

Sorry for the mess around!
Image
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by michaelm75au »

There is a property of the unit that shows the units hosted at a base.
local oldAirbase = ScenEdit_GetUnit({side='Cuba',name='Holguin Airfield'})
oldAirbase.hostedUnits['Aircraft']
is a table of GUIDs for the aircraft held there.

At the moment, it is not being formatted as a proper Lua table. This has been fix the Command code, but not deployed yet.
There is a workaround for it to create a proper Lua table.
local units = {}
for i=1, 100, 1 do
if u.hostedUnits['Aircraft'][tostring(i)] == nil then break end
table.insert(units, u.hostedUnits['Aircraft'][tostring(i)])
end
Michael
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by michaelm75au »

I ran the scenario in the retail version (998.6) without any issue.
Updated to 998.7 and it also works.

Not sure why you can't get it to work

I loaded up the scenario.
Opened console and loaded the attached Lua script into it.
Pressed run
No errors showed up in the Logs/LuaHistory file.
Michael
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by michaelm75au »

ORIGINAL: fatgreta1066

I don't have my game installed to the default location on my computer, could that make it so the script can't find the file to import?
You will get that error if it can't load the INST file - I changed the name in the script and got that error.
Can you check that you have the import file? The game folder part shouldn't matter as the code should know where the game is installed.
C:\Matrix Games\Command Modern Air Naval Operations\ImportExport\CWDB Cuba\Holguin AB 1960.inst
Michael
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by michaelm75au »

Add this line to top of the script. It will then run as if no console attached and show where the error occurs
Tool_EmulateNoConsole(true)

When it can't find the INST, I get this error in the LuaHistory log
Function:ScenEdit_GetUnit (0) Error:Can't find Unit 'Holguin Airfield' on Side 'Cuba'
Exception: [string "Console"]:18: attempt to index local 'newAirbase' (a nil value)
Stack Trace: at NLua.Lua.ThrowExceptionFromError(Int32 oldTop)
at NLua.Lua.DoString(String chunk, String chunkName)
at Command_Core.Lua.LuaSandBox.RunScript(String str, Boolean RunInteractively, String script)

Internal ERROR: [string "Console"]:18: attempt to index local 'newAirbase' (a nil value)

The 'Internal ERROR: [string "Console"]:18' indicates error is on line 18
Michael
fatgreta1066
Posts: 513
Joined: Sun Dec 30, 2012 2:37 am

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by fatgreta1066 »

Hi,

Thanks for so much help with this. I checked and the .inst file is where it should be. I am version 998.7. I'll try the other fixes you mentioned and let you know if I get it working. Again, thanks for helping.

Chris
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Converting a Single Unit Airfield to a multi-unit Inst file based airfield

Post by michaelm75au »

You could try running the command in the console to make sure that the import is working
ScenEdit_ImportInst('Cuba', '/CWDB Cuba/Holguin AB 1960.inst') --import the inst file unit

Delete the base from the map, and then run it to see if it creates the base. Then check if the unit name and side are correct.
Michael
Post Reply

Return to “Lua Legion”