Page 1 of 3
					
				 Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 12:10 pm
				by BeirutDude
				 Guys honestly I'm trying to learn Lua but I've never been a good coder. I want to check if a unit "Pro 1" is airborne and do a random check for engine trouble and RTB with a 5% chance of occurance. Then will do a time check every 15 minutes. 
 
 I took some code from Kushan04's El Dorado Canyon scenario and it seems to be working in the Lua Conspule but can't figure out if...
 
 A this will check if the aircraft is airborne, 
 B. how I RTB it. 
 
 So a man's got to know his limitations, and I can adjust code when I understand what it is doing but I doubt I'm ever writing unique code! I'm betting there is a much easier way to do what I'm asking!
 
 Pro 1 Engine Damage - Damage level = 0 in the Event Trigger (I assume this is to ID the aircraft)
 
 Pro_1SystemsCheck = math.random(1,20)
 
 if Pro_1SystemsCheck == 20 then
 
 	ScenEdit_SetEvent('Pro 1 Engine Damage',{isactive='true'})
 	ScenEdit_SpecialMessage ('NATO', 'Pro 1 reports it is suffering from degradation of engine systems, must RTB.')
 
 end
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 6:34 pm
				by BeirutDude
				 So I think I have it? RTB by assigning to a ferry mission...
 
 Pro_1SystemsCheck = math.random(1,20)
 
 if Pro_1SystemsCheck == 20 then
 
 	ScenEdit_GetUnit({name='Pro 1',side='NATO'})
 	ScenEdit_SpecialMessage ('NATO', 'Pro 1 reports it is suffering from degradation of engine systems, must RTB.')
 	ScenEdit_AssignUnitToMission("Pro 1", "Return to Keflavik")
 
 end
 
 Or can I just skip the GetUnit() ?????????
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 8:14 pm
				by michaelm75au
				 local u = ScenEdit_GetUnit({ guid='f305d072-e884-46f8-ab14-ad5d7a7a2595'})
 u:RTB(true)
 
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 8:31 pm
				by BeirutDude
				 I kind of understand that Local u is assigning the unit to U using the GetUnit command. What exactly does the "local" command (is it command???) do?
 
 Then if I understand correctly the u:RTB(true) is telling U which has been assigned to f305d072-e884-46f8-ab14-ad5d7a7a2595 to RTB
 
 
 Also if I'm reading the tea leaves correctly if it was RTB(false) then the unit would not RTB (say in an if statement)?
 
 Could it be coded...
 
 local Pro_1 = ScenEdit_GetUnit({ guid='f305d072-e884-46f8-ab14-ad5d7a7a2595'})
 Pro_1:RTB(true)
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 9:24 pm
				by michaelm75au
				 'local' is a Lua directive to create the variable for the session only. Once the script finishes, the variable/memory is freed up.
 By default, Lua variables are 'global' so every script can see it. This often runs into issues where one script has set value 'A', and a later script runs doing something with 'A' which isn't the variable that it should be using.
 
 I have a habit of using 'local' for all variables (unless it applies to the whole scenario) just to minimize these types of errors.
 
 And yes, what you wrote should work.
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 9:25 pm
				by michaelm75au
				 If you want to test the RTB, use 
if Pro_1.RTB == true ...
 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 9:28 pm
				by BeirutDude
				 local u = ScenEdit_GetUnit({ guid='f305d072-e884-46f8-ab14-ad5d7a7a2595'})
 u:RTB(true)
 
 OK so this is what I have to check the flight every 5 minutes with a 5% of the event occurring and causing the aircraft to RTB
 
 
