Can't Dynamically Assign Course

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
Bashkire
Posts: 81
Joined: Mon Feb 23, 2015 6:28 pm

Can't Dynamically Assign Course

Post by Bashkire »

Hello chaps,

I'm toying around with a piece of code to cause a stolen civilian aircraft to fly into the side of a warship to simulate a suicide attack,but for the life of me I can't see where this code is going wrong.

The below code was given to me by Apache85, who's help has been invaluable so far:

Code: Select all

local NatoShip = ScenEdit_GetUnit({side='NATO',name='NatoShip'}) -- change as appropriate
 
 local kamikazeAircraft = ScenEdit_GetUnit({side='Banzai',name='Sake #1'}) -- change as appropriate
 
 local kamikazeCourse = {latitude=NatoShip.latitude, longitude=NatoShip.longitude}) --Desired speed only kicks in when you reach the waypoint, once you've got this segment working we can add more logic there
 
 ScenEdit_SetUnit({guid=kamikazeAircraft.guid, course=kamikazeCourse})

Now I then edited it to my uses so it became the following:

Code: Select all

local Albion = ScenEdit_GetUnit({side='NATO',name='[L 14] HMS Albion [LPD]'});
 
 local LoSlo = ScenEdit_GetUnit({side='Civilian Air (Hijacked)',name='Lo-Slo'});
 
 local terminalCourse = ({latitude=Albion.latitude, longitude=Albion.longitude});
 
 ScenEdit_SetUnit({guid=LoSlo.guid, course=terminalCourse});



The problem is that running the code in the Lua editor to test before I put it into an event returns the error:

Code: Select all

ERROR: Input string was not in a correct format.

Doing some messing about and commenting out lines, I believe that the problem lies in the last line where Lo-Slo gets given his waypoint. The Command Lua repository (here) mentions about a waypoint table with lots of possible inputs, but all I really need at the moment is the course to follow the ship and I can worry about speeds and altitudes later.

Can anyone see where this code is going wrong?



EDIT:

Just to keep people informed of progress, I now have this code which is seeming to work somewhat correctly, although I now need to loop it to continue updating the waypoint onto of the ship as it moves:

Code: Select all

local Albion = ScenEdit_GetUnit({side='NATO',name='[L 14] HMS Albion [LPD]'});
 
 local LoSlo = ScenEdit_GetUnit({side='Civilian Air.',name='Lo-Slo'});
 
 local terminalCourse = LoSlo.course;
 
 terminalCourse[1].latitude = Albion.latitude;
 terminalCourse[1].longitude = Albion.longitude;
 
 ScenEdit_SetUnit({side='Civilian Air.',name='Lo-Slo', course=terminalCourse, manualSpeed=170.0, manualAltitude=80.0});

Does anyone know how to loop this thing? I'm not seeing any syntax of while() loops on the documentation I linked above.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Can't Dynamically Assign Course

Post by KnightHawk75 »

You wouldn't need to loop then, just run the code every so often to update the position inside an event say every 5 or 10 seconds.

Code: Select all

   local terminalCourse = {};
   local Albion = ScenEdit_GetUnit({side='NATO',name='[L 14] HMS Albion [LPD]'});
   local LoSlo = ScenEdit_GetUnit({side='Civilian Air.',name='Lo-Slo'});
   if (LoSlo ~=nil and Albion ~=nil) then  --if both exist then do this
     table.insert(terminalCourse,{latitude=Albion.latitude,longitude=Albion.longitude,description='impact'})
     LoSlo.course = terminalCourse
     ScenEdit_SetUnit({side='Civilian Air.',name='Lo-Slo', manualSpeed=170.0, manualAltitude=80.0});
   else 
     --probably put code here to disable the event since one or both are gone or otherwise handle the situation.
     --ScenEdit_SetEvent ("myEventNameOrGuidofEvent", { isActive=false } )
   end
 
You can find all the standard for\while statement documentation in the lua documentation
https://www.lua.org/manual/5.3/contents.html#contents


Bashkire
Posts: 81
Joined: Mon Feb 23, 2015 6:28 pm

RE: Can't Dynamically Assign Course

Post by Bashkire »

At first I don't think I understood what you mean, but I get it now. I didn't notice the Regular Time trigger. That's really handy and something that I didn't even consider existing!

But now I wonder how to delete the event so that the trigger doesn't continually fire once the plane has crashed.

I'll have a play about in that Lua documentation you posted and see if I can think of anything, otherwise I'll be looking at the Command stuff again to search for events.

Cheers!
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Can't Dynamically Assign Course

Post by KnightHawk75 »

The commented out line in the 'else ' clause above will disable the event when you no longer want it active.

If you do want to actually delete it you can do that too.
ScenEdit_SetEvent("EventNameOrGuidHere",{mode='remove'}) <-- I think, mode isn't included above because the default it 'update'.
Bashkire
Posts: 81
Joined: Mon Feb 23, 2015 6:28 pm

RE: Can't Dynamically Assign Course

Post by Bashkire »

Thanks chap. I'll add that line in.

I hope I'm not imposing, but I've also sent you a PM (or rather I'm typing it now) to ask for some more advice regarding this.

Incidentally, for anyone that wants to help me out or see what I've done, here's a stripped down version of my mission with only the kamikaze element.
Attachments
Suicide Attack Test.zip
(44.1 KiB) Downloaded 22 times
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Can't Dynamically Assign Course

Post by KnightHawk75 »

All good, sent you a reply and working example scene that highlights the basic enable and disable process.
Replied before I saw your attachment here, else I would have just used the real one. lol
Post Reply

Return to “Lua Legion”