Fisherman82 Posted January 28, 2022 Posted January 28, 2022 Im using a script for setting a flag when a EWR radar detects a blue unit in a zone. This is the script that is used as a lua predicate on a continus trigger: EWR1_RED_Unit = Unit.getByName('EWR_1') if EWR1_RED_Unit ~= nil then EWR1_RED_Ctrl = EWR1_RED_Unit:getGroup():getController() local Targets = EWR1_RED_Ctrl:getDetectedTargets(VISUAL, RADAR) if #Targets > 0 then local InterestingTargets = 0 for n = 1, #Targets do if Targets[n].object:isExist() == true and Targets[n].object:inAir() == true and Targets[n].object:getCoalition() == 2 then InterestingTargets = InterestingTargets + 1 local TargetPosition = Targets[n].object:getPosition() local RedZoneInfo = trigger.misc.getZone('RedEWRzone1') local DistanceToRedZone = math.sqrt(math.pow(TargetPosition.p.x - RedZoneInfo.point.x, 2) + math.pow(TargetPosition.p.y - RedZoneInfo.point.y, 2)) if DistanceToRedZone <= RedZoneInfo.radius then return true end end end end end The problem is that the script sometimes crashes when a blue unit is shot down by a SAM, not every time but sometimes, seemingly randomly. I get the error in the image. What is the reason for the error and can I tweak the script to avoid it? Would be most thankfull for any help
Fisherman82 Posted January 29, 2022 Author Posted January 29, 2022 I think its something with when the only detected blue unit is destroyed when the script is running then the n value becomes blank (nil) and then the script crashes. I dont know how to prevent that, any ideas?
cfrag Posted January 29, 2022 Posted January 29, 2022 44 minutes ago, Fisherman82 said: I think its something with when the only detected blue unit is destroyed when the script is running then the n value becomes blank (nil) and then the script crashes. Lets try and find out. You are doing a lot of stuff in the line: indexing, dereferencing and invoking all at once. Perhaps build in some more guards. Instead of for n = 1, #Targets do if Targets[n].object:isExist() == true and Targets[n].object:inAir() == true and Targets[n].object:getCoalition() == 2 then end end try for n = 1, #Targets do local theTarget = Targets[n] if theTarget then local theObject = theTarget.object if theObject and theObject:isExist() and theObject:inAir() and theObject:getCoalition() == 2 then --[[-- do stuff here --]]-- end end end
Fisherman82 Posted January 29, 2022 Author Posted January 29, 2022 5 hours ago, cfrag said: Lets try and find out. You are doing a lot of stuff in the line: indexing, dereferencing and invoking all at once. Perhaps build in some more guards. Instead of for n = 1, #Targets do if Targets[n].object:isExist() == true and Targets[n].object:inAir() == true and Targets[n].object:getCoalition() == 2 then end end try for n = 1, #Targets do local theTarget = Targets[n] if theTarget then local theObject = theTarget.object if theObject and theObject:isExist() and theObject:inAir() and theObject:getCoalition() == 2 then --[[-- do stuff here --]]-- end end end I tried this, and I think it works! Ran the test mission 6 times and got the only detected blue aircraft shot down with a SAM everytime without getting the script error message. Since it used to occur randomly I cant be 100% sure that it works but 6 tests with no errors was unusual before, will test more, thank you! this script is a gamechanger!
Recommended Posts