Script Repository
Put your Lua scripts here. This is less for learning how the scripts work and instead giving working examples and showing the results. If this is not your script please attribute the author.
Please add a description of functionality and use the <syntaxhighlight lang="lua" line='line'>Lua code here</syntaxhighlight> tags.
Contents
- 1 Airfields
- 2 Unit Functions
- 3 Waypoints
- 4 Check Unit Name
- 5 Compare a Unit Detail
- 6 Add a group of trucks to a random zone with a random course.
- 7 Display an image as a SpecialMessage
- 8 Create a single unit in an airfield.
- 9 Create a single unit in the air.
- 10 Create a single unit on whatever side you're playing that has a base named Home.
- 11 Create multiple aircraft with the same name.
- 12 Assign detected unit as target to Strike Mission.
- 13 Create multiple aircraft, numbered.
- 14 Set single unit as ready.
- 15 Set an entire batch as ready.
- 16 Swap Unit Side
- 17 Set SAM's Active if an HQ is Destroyed
- 18 Randomly create and delete units.
- 19 Assign units to a mission
- 20 Assign units to a group
- 21 See component damage
- 22 Set unit Damage
- 23 Arrayed Pilot Name List
- 24 Show Unit Component Damage
- 25 ScenEdit_SetLoadout
- 26 Set Airfield to Ready
- 27 Store Table for Later Use
- 28 Make Units Non-Detectable
- 29 Search a Table
- 30 Create a event that will execute after the specific time passed
- 31 Hello World!
Airfields
- This script places a unit into an airbase.
1 ScenEdit_AddUnit({type = 'Air', unitname = 'F-15C Eagle', loadoutid = 16934, dbid = 3500, side = 'Mercs', base='Home'})
Unit Functions
- This script uses the filterOnComponent method to return a type of component.
1 local unit = ScenEdit_GetUnit({side="BLUFOR", unitname="MIG-21"})
2 print(unit)
3 print(unit.components)
4 print(unit:filterOnComponent("Sensor"))
Which returns the below values. filterOnComponent results highlighted in yellow.
1 unit {
2 type = 'Aircraft',
3 subtype = '2001',
4 name = 'MIG-21',
5 side = 'BLUFOR',
6 guid = '6d088198-4a5b-44a5-b87f-75da50f8cf8c',
7 proficiency = 'Regular',
8 latitude = '0.706202368990351',
9 longitude = '-2.44914023444803',
10 altitude = '10972.8',
11 heading = '0',
12 speed = '350',
13 throttle = 'Loiter',
14 autodetectable = 'False',
15 mounts = '1',
16 magazines = '1',
17 unitstate = 'Unassigned',
18 fuelstate = 'None',
19 weaponstate = 'None',
20 }
21 { [1] = { comp_guid = '33dec6b3-3bef-4858-8c92-4aaf4b8cc31a', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 887, comp_name = 'Jay Bird [RP-22M Sapfir-22]' }, [2] = { comp_guid = '6771ae2d-0e11-413a-9321-b1bf0cd7483d', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 2750, comp_name = 'SPO-10 Sirena-3M' }, [3] = { comp_guid = 'fe11edda-9b00-4175-a83e-369157d354af', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 0, comp_name = 'Mk1 Eyeball' }, [4] = { comp_guid = '721ecc1f-ca3d-42a7-b460-ef43829868c7', comp_type = 'Mount', comp_status = 'Operational', comp_dbid = 274, comp_name = '23mm Gsh-23L [200 rnds]' }, [5] = { comp_guid = '0231e457-4203-4dfc-81ce-5ff0068b6c58', comp_type = 'CommDevice', comp_status = 'Operational', comp_dbid = 20, comp_name = 'UHF/VHF Radio [Unsecure]' }, [6] = { comp_guid = 'f6058dbb-6ce0-4bb0-816b-04437e4a3e75', comp_type = 'Engine', comp_status = 'Operational', comp_dbid = 393, comp_name = 'R-25-300 #1' } }
22 { [1] = { comp_guid = '33dec6b3-3bef-4858-8c92-4aaf4b8cc31a', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 887, comp_name = 'Jay Bird [RP-22M Sapfir-22]' }, [2] = { comp_guid = '6771ae2d-0e11-413a-9321-b1bf0cd7483d', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 2750, comp_name = 'SPO-10 Sirena-3M' }, [3] = { comp_guid = 'fe11edda-9b00-4175-a83e-369157d354af', comp_type = 'Sensor', comp_status = 'Operational', comp_dbid = 0, comp_name = 'Mk1 Eyeball' } }
- This script set a unit to "Out of Comms". This is a simpler way than destroying a radio component.
1 ScenEdit_SetUnit({Name="USS Barry", OutOfComms="True"})
Waypoints
This script will set waypoints for a unit. Useful for pathing OPFOR after they take off for a strike mission.
1 ScenEdit_SetUnit({side="BLUFOR",
2 unitname="Unit",
3 course={
4 {latitude='41.4581064637219', longitude='23.271687278387', presetAltitude=min, desiredAltitude='200', description='test'},
5 {latitude='42.2706667596912', longitude='23.0730243792651'},
6 {latitude='42.6907498549919', longitude='23.0425951461823'},
7 {latitude='42.7065667663771', longitude='23.4498477038231'}
8 }
9 })
Check Unit Name
This script will check if a unit matches a name. Useful to detect if a certain unit or type of unit enters an area.
1 local unit = UnitX()
2 print(unit)
3 local namecheck = 'JAS 39F Gripen NG'
4 local name2 = 'JAS 39B Gripen'
5 if unit.name == namecheck or unit.name == name2 then print('New!') end
Compare a Unit Detail
Looks at a unit and detects if it matches a variable. The % is needed before the - and the / as Lua will ignore it otherwise.
1 local unit = UnitX()
2 print(unit)
3 print(unit.name)
4
5 if string.find(unit.name, "GBU%-39%/B SDB") then
6 print("SDB")
7 end
Add a group of trucks to a random zone with a random course.
1 local x =0
2 local la="N46"
3 local lo="E25"
4 while x<math.random(10,50) do
5 la = "N" .. math.random(29.5,36.5)
6 lo = "E" .. math.random(60,71)
7 ScenEdit_AddUnit({type = 'FACILITY', name = 'Convoy',
8 heading = math.random(0,359), dbid = 624, side = 'NOTSOVIET', Latitude=la, Longitude=lo,
9 autodetectable="false",
10 course={{lat="N".. math.random(30,37), lon="E" .. math.random(60,71)}}
11 })
12 x=x+1
13 end
Display an image as a SpecialMessage
1 ScenEdit_SpecialMessage("blufor"," <img src=https://i.imgur.com/Zxn84Lol.jpg> ");
2 ScenEdit_SpecialMessage("blufor"," <img src='https://i.imgur.com/Zxn84Lol.jpg' width='525' align='center'> ");
Create a single unit in an airfield.
1 ScenEdit_AddUnit({type = 'Air', unitname = 'F-15C Eagle', loadoutid = 16934, dbid = 3500, side = 'Mercs', base='Home'})
Create a single unit in the air.
1 ScenEdit_AddUnit({type="air", side="Mercs", name="Plane", dbid=1160, loadoutid=12354, latitude="29.297778", longitude="90.911944", altitude="5000 ft", heading=0})
Create a single unit on whatever side you're playing that has a base named Home.
1 ScenEdit_AddUnit({type = 'Air', unitname = 'JAS-39C Gripen', loadoutid = 17279, dbid = 3226, side = ScenEdit_PlayerSide(), base='Home'})
Create multiple aircraft with the same name.
1 for planes = 1, 8, 1 do
2 ScenEdit_AddUnit({type = 'Air', unitname = 'JAS-39C Gripen', loadoutid = 17279, dbid = 3226, side = 'Mercs', base='Home'})
3 end
Assign detected unit as target to Strike Mission.
1 local detectedUnit = ScenEdit_UnitC()
2 ScenEdit_AssignUnitAsTarget(detectedUnit.name, '4thBrigadeStrike')
Create multiple aircraft, numbered.
1 for planes = 1, 8, 1 do
2 ScenEdit_AddUnit({type = 'Air', unitname = 'JAS-39C Gripen #'..planes, loadoutid = 17279, dbid = 3226, side = 'Mercs', base='Home'})
3 end
Set single unit as ready.
1 ScenEdit_SetLoadout({UnitName = "F-15C Eagle", LoadoutID = 0, TimeToReady_Minutes = 0, IgnoreMagazines = 1})
Set an entire batch as ready.
1 for i=1,6 do ScenEdit_SetLoadout({UnitName = "Ghostrider #"..i, LoadoutID = 0, TimeToReady_Minutes = 0, ignoremagazines = 1}) end
Swap Unit Side
1 ScenEdit_SetUnitSide({side='Opfor', name='airfield', newside='Blufor'})
Set SAM's Active if an HQ is Destroyed
Pair this with a "Unit Destroyed" trigger. Found in the CMO tutorial from P Gatcomb.
1 local unit = ScenEdit_UnitX()
2 local latitude= unit.latitude
3 local longitude = unit.longitude
4
5 if(string.find(unit.name,"Control")) then
6
7 local units = VP_GetSide({side='Side'}).units
8 for k, v in ipairs(units) do
9 if(string.find(v.name,"SAM")) then
10 if(Tool_Range({latitude=latitude, longitude=longitude}, v.guid) < 20) then
11 ScenEdit_SetEMCON('Unit',v.guid,'Radar=Active')
12 end
13 end
14 end
15 end
Randomly create and delete units.
1 math.randomseed(os.time())
2 math.random()
3
4 a = math.random(1,3)
5
6 print(a)
7
8 if a == 1 then
9
10 ScenEdit_DeleteUnit({name="Radar (China JY-8A Wall Rust)", base="Lhasa Gonggar International Airport"})
11 print("The Radar Blew Up!")
12
13 elseif a == 2 then
14
15 ScenEdit_DeleteUnit({name="Radar (China JY-8A Wall Rust)", base="Lhasa Gonggar International Airport"})
16 ScenEdit_AddUnit({type="land", side="China", name="AAA Sec (35mm/90 Type 90 x 2) (2x)", dbid=1682, latitude="29.297778", longitude="90.911944"})
17 print("The Radar Blew up! But you detect AAA radar firing up...")
18
19 elseif a == 3 then
20
21 ScenEdit_AddUnit({type="land", side="China", name="AAA Sec (35mm/90 Type 90 x 2) (2x)", dbid=1682, latitude="29.297778", longitude="90.911944"})
22 ScenEdit_AddUnit({type="land", side="China", name="AAA Sec (35mm/90 Type 90 x 2) (2x)", dbid=1682, latitude="29.304571450518", longitude="90.8938412514606"})
23 print("The mercs cut a sewage line and alerted the base. You detect AAA radar firing up...")
24
25 end
Assign units to a mission
1 for i=1,6 do ScenEdit_AssignUnitToMission("Jas Gripen 39C #"..i, "Suicide Run") end
Assign units to a group
1 for i = 1, math.random(2,3), 1 do
2 ScenEdit_AddUnit({type='Facility', name='HMMWV #'..i, dbid=2034, side='OPFOR', Latitude=42.6,Longitude=21.3+(i/200)})
3 local unit_toupdate = ScenEdit_GetUnit({name='HMMWV #'..i})
4 unit_toupdate.group = 'Group South'
5 end
See component damage
1 local unit = ScenEdit_GetUnit({side="Opfor", unitname="JAS-39C Gripen#1"})
2 print(unit)
3 print(unit.components)
Set unit Damage
1 ScenEdit_SetUnitDamage({side="Opfor", unitname="JAS-39C Gripen #1", fires=1, components={{"F404-GE-400 RM.12 #1","None"}}}) \\ Sets unit component damaged
Arrayed Pilot Name List
1 arr = {"pilot x", "pilot y"}
2
3 for i,line in ipairs(arr) do
4 ScenEdit_AddUnit({type = 'Air', unitname = line, loadoutid = 17279, dbid = 3226, side = 'Mercs', base='Home'})
5 end
Show Unit Component Damage
To be paired with a trigger for that unit. As below it will print out the component status.
1 local unit = ScenEdit_UnitX()
2 print(unit.damage)
3 print(unit.components)
4 for i,v in ipairs(unit.components) do
5 --print(unit.components[i].comp_status)
6 print(unit.name .. "report says The " .. unit.components[i].comp_name .. " is currently " .. unit.components[i].comp_status .. ".")
7 end
ScenEdit_SetLoadout
1 ScenEdit_SetLoadout({side='Israel', name='908th EARS #1', LoadoutID=3, TimeToReady_Minutes=0})
Set Airfield to Ready
Script from Eboreg.
Okay, say you're building a scenario with some aircraft in it and you want to a) Have instant action and b) Let the players choose which loadouts to carry.
Oh boy, Ready times are pretty long and you don't want the player to have to wait 3-6 hours to get the loadout he wants, what do you do? Well, there's a pretty simple answer in the form of Special Actions. Just create a new special action, set it to active and repeatable, and put this as the Lua Script:
1 local carrierorairfield = ScenEdit_GetUnit({side="Your Side", unitname="Your AirBase"})
2 for i = 1, #carrierorairfield.embarkedUnits.Aircraft do
3 local currentaircraft = ScenEdit_GetUnit({guid=carrierorairfield.embarkedUnits.Aircraft[i]})
4 ScenEdit_SetLoadout({UnitName=currentaircraft.name, LoadoutID=0, TimeToReady_Minutes=0})
5 end
But wait, you don't want players to do this all the time! That would be blatant cheating! Well, enter Events. Set a Time Trigger to go off 1 second after the scenario start and create a Lua Script as the Action, namely:
1 ScenEdit_SetSpecialAction({ActionNameOrId='Instant Aircraft Ready Special Event', IsActive=false})
Store Table for Later Use
This will take a table, convert it to a string, then store it as a key. The second function accesses that key and turns it back into a table.
1 local truckstable = { [1] = '4YWDWP-0HMGEF37B8V13', [2] = '4YWDWP-0HMGEF37B8V23', [3] = '4YWDWP-0HMGEF37B8V1J', [4] = '4YWDWP-0HMGEF37B8V2J', [5] = '4YWDWP-0HMGEF37B8V33', [6] = '4YWDWP-0HMGEF37B8V3J' }
2
3 a = (table.concat(truckstable, ","))
4 --print(a)
5 ScenEdit_SetKeyValue ("trucksStored",a)
6
7 str = ScenEdit_GetKeyValue("trucksStored")
8 newTrucksTable={}
9 for word in string.gmatch(str, '([^,]+)') do
10
11 table.insert(newTrucksTable, word)
12 end
13 print(t)
Make Units Non-Detectable
This uses Apache85's GenerateListofUnitsOnSide function.
1 local OPFOR = GenerateListOfUnitsOnSide("OPFOR")
2
3
4 for k,v in ipairs(OPFOR) do
5 local unit = ScenEdit_GetUnit({guid=v.guid})
6 --print(unit.type)
7 if unit.type == "Facility" then
8 unit.autodetectable=false
9 print("The unit "..unit.name.." is now not detectable.")
10 end
11 end
Search a Table
1 function FindIn( tInput, Value )
2 for _ in pairs( tInput ) do
3 if Value == tInput[_] then return true end
4 end
5 return false
6 end
7
8
9 local tableDBID = {5001,5002,5005}
10
11 local mynumber = 5002
12
13 if FindIn( tableDBID, mynumber ) then
14 print("true")
15 else
16 print("false")
17 end
Create a event that will execute after the specific time passed
1 local timeEVENT1_unix = ScenEdit_CurrentTime() +20;--get time, add 20second
2 timeEVENT1_string = os.date("%Y/%b/%d %H:%M:%S",timeEVENT1_unix) --convert unix time to string format
3 --print(timeEVENT1_string) --debug purpose, check time_string
4 ScenEdit_SetTrigger ({Description = "trigger1", Mode = "add", name = "Trigger1", type = "Time", Time = timeEVENT1_string})--create time trigger, based on timeEvent1
5
6
7 ScenEdit_SetEvent ("event1", {Description = "Event1", Mode = "add"})--creating event called "event1", displayed as "Event1" (Description = "Event1", is optional thus it does not required to create the event)
8 ScenEdit_SetEventTrigger ("event1",{mode = "add", name = "trigger1"})--add Trigger to "event1"
9 ScenEdit_SetEventAction('event1', {mode='add', name = "msg:temp"})--add action to "event1"
Hello World!
1 test = "Hello World!"
2 print(test)