Difference between revisions of "Script Repository"

From Weapons Release Wiki
Jump to: navigation, search
 
Line 279: Line 279:
  
 
ScenEdit_SetSpecialAction({ActionNameOrId='Instant Aircraft Ready Special Event', IsActive=false})
 
ScenEdit_SetSpecialAction({ActionNameOrId='Instant Aircraft Ready Special Event', IsActive=false})
 +
 +
</syntaxhighlight>
 +
 +
===Make Units Non-Detectable===
 +
 +
[https://www.matrixgames.com/forums/tm.asp?m=4804451 This uses Apache85's GenerateListofUnitsOnSide function.]
 +
 +
<syntaxhighlight lang="lua" line='line'>
 +
local OPFOR = GenerateListOfUnitsOnSide("OPFOR")
 +
 +
 +
for k,v in ipairs(OPFOR) do
 +
    local unit = ScenEdit_GetUnit({guid=v.guid})
 +
--print(unit.type)
 +
    if unit.type == "Facility" then
 +
unit.autodetectable=false
 +
print("The unit "..unit.name.." is now not detectable.")
 +
end
 +
end
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:22, 10 May 2020

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.

Airfields[edit]

  • 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[edit]

  • 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[edit]

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[edit]

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[edit]

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.[edit]

 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

Create a single unit in an airfield.[edit]

1 ScenEdit_AddUnit({type = 'Air', unitname = 'F-15C Eagle', loadoutid = 16934, dbid = 3500, side = 'Mercs', base='Home'})


Create a single unit in the air.[edit]

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.[edit]

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.[edit]

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.[edit]

1 local detectedUnit = ScenEdit_UnitC()
2 ScenEdit_AssignUnitAsTarget(detectedUnit.name, '4thBrigadeStrike')

Create multiple aircraft, numbered.[edit]

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.[edit]

1 ScenEdit_SetLoadout({UnitName = "F-15C Eagle", LoadoutID = 0, TimeToReady_Minutes = 0, IgnoreMagazines = 1})

Set an entire batch as ready.[edit]

1 for i=1,6 do ScenEdit_SetLoadout({UnitName = "Ghostrider #"..i, LoadoutID = 0, TimeToReady_Minutes = 0, ignoremagazines = 1}) end

Swap Unit Side[edit]

1 ScenEdit_SetUnitSide({side='Opfor', name='airfield', newside='Blufor'})

Randomly create and delete units.[edit]

 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[edit]

1 for i=1,6 do ScenEdit_AssignUnitToMission("Jas Gripen 39C #"..i, "Suicide Run") end

See component damage[edit]

1 local unit = ScenEdit_GetUnit({side="Opfor", unitname="JAS-39C Gripen#1"})
2 print(unit)
3 print(unit.components)

Set unit Damage[edit]

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[edit]

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[edit]

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

Set Airfield to Ready[edit]

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})

Make Units Non-Detectable[edit]

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

Variable[edit]

1 test = "Hello World!"
2 print(test)