LUA to Assign Points When a Unit Arrives at a Location

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
tmoilanen
Posts: 111
Joined: Wed Oct 19, 2011 4:28 pm

LUA to Assign Points When a Unit Arrives at a Location

Post by tmoilanen »

I want to build an Event where I assign points to a side as aircraft arrive at an airbase. Is there an easy way to do this via LUA scripting?

In my scenario, I am deploying a SAM battery to an airfield aboard an airlift of 8 C-5 Galaxy's.

I created a "Unit Remains in Area" Event Trigger for the C-5 aircraft that assigns 5 points to a non-player side whenever the aircraft has remained within the Reference Point area for a period of time. Once the side reaches 40 points (8 C-5s x 5 points each), I trigger a script that adds the SAM battery.

The problem that I am encountering is with the points trigger. It never seems to capture all of the C-5s on the airfield.

I can get to the points trigger by creating a "Unit Remains in Area" trigger for each individual aircraft, but that seems a bit unwieldy. I can get to the points trigger with only C-5M's as the Target Class for the "Unit Remains in Area" trigger if I stagger the arrivals such that only one C-5 is located within the area at a time. Again, that seems a bit unwieldy.

Can anybody suggest a method of doing this via LUA script?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: LUA to Assign Points When a Unit Arrives at a Location

Post by KnightHawk75 »

Code: Select all

local function checkC5AtBaseCount()
  local airbase= SE_GetUnit({guid='4FH7PU-0HM53OIPVVKRU'}) --airbase you are checking.
  local counter = 0;
  local tbl = {  --table of c-5 aircraft guids you want to track.
  '4FH7C5-0HM53OIPVVKU1', '4FH7C5-0HM53OIPVVKU2',
  '4FH7C5-0HM53OIPVVKU3', '4FH7C5-0HM53OIPVVKU4',
  '4FH7C5-0HM53OIPVVKU5', '4FH7C5-0HM53OIPVVKU6',
  '4FH7C5-0HM53OIPVVKU7', '4FH7C5-0HM53OIPVVKU8'
  }
  if (airbase ~=nil) and airbase.embarkedUnits.Aircraft ~=nil then
    for _,v in pairs(airbase.embarkedUnits.Aircraft) do --for every aircraft at the base
        for i=1,#tbl do -- loop through our list the match against.
          if(tbl[i] == v) then -- do we have a match?
            counter = counter +1; 
          end
        end
    end
  end
  return counter;
 end 
 
 if checkC5AtBaseCount() >= 8 then 
   print('do sam addition etc here'); 
 else
   print('do nothing.');
 end;
tmoilanen
Posts: 111
Joined: Wed Oct 19, 2011 4:28 pm

RE: LUA to Assign Points When a Unit Arrives at a Location

Post by tmoilanen »

Thank you! I was able to get this to work, however I did have one problem (with my scenario, not your code.) Not all of the aircraft would be at the base at the same time, as I want to simulate them landing, unloading their cargo and then returning to their home station. Below is what I ended up using.


local unit = ScenEdit_UnitX()
print(unit.name)
unitname = unit.name
print(unitname)

for i = 1, 8 do
aircraftnum = "68th AS #" ..i
if unitname == aircraftnum then
-- get the current score for Eareckson C-5
a = ScenEdit_GetScore("Eareckson C-5")
print(a)
a = a + 1
-- add 1 point
ScenEdit_SetScore("Eareckson C-5",a,"C-5 Delivery")
-- get the new score for Eareckson C-5
a = ScenEdit_GetScore("USTRANSCOM")
print(a)
-- assign the aircraft to return to it's home base
ScenEdit_AssignUnitToMission(aircraftnum,'USTRANSCOM Ferry - Gray Army')
print(unit.mission)
end
end
Post Reply

Return to “Lua Legion”