Pro_4SystemsCheck = math.random(1,20)
 
 if Pro_4SystemsCheck == 20 then
 
     local Pro_4 = ScenEdit_GetUnit({ guid='XMJRWB-0HLVVBRGSPNTS'})
     Pro_4:RTB(true)
     ScenEdit_SpecialMessage ('NATO', 'Pro 4 reports it is suffering from a hydrolics systems failure.')
 
 end 
  
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 9:36 pm
				by michaelm75au
				 
 I would probably throw in an additional check to make sure it isn't already on RTB
 
 
 local Pro_4 = ScenEdit_GetUnit({ guid='XMJRWB-0HLVVBRGSPNTS'})
 if Pro_4.RTB == false then
    Pro_4SystemsCheck = math.random(1,20)
 
    if Pro_4SystemsCheck == 20 then
 
        Pro_4:RTB(true)
        ScenEdit_SpecialMessage ('NATO', 'Pro 4 reports it is suffering from a hydrolics systems failure.')
 
    end
 end
 
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 10:55 pm
				by BeirutDude
				 Thank you for the help!
 
 
 What is the significance of the "Local" command? What does that do, I'm really trying to understand, not just mimic!
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sat May 23, 2020 11:26 pm
				by michaelm75au
				 'local' is a Lua directive to create a temporary variable for the current script/session only. Once the script finishes, the variable/memory is freed up.
 By default, Lua variables (without the 'local' in front) are 'global' so every script can see it. This often runs into issues where one script has set value for 'A', and a later script that runs does something with 'A' which isn't the variable that it should be using.
 
 I have a habit of using 'local' for all Lua variables (unless it applies to the whole scenario) just to minimize these types of errors.
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sun May 24, 2020 5:17 am
				by KnightHawk75
				 @michael
 
 this will fail will it not? As it's a function and it requires the true\false parameter (which will take action no matter what you feed it), unless something changed very recently. I wish checking if something was already rtb'ing was that easy, I've always had to parse the unitstate string for contains 'RTB ' etc and not contains 'cancelled' or 'called off', and .condition =='Airborn'. PITA 
 
 
  
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sun May 24, 2020 6:15 am
				by KnightHawk75
				 BeirutDude - 
 When you execute a LUAScript action, it executes the chuck of script, anything that runs inside it is one-and-done when using local definitions, and that's what you want most of the time. You want to define your variables or functions with local, until such time you specifically and purposely want them global. Also, when you do want them to be global, I'd suggest prefacing them with a personal prefix, or storing them all inside a global table\namespace.  Cause CMO no longer wipes the environment between scene loads like CMANO used too so stuff from one scenario stays around for the next till you restart Command. So if you have a scenario that uses some global var named orange, and the next scenario loaded also uses some global variable named orange one can run into weird unexpected situations.
 
 Take the follow two simple scripts
 
Code: Select all
--Example LuaScript action that runs on ScenarioLoad:
 gKH = {}  -- this sets up a global var called gKH, it is a lua table, everything in it will be accessible from any other script.
 --btw..don't actually use gKH.. i'm claiming it as mine! ;)
 gKH.LocalTimeOffset = -4 * 3600; -- create a field in our global table and set it's value. 
 function gKH.TimeLog(sLogMessage) -- create a global function to be called from various other scripts later.
   local ingameTime = os.date("%x %X",ScenEdit_CurrentTime()) -- we don't need ingameTime to exist more than temporary inside the function
   --print message with timestamp in front both real and ingame, real time offset to my current location and daylight savings.
   print(os.date("%x %X",os.time() - gKH.LocalTimeOffset) ..' gametime: '.. ingameTime .. '  '.. tostring(sLogMessage)) 
 end
 gKH.TimeLog('startup script completed.')  --uses the above function.
 --end example scenario load script (or just put it in your console and run it once, then delete it from the console)
 
