Setting course/speed with lua script?
Moderator: MOD_Command
Setting course/speed with lua script?
Is it possible to set a "manual" course/speed for a unit with lua-script (as with F2/F3-buttons, just with lua instead)?
What is the appropriate command therefor? I could not find it on the documentation-links.
I can't use a "support mission" with defined waypoints, so it would be great to use manual setting of course/speed with lua.
Thanks!
What is the appropriate command therefor? I could not find it on the documentation-links.
I can't use a "support mission" with defined waypoints, so it would be great to use manual setting of course/speed with lua.
Thanks!
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Setting course/speed with lua script?
You can set a course for a unit currently.
example...
unit=ScenEdit_GetUnit({Name="Cactus #2"}); print(unit);
-- give the aircraft a random course to follow
x=math.random(-10,10)
y=math.random(-10,10)
-- course is a pair of point (lon/lat)
unit.course={ {lat=34.39 +x, lon=136.09 +y}, {lat=37.9 +y, lon=140.0 +x } }; print(unit);
To see the course..
print( unit.course )
example...
unit=ScenEdit_GetUnit({Name="Cactus #2"}); print(unit);
-- give the aircraft a random course to follow
x=math.random(-10,10)
y=math.random(-10,10)
-- course is a pair of point (lon/lat)
unit.course={ {lat=34.39 +x, lon=136.09 +y}, {lat=37.9 +y, lon=140.0 +x } }; print(unit);
To see the course..
print( unit.course )
Michael
RE: Setting course/speed with lua script?
Hello Michael!
Thanks for this information.
I will try if I can use your idea with setting the course.
Martin
Thanks for this information.
I will try if I can use your idea with setting the course.
Martin
-
bearhunter007
- Posts: 23
- Joined: Sat Mar 11, 2017 2:51 am
RE: Setting course/speed with lua script?
Michael other Lua expert,
With the release of Version 1.11 SR7 is it now possible to set the speed of the unit? Alternatively could you set the throttle?
Thanks
With the release of Version 1.11 SR7 is it now possible to set the speed of the unit? Alternatively could you set the throttle?
Thanks
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Setting course/speed with lua script?
One method:
local unit = ScenEdit_SetUnit({name='CVN 69 Dwight D. Eisenhower [Nimitz Class]', guid='92269e9c-0a02-4659-9338-3aa0986fe8c6',manualspeed='current',throttle='creep'})
In order to keep the speed from the AI, the manual switch (manualspeed='current') needs to be set. Then the throttle settings can be used. The manual speed setting should also be available but it looks like the speed setting is getting overridden by the throttle; It should be taking the speed and adjusting throttle to match.
I'll look in to this
local unit = ScenEdit_SetUnit({name='CVN 69 Dwight D. Eisenhower [Nimitz Class]', guid='92269e9c-0a02-4659-9338-3aa0986fe8c6',manualspeed='current',throttle='creep'})
In order to keep the speed from the AI, the manual switch (manualspeed='current') needs to be set. Then the throttle settings can be used. The manual speed setting should also be available but it looks like the speed setting is getting overridden by the throttle; It should be taking the speed and adjusting throttle to match.
I'll look in to this
Michael
-
bearhunter007
- Posts: 23
- Joined: Sat Mar 11, 2017 2:51 am
RE: Setting course/speed with lua script?
Thanks Michael!
RE: Setting course/speed with lua script?
Hi Michael,
Any update to this? I'm having a similar problem. While I can set the throttle in normal plotted course situations, it won't allow it (immediately returns to cruise) if the unit is RTBing back to the mothership. I know it sounds like a rare condition, but this is exactly what I want to do. I'm writing several scripts to address inconsistencies between ship and aircraft behavior, and this is one of them - airecraft will go to Military or even AfterBurner when RTBing - ships don't. My script as well restricts it to if the Fuel WithDraw 25% doctrine is used, which ensures there is enough fuel for the sprint home.
Please let me know if you would like my scenario and/or LUA, but I doubt you will - its easy to setup.
On another note about course corrections, I played around with that and discovered that you can't make changes while RTBing as well. It immediately changes back to the RTB course. Now the difference between the course and the throttle, is that I can manually change the throttle in CMANO - and of course (pun intended), the course will auto ajdust on its own, which can be a handy thing - I can only think of a very rare case requiring to do so, therefore I'm not too interested in trying to change course while on RTB. However, I was surprised this simple statement failed in LUA:
my_unit.course={{latitude=my_unit.base.latitude},{longitude=my_unit.base.longitude}}
I think its due to the sig digits. lat/lon obtained 'from' a unit has 13 significant digits. But from the CMANO LUA documentation, there are only 3 significant digits used for lat/long inputs. Thats my guess as to why this is failing...do you happen to know a base function to easily chop these digits off?
Thanks,
Dan
Any update to this? I'm having a similar problem. While I can set the throttle in normal plotted course situations, it won't allow it (immediately returns to cruise) if the unit is RTBing back to the mothership. I know it sounds like a rare condition, but this is exactly what I want to do. I'm writing several scripts to address inconsistencies between ship and aircraft behavior, and this is one of them - airecraft will go to Military or even AfterBurner when RTBing - ships don't. My script as well restricts it to if the Fuel WithDraw 25% doctrine is used, which ensures there is enough fuel for the sprint home.
Please let me know if you would like my scenario and/or LUA, but I doubt you will - its easy to setup.
On another note about course corrections, I played around with that and discovered that you can't make changes while RTBing as well. It immediately changes back to the RTB course. Now the difference between the course and the throttle, is that I can manually change the throttle in CMANO - and of course (pun intended), the course will auto ajdust on its own, which can be a handy thing - I can only think of a very rare case requiring to do so, therefore I'm not too interested in trying to change course while on RTB. However, I was surprised this simple statement failed in LUA:
my_unit.course={{latitude=my_unit.base.latitude},{longitude=my_unit.base.longitude}}
I think its due to the sig digits. lat/lon obtained 'from' a unit has 13 significant digits. But from the CMANO LUA documentation, there are only 3 significant digits used for lat/long inputs. Thats my guess as to why this is failing...do you happen to know a base function to easily chop these digits off?
Thanks,
Dan
- CCIP-subsim
- Posts: 467
- Joined: Tue Nov 10, 2015 6:59 pm
RE: Setting course/speed with lua script?
hmm? I've had no trouble setting unit speeds via Lua at the moment, actually (by using manualspeed) - thought I should note that I've only tried with ground units so far.
RE: Setting course/speed with lua script?
Ooops - Forget my course correction issue - I was using the wrong format, putting lat and lon in their own tables. This works fine:
my_unit.course={{latitude=my_unit.base.latitude,longitude=my_unit.base.longitude}}
Still looking to be able to change throttle to flank, when in RTB state though. The only workaround I can think of if this can't be done, is to create a script which will define a custom Bingo fuel value, and when reached, remove the mission from the craft, set intercept course and speed to mothership, and then try to toggle the RTB flag when "ahead of" the mothership's course to ensure intercept while at a slower speed than the mothership. Then I of course have to add the unit to the mission again, once the craft is logically docked...
Cheers
my_unit.course={{latitude=my_unit.base.latitude,longitude=my_unit.base.longitude}}
Still looking to be able to change throttle to flank, when in RTB state though. The only workaround I can think of if this can't be done, is to create a script which will define a custom Bingo fuel value, and when reached, remove the mission from the craft, set intercept course and speed to mothership, and then try to toggle the RTB flag when "ahead of" the mothership's course to ensure intercept while at a slower speed than the mothership. Then I of course have to add the unit to the mission again, once the craft is logically docked...
Cheers
RE: Setting course/speed with lua script?
Uhg - ok, I wrote a new script to act as a new customized Bingo Fuel feature for my surface small craft. It works great, switching the mission over to an "RTB mission" which plots a course to a relative bearing point, fixed x nm ahead of the mothership's current course. I use support mission and while in transit, the craft goes Flank speed, and switches to Full speed when "on station". The craft is "on station" about 1.5nm from the actual ref point, so I set X to 3nm for now.
Here is the logic for the RTB determination (if my RTB mission is set and unit is 'onstation' - btw, the onstation bool doesn't seem to work, so I used throttle to know). It all works fine if the script is put in the LUA Console - but if setup in an Event Action, it doesn't work, and I get the following error:
if my_unit.mission ~= nil then
if string.sub(my_unit.mission.name,1,6) == 'RTB - ' then
if my_unit.mission.throttle ~= 'Flank' then
ScenEdit_SetUnit({side=my_side,name=my_unit.name,RTB=true})
end
end
end
6/5/2017 1:12:50 PM -- B936.13 -- Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Exception: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Stack Trace: at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at NLua.Extensions.TypeExtensions.GetExtensionMethods(Type type, IEnumerable`1 assemblies)
at NLua.Extensions.TypeExtensions.GetExtensionMethod(Type t, String name, IEnumerable`1 assemblies)
at NLua.MetaFunctions.IsExtensionMethodPresent(Type type, String name)
at NLua.MetaFunctions.GetMethodInternal(LuaState luaState)
at NLua.MetaFunctions.GetMethod(LuaState luaState)
at KeraLua.NativeMethods.LuaNetPCall(IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc)
at NLua.Lua.DoString(String chunk, String chunkName)
at Command_Core.Lua.LuaSandBox.RunScript(String str, Boolean RunInteractively, String script)
at .?.(Scenario , SimEvent )
Call Stack & Error details:
Error at 101316,
Has anyone been able to use the RTB unit field successfully? Odd how it works in LUA Console, but doesn't in the Event Editor. I also tried a special action to trigger manually, same issue.
Here is the logic for the RTB determination (if my RTB mission is set and unit is 'onstation' - btw, the onstation bool doesn't seem to work, so I used throttle to know). It all works fine if the script is put in the LUA Console - but if setup in an Event Action, it doesn't work, and I get the following error:
if my_unit.mission ~= nil then
if string.sub(my_unit.mission.name,1,6) == 'RTB - ' then
if my_unit.mission.throttle ~= 'Flank' then
ScenEdit_SetUnit({side=my_side,name=my_unit.name,RTB=true})
end
end
end
6/5/2017 1:12:50 PM -- B936.13 -- Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Exception: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Stack Trace: at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at NLua.Extensions.TypeExtensions.GetExtensionMethods(Type type, IEnumerable`1 assemblies)
at NLua.Extensions.TypeExtensions.GetExtensionMethod(Type t, String name, IEnumerable`1 assemblies)
at NLua.MetaFunctions.IsExtensionMethodPresent(Type type, String name)
at NLua.MetaFunctions.GetMethodInternal(LuaState luaState)
at NLua.MetaFunctions.GetMethod(LuaState luaState)
at KeraLua.NativeMethods.LuaNetPCall(IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc)
at NLua.Lua.DoString(String chunk, String chunkName)
at Command_Core.Lua.LuaSandBox.RunScript(String str, Boolean RunInteractively, String script)
at .?.(Scenario , SimEvent )
Call Stack & Error details:
Error at 101316,
Has anyone been able to use the RTB unit field successfully? Odd how it works in LUA Console, but doesn't in the Event Editor. I also tried a special action to trigger manually, same issue.
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Setting course/speed with lua script?
In the LuaHistory file in the Logs folder, it should say which line got the error.
I have used RTB before during some tests so it was working.
If you attach the scenario, I can have a look at these.
I have used RTB before during some tests so it was working.
If you attach the scenario, I can have a look at these.
Michael
RE: Setting course/speed with lua script?
Thanks Michael - I need to start a new thread if I am going to attach - I'll add it in the Mods section, as this is a LUA specific item. I need to first clean up my scenario, to make it as straight forward as possible.
RE: Setting course/speed with lua script?
Fook me
if my_unit.mission.throttle ~= 'Flank' then
I should have deleted the mission field. Throttle is under unit, not mission. I had mission on the brain because the mission's Transit throttle was Flank, and Station throttle is Full.
unit.RTB works fine
Thanks
if my_unit.mission.throttle ~= 'Flank' then
I should have deleted the mission field. Throttle is under unit, not mission. I had mission on the brain because the mission's Transit throttle was Flank, and Station throttle is Full.
unit.RTB works fine
Thanks
RE: Setting course/speed with lua script?
Folks,
Just a quick query, to make sure I understand the gist of the above replies, BTW am a complete LUA luddite.
So at the momment there is a way to create a LUA script that will set a units/groups speed...
BUT
It will only allow you to set a default Loiter/Creep/Cruise/Mil speed, you cant say to CMNAO I want to have group X procceed at exactly 7 knots or 22 knots?
The reason I ask for clarification, I have a scenario that requires a truck convoy to move at slow speed, stop , then proceed again all at low speed along jungle tracks. Its a bit of a bugger as the preset speed of the land units is either 0 or 30knts
Cheers
Butch
Just a quick query, to make sure I understand the gist of the above replies, BTW am a complete LUA luddite.
So at the momment there is a way to create a LUA script that will set a units/groups speed...
BUT
It will only allow you to set a default Loiter/Creep/Cruise/Mil speed, you cant say to CMNAO I want to have group X procceed at exactly 7 knots or 22 knots?
The reason I ask for clarification, I have a scenario that requires a truck convoy to move at slow speed, stop , then proceed again all at low speed along jungle tracks. Its a bit of a bugger as the preset speed of the land units is either 0 or 30knts
Cheers
Butch
RE: Setting course/speed with lua script?
Hi,
As far as I know, you cant set speed for groups (if I am wrong correct me), but you can set speed of individual unit's, just make sure to set speed for the leading unit, the rest of the group will follow him. or use
You must set up series of event's that will change speed to your desire. Best way to use this is with unit enters area trigger. Then for the action of the trigger use lua code from above to set speed manually.
As far as I know, you cant set speed for groups (if I am wrong correct me), but you can set speed of individual unit's, just make sure to set speed for the leading unit, the rest of the group will follow him.
Code: Select all
ScenEdit_SetUnit({side="", unitname="", manualSpeed=""})Code: Select all
ScenEdit_SetUnit({guid="", manualSpeed=""})You must set up series of event's that will change speed to your desire. Best way to use this is with unit enters area trigger. Then for the action of the trigger use lua code from above to set speed manually.
-
Rory Noonan
- Posts: 2418
- Joined: Thu Dec 18, 2014 1:53 am
- Location: Brooklyn, NY
RE: Setting course/speed with lua script?
Guys,
Thanks for your replies and assistance am again in your debt.
I have tried out what you suggested Somi83, I initially tried a script that was for the group lead, and the event fired but the units didnt move.
So I split the convoy into units, named each unit individually, and then put the LUA script in for each unit, still it fires by the unit doesnt move.
My trigger works fine in this, but my units dont move, heres the LUA Script that I entered, its probably something I have left in or taken out.
ScenEdit_SetUnit({side="Guatemala", unitname="tank1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="tank2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="aaa1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="sam1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck4", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck5", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="tank3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="aaa2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck6", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck7", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck8", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck9", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="sam2", manualSpeed="9"})
I hit the trigger time of 0600 local, the event fires but the units dont move can any one see my error?
Ive included the scenario as an attachment if anyone could spare the time to lookover it for any possible errors.
Thanks again guys
Regards Butch
Thanks for your replies and assistance am again in your debt.
I have tried out what you suggested Somi83, I initially tried a script that was for the group lead, and the event fired but the units didnt move.
So I split the convoy into units, named each unit individually, and then put the LUA script in for each unit, still it fires by the unit doesnt move.
My trigger works fine in this, but my units dont move, heres the LUA Script that I entered, its probably something I have left in or taken out.
ScenEdit_SetUnit({side="Guatemala", unitname="tank1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="tank2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="aaa1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="sam1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck1", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck4", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck5", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="tank3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="apc3", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="aaa2", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck6", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck7", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck8", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="truck9", manualSpeed="9"})
ScenEdit_SetUnit({side="Guatemala", unitname="sam2", manualSpeed="9"})
I hit the trigger time of 0600 local, the event fires but the units dont move can any one see my error?
Ive included the scenario as an attachment if anyone could spare the time to lookover it for any possible errors.
Thanks again guys
Regards Butch
- Attachments
-
- RumbleIn..1985V5.zip
- (76.64 KiB) Downloaded 32 times
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Setting course/speed with lua script?
There is no course set for the units from what I could see. I set one, the unit started up and stopped at 9kt.
Michael
RE: Setting course/speed with lua script?
Michael,
This might sound a dumb question, but does the course need to be set via LAU?
I course plotted using the drop down menu,plot course command, I then set the throttle to 0knts, as I didnt want my group to move, before the event fired.
I could swear that the course was still there for each unit when I deleted the group and named all the units individually.
Can I simply plot each units path again using the menu, or will I need to add that to my script somehow?
I did warn you am hopeless at scripting , but I really do appreciate the help I get from the community
Regards
Butch
This might sound a dumb question, but does the course need to be set via LAU?
I course plotted using the drop down menu,plot course command, I then set the throttle to 0knts, as I didnt want my group to move, before the event fired.
I could swear that the course was still there for each unit when I deleted the group and named all the units individually.
Can I simply plot each units path again using the menu, or will I need to add that to my script somehow?
I did warn you am hopeless at scripting , but I really do appreciate the help I get from the community
Regards
Butch


