getDetectedTargets() retains the detected targets and never resets itself.
Once it detects a target it will retain it "forever" even if it's way beyond the detection range.
I tested it with enum 1, 4 and nil and it's the same.
local SAM = "Ground-1-1"
local maxRange = 30000
local controller_unit = Unit.getController(Unit.getByName(SAM))
local tDetectedTargets = controller_unit:getDetectedTargets()
for i, v in ipairs(tDetectedTargets) do
if v then
local objName = Object.getName(tDetectedTargets[i].object)
if Unit.getByName(objName) then
local coal = Unit.getByName(objName):getCoalition()
if coal == 2 then
local coord1 = Unit.getByName(SAM):getPoint()
local coord2 = Unit.getByName(objName):getPoint()
local distancia = Distancia(nil, nil, coord1, coord2)
if distancia < maxRange then
a = 1
else
a = 0
end
end
end
end
end