Code: Select all
 --Example 2nd LuaScript action that runs during say some event:
 local function doSomethingWithUnit(u)
  -- pretend we're doing something in here that is involved, and specific to this event
  -- and doesn't need to hang around when this event and script is done executing.
 end
 
 local myunit = SE_GetUnit({side='blue',name='someUnitName'}) -- we don't need 'myunit' var to exist when this script is over.
 doSomething(myunit); --calls the above local function, that function will not exist when this event is over (till it runs again)
 gKH.LocalTimeOffset = -5 * 3600 --changes the global variable (from -4 hours to -5 hours)
 gKH.TimeLog('Calling the gKH.TimeLog function from an event script that does not contain the code for it') --uses global function
 gBDude_SomeUnitGuid = tostring(myunit.guid); -- store something in a new global variable to use later in some 3rd script.
 -- end example event\action sample script. (or just run it from the console - after you've run the first one and removed it)
 When the script 2 is done, the myunit variable does not exist anymore as it's context was local and execution is complete.
 When the script 2 is done, the function 'doSomethingWithUnit' does not exist anymore as it's context was local and execution is complete.
 When the script 2 is done, gBDude_SomeUnitGuid still exists as it was not marked local, like all the gKH stuff it's accessible from all other script now.
 
 Example, take the special actions in Northern Fury 41 related to tankers in Gunner's latest.
 a=ScenEdit_GetScore("NATO")
 
 That ideally should be local a=ScenEdit_GetScore("NATO"), since 'a' doesn't need to be global, and after someone executes the SA, 'a' will exist now till restart, now this doesn't cause a problem that I can tell because 'a' is always being set to something fresh before re-use in those routines, but it could if 'a' is used elsewhere that I've not seen yet.
 
 
 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Sun May 24, 2020 7:32 am
				by michaelm75au
				 ORIGINAL:  KnightHawk75
 
 @michael
 
 this will fail will it not? As it's a function and it requires the true\false parameter (which will take action no matter what you feed it), unless something changed very recently. I wish checking if something was already rtb'ing was that easy, I've always had to parse the unitstate string for contains 'RTB ' etc and not contains 'cancelled' or 'called off', and .condition =='Airborn'. PITA 
 
 
 
 
 There are 2 seperate things:
 'Pro_4.RTB == false' is testing the status of RTB flag on the wrapper
 'Pro_4:RTB(true)' is setting the unit to do RTB
 
 --
 Sorry, you are actually correct. I have been fighting a headache today, and didn't take enough notice of the documentation. Yes, being able to just check the status would be useful.
 At the moment, you can be RTBing for about 3 different reasons - fuel, weapons, damage
 
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 11:01 am
				by BeirutDude
				 KnightHawk75 and michaelm75au  Thanks I understand the "Local" issue better. Sorry but this is how I learn, having things explained to me. Sometimes I can look at code and figure it out if there is good documentation and examples. I actually found the old Command Lua page more helpful, to be honest, as there were more and, IMHO better, examples.
 
 Now my issue with the systems check is it will check for a system failure and RTB the aircraft, but it will do it whether on the ground or not. I actually don't think that is actually unrealistic as a maintenance crew might find an issue or the aircraft takes off and something fails (happened all of the time), but it is interesting that one aircraft (out of eight) with the exact same 2% chance keeps passing the test and RTBing, and I can't figure out why that one. Also an interesting issue is once a aircraft triggers, it tends to do so more frequently???? Again each test should be an independent variable and the previous test should have no bearing on future tests, IF I'M READING THE TEA LEAVES CORRECTLY! 
 
 Sooooo is there a check to see if an aircraft is airborne that I can incorporate before it does a systems status check? probably easy, but like I said, I'm just not that good.
 
 Also This brought up an idea that it might be nice to check grounded aircraft that are in Maintenance status to see if they become available (change to Reserve). Maybe another day, another scenario but think it would be interesting.
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 12:37 pm
				by KnightHawk75
				 ORIGINAL:  BeirutDude
 
 KnightHawk75 and michaelm75au  Thanks I understand the "Local" issue better. Sorry but this is how I learn, having things explained to me. Sometimes I can look at code and figure it out if there is good documentation and examples. I actually found the old Command Lua page more helpful, to be honest, as there were more and, IMHO better, examples.
 
 Now my issue with the systems check is it will check for a system failure and RTB the aircraft, but it will do it whether on the ground or not. I actually don't think that is actually unrealistic as a maintenance crew might find an issue or the aircraft takes off and something fails (happened all of the time), but it is interesting that one aircraft (out of eight) with the exact same 2% chance keeps passing the test and RTBing, and I can't figure out why that one. Also an interesting issue is once a aircraft triggers, it tends to do so more frequently???? Again each test should be an independent variable and the previous test should have no bearing on future tests, IF I'M READING THE TEA LEAVES CORRECTLY! 
 
 Sooooo is there a check to see if an aircraft is airborne that I can incorporate before it does a systems status check? probably easy, but like I said, I'm just not that good.
 
 Also This brought up an idea that it might be nice to check grounded aircraft that are in Maintenance status to see if they become available (change to Reserve). Maybe another day, another scenario but think it would be interesting.
 
 
 As to airborn yes, mentioned in post 11. 
 assuming u is the unit.  if (u.condition == 'Airborn') then print('unit is in the air') end;
 There is always checking altitude > home base's lon\lat elevation as well.
 
 As for what's happening where the same unit is getting chosen, that sounds like maybe a math.random problem, ie not resetting the seed on each call? Might want to print() the value it generates, if it's always pulling the same number out of 1-8 in say 3-4 separate calls at least 1 second apart then you'll know that's it, and adding math.randomseed(os.time()) before the call should help if that's the problem, it shouldn't be the problem but never know. Many be post the whole section of script for it, if you can't find the problem.
  
 
 
 
 
 
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 3:13 pm
				by BeirutDude
				 So I deleted the Lua code for that aircraft and re-entered it. In two play test it is working fine. So Just one of those things I guess?
 
 Third play test and it's happening again, sigh. Also the path for the Frigate is doing crazy things!
 
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 5:23 pm
				by BeirutDude
				 Just going to disable the code, it was an interesting idea, but apparently beyond my comprehension.
 
 For example if RTB is a "Condition" is there a list of "Conditions" in Command? If so Where?
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 7:35 pm
				by BeirutDude
				 OK so just thinking on this more, and maybe overthinking it, I had the math.Random variable for Pro_1SystemsCheck set to trigger at 25, is it possible it is trigger for 25 or higher (I don't know why it would) if I adjusted the code to this might it not fire so much? Or could the fact that the previous code had the unit variable defined within the If statement be causing it to not forget previous triggers? Would taking the unit variable designation (Pro_1) out of the If statement maybe correct the issue?
 
 local Pro_1 = ScenEdit_GetUnit({ guid='XMJRWB-0HLVVBRGSPNS2'})
 
 Pro_1SystemsCheck = math.random(1,50)
 
 if Pro_1SystemsCheck == 50 then
 
     Pro_1:RTB(true)
     ScenEdit_SpecialMessage ('NATO', 'Pro 1 reports it is suffering from a engine systems failure.')
 
 end
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 7:56 pm
				by BeirutDude
				 OR what about this, since Pro_1SystemsCheck is a random number between 1-50 to ensure Command isn't storing the precious results anywhere conduct a "Global" assignment of Pro_1SystemsCheck to 0 before running the script and then just to be sure setting it to 0 Globally right after? Probably overkill, but something weird is happening...
 
 Pro_1SystemsCheck = 0
 
 local Pro_1 = ScenEdit_GetUnit({ guid='XMJRWB-0HLVVBRGSPNS2'})
 
 Pro_1SystemsCheck = math.random(1,50)
 
 if Pro_1SystemsCheck == 50 then
 
        Pro_1:RTB(true)
        ScenEdit_SpecialMessage ('NATO', 'Pro 1 reports it is suffering from a engine systems failure.')
 
 end
 
 Pro_1SystemsCheck = 0
			 
			
					
				 RE: Checking an aircraft in flight for RTB (5% chance)
				Posted: Mon May 25, 2020 8:58 pm
				by BeirutDude
				 OK in my last run Konge_1 (A Norwegian P-3B) was triggering for RTB every ten to fifteen minutes after the initial trigger. So it may have taken 3 hours until it triggered but then after a plane triggers it gets stuck and keeps RTBing.  So I decided to test the script in the Lua Counsel and add Print functions to see what comes back. One right after the initial zeroing out, One right after the IF statement and one right after the finial zeroing it out. 
 
 Konge_1SystemsCheck = 0
 print(Konge_1SystemsCheck)
 
 local Konge_1 = ScenEdit_GetUnit({ guid='XMJRWB-0HLVVBRGSPOG6'})
 
 Konge_1SystemsCheck = math.random(1,50)
 
 if Konge_1SystemsCheck == 50 then
 
     Konge_1:RTB(true)
     ScenEdit_SpecialMessage ('NATO', 'Konge 1 reports it is suffering from a engine systems failure.')
 
 end
 print(Konge_1SystemsCheck)
 
 Konge_1SystemsCheck = 0
 print(Konge_1SystemsCheck)
 
 IT ran for over one hundred attempts before it triggered, but in the game its firing every 10-15 minutes (after initially triggering). Something is up and I don't think its the coding (and lord knows I know I very little about code but run it yourself and you'll see). 
 
 Below are 100 test runs with a 2% chance it should fire about two time, guess what it did! So there is something getting stuck in the game. Although it is interesting how often 49 came up, and numbers in their 40s for that matter, but I have trued using "1" and "25" as the trigger with the same result for the aircraft.
 
 
 0
 27
 0
 0
 16
 0
 0
 15
 0
 0
 8
 0
 0
 22
 0
 0
 30
 0
 0
 9
 0
 0
 48
 0
 0
 26
 0
 0
 48
 0
 0
 6
 0
 0
 10
 0
 0
 49
 0
 0
 8
 0
 0
 33
 0
 0
 28
 0
 0
 13
 0
 0
 35
 0
 0
 3
 0
 0
 35
 0
 0
 4
 0
 0
 11
 0
 0
 34
 0
 0
 40
 0
 0
 6
 0
 0
 15
 0
 0
 23
 0
 0
 39
 0
 0
 12
 0
 0
 50
 0
 0
 36
 0
 0
 30
 0
 0
 5
 0
 0
 6
 0
 0
 2
 0
 0
 47
 0
 0
 39
 0
 0
 31
 0
 0
 38
 0
 0
 47
 0
 0
 15
 0
 0
 47
 0
 0
 18
 0
 0
 45
 0
 0
 36
 0
 0
 47
 0
 0
 30
 0
 0
 7
 0
 0
 42
 0
 0
 49
 0
 0
 39
 0
 0
 21
 0
 0
 47
 0
 0
 37
 0
 0
 30
 0
 0
 16
 0
 0
 30
 0
 0
 47
 0
 0
 12
 0
 0
 15
 0
 0
 13
 0
 0
 38
 0
 0
 11
 0
 0
 1
 0
 0
 19
 0
 0
 20
 0
 0
 22
 0
 0
 31
 0
 0
 44
 0
 0
 2
 0
 0
 4
 0
 0
 43
 0
 0
 39
 0
 0
 49
 0
 0
 39
 0
 0
 49
 0
 0
 32
 0
 0
 21
 0
 0
 35
 0
 0
 19
 0
 0
 38
 0
 0
 49
 0
 0
 47
 0
 0
 40
 0
 0
 41
 0
 0
 50
 0
 0
 44
 0
 0
 2
 0
 0
 38
 0
 0
 47
 0
 0
 49
 0
 0
 49
 0
 0
 31
 0
 0
 43
 0
 0
 29
 0
 0
 11
 0
 0
 20
 0
 0
 17
 0
 0
 7
 0
 0
 23
 0