Attack a specific unit

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
LorenAgus
Posts: 12
Joined: Wed Nov 24, 2021 3:00 pm

Attack a specific unit

Post by LorenAgus »

This is the situation and for which I cannot find a solution:

Two countries (BLUE and RED). BLUE sends a transport plane to base A. Once it arrives it deploys 3 NASAMS batteries nearby via an event. So far everything is correct.

local h;
h = ScenEdit_AddUnit ({type = 'Facility', name = 'LAUNCH 1', dbid = 653, side = "BLUE", Lat = "28.983333333333334" , Lon = "-13.58333333333333",})
h = ScenEdit_AddUnit ({type = 'Facility', name = 'LAUNCH 2', dbid = 653, side = "BLUE", Lat = "28.983333333333334" , Lon = "-13.6",})
h = ScenEdit_AddUnit ({type = 'Facility', name = 'LAUNCH 3', dbid = 653, side = "BLUE", Lat = "28.983333333333334" , Lon = "-13.616666666666667",})
ScenEdit_DeleteUnit({side="BLUE",unitname="DUMBO #1"})
a = 1


Once deployed I try to get a RED plane to take off to attack LAUNCH 1 through another event. The event checks every minute the value of a:

if a == 1 then
ScenEdit_AttackContact("Bomber #1", "LAUNCH 1")
end


But the plane does not take off. The plane is an F1 with air-to-ground armament.

I have thought about trying it with another method: define an area and in that area a STRIKE mission with Bomber #1 assigned but without targets, and later, when a=1 use a LUA command to assign the target to the mission, but I don't know how it's done.

If someone can help me I appreciate it. Thank you very much
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Attack a specific unit

Post by KnightHawk75 »

Are you telling it to take off first? I only see the AttackContact command.

This will tell a fighter to take off and give it a named target that it auto-engage upon takeoff.

Code: Select all

local function CheckLaunchAndAttack(theside,attackernameorguid,contactnameorguid)
  local retval,u = pcall(ScenEdit_GetUnit,{side=theside,name=attackernameorguid});
  local retval2,c = pcall(ScenEdit_GetContact,{side=theside,unitname=contactnameorguid});
  if (retval and retval2) and c ~= nil and u ~= nil then
    --Note if ready time was >0 and then reset to 0 then launch call will fail this pulse-second and this must be called next cycle, seems a bug.
    --remove next line if you want unit wait it's normal readytime, or you want to never have to call this twice.
    if u.readytime_v > 0 then u.readytime_v = 0; end
    if u.loadoutdbid ~=nil and u.loadoutdbid > 4 then
      u:Launch(true);
      ScenEdit_AttackContact(u.guid, c.guid,{mode=0}); --mode = automatic
      return true
    else
      print('Attacking unit does not have a loadout.');
    end
  else
   print("Attacking unit or it's contact don't exist.");
  end
  return false
end

---This assumes Red has picked up a contact and the contact's name (not the real unit name) is "LAUNCH 1" on side Red.
---If you don't know what the contact name will be (ie it's not autodetectable) you can get it from the real unit's ascontact table. Ask if need example.
if a == 1 then
  CheckLaunchAndAttack("Red","Bomber #1","LAUNCH 1");
end
LorenAgus
Posts: 12
Joined: Wed Nov 24, 2021 3:00 pm

Re: Attack a specific unit

Post by LorenAgus »

thanks for the answer, it has been very helpful.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Attack a specific unit

Post by KnightHawk75 »

LorenAgus wrote: Sun May 29, 2022 9:41 am thanks for the answer, it has been very helpful.
Glad it helped. Image
Post Reply

Return to “Lua Legion”