normre14232 Posted March 20, 2023 Posted March 20, 2023 (edited) Ok I know I am missing something stupid....but I just really not that good with lua I would like this code to test if any plane units named Threat* are radar detected by any EWR units named EWR* and the trigger flag "detected" All help is greatly appreciated -- Create empty tables to store the EWR and threat units local ewr_units = {} local threat_units = {} -- Loop through all units in the mission and store the EWR and threat units for _, unit in pairs(mist.DBs.unitsByName) do if string.find(unit.name, "^EWR") then table.insert(ewr_units, unit) end if string.find(unit.name, "^Threat") then table.insert(threat_units, unit) end end -- Output the contents of the ewr_units and threat_units tables local ewr_units_str = "EWR units: " .. table.concat(ewr_units, ", ") local threat_units_str = "Threat units: " .. table.concat(threat_units, ", ") trigger.action.outText(ewr_units_str .. "\n" .. threat_units_str, 10) -- Define function to check for detections local function checkDetections() for i, plane in pairs(threat_units) do for j, radar in pairs(ewr_units) do if Controller.isTargetDetected(radar, plane, RADAR) == true then trigger.action.setUserFlag("detected", true) end end end -- Schedule the function to run again in 1 second timer.scheduleFunction(checkDetections, {}, timer.getTime() + 1) end -- Schedule the initial function call in 1 second timer.scheduleFunction(checkDetections, {}, timer.getTime() + 1) P.S ChatGPT can only get you so far....lol Edited March 20, 2023 by normre14232
Grimes Posted March 20, 2023 Posted March 20, 2023 Two take-aways. That is actually kind of close. Why do I get the sense it might have been trained on something I wrote... -- Create empty tables to store the EWR and threat units local ewr_units = {} local threat_units = {} -- Loop through all units in the mission and store the EWR and threat units for uName, unit in pairs(mist.DBs.unitsByName) do if string.find(uName, "^EWR") then -- first problem that it was checking unit.name, which is nil. But the first value from the in pairs is the unitname, so just use that. table.insert(ewr_units, unit.groupName) -- it was inserting the whole table entry of the mist DB. I changed it to the group name since you cant use the unit controller for ground units. end if string.find(uName, "^Threat") then table.insert(threat_units, unit.unitName) --- this is the unit names though. end end -- Output the contents of the ewr_units and threat_units tables local ewr_units_str = "EWR units: " .. table.concat(ewr_units, ", ") local threat_units_str = "Threat units: " .. table.concat(threat_units, ", ") trigger.action.outText(ewr_units_str .. "\n" .. threat_units_str, 10) -- Define function to check for detections local function checkDetections() for j, radar in pairs(ewr_units) do -- I swapped this because its more likely that you will have fewer radars to be checked. local radarGP = Group.getByName(radar) -- it was passing the whole mist DB table entry for the radar.. which can't be directly used in the controller. if radarGP then -- it also assumes everything is accessible and not killed. Which is why I added a bunch of if statements. local rCon = radarGP:getController() -- gotta get the controller to pass to isTargetDetected for i, plane in pairs(threat_units) do local tUnit = Unit.getByName(plane) -- get the plane because again it was a mist.DBs entry. if tUnit then -- unit must be accessible and alive. if Controller.isTargetDetected(rCon, tUnit, Conroller.Detection.RADAR) == true then -- the radar call was wrong because RADAR by itself is not declared anywhere trigger.action.setUserFlag("detected", true) end end end end end -- Schedule the function to run again in 1 second timer.scheduleFunction(checkDetections, {}, timer.getTime() + 1) end -- Schedule the initial function call in 1 second timer.scheduleFunction(checkDetections, {}, timer.getTime() + 1) Donno what happened with some of the tabs. It had a lot of the right ideas of how to do it but the wrong specifics. There are a few optimizations that can be made like saving the group controller and unit objects, then checking if they are still alive rather than constantly re-calling Group.getByName and Unit.getByName each time the function gets called. Especially if you have multiple EWRs it is going to get the same unit X times each check when it already has them. But I kept it roughly in the same format for the sake of comparison. The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
normre14232 Posted March 20, 2023 Author Posted March 20, 2023 Grimes, Thanks very much for the help. Its kinda like chatGPT knows the words and the grammer, but not the context. NormRe
Chump Posted March 20, 2023 Posted March 20, 2023 (edited) GrimesGPT™ Edited March 20, 2023 by Chump lol @ ChatGPT 1
Recommended Posts