Jump to content

Recommended Posts

Posted (edited)

I have a repetitive script running to check whether the player has been detected by any enemy unit. Either by radar, visual, optic, etc.

This works fine until an enemy unit gets killed. Then I get this error:

image.png

 

Here's my script:

local function ScanZone(category, coalition, zoneName)
    local foundUnits = {}

    if trigger.misc.getZone(zoneName) ~= nil then

        local searchZone = trigger.misc.getZone(zoneName)

        -- new sphere searchVolume from searchZone
        local searchVolume = {
            ["id"] = world.VolumeType.SPHERE,
            ["params"] = {
                ["point"] = searchZone.point,
                ["radius"] = searchZone.radius,
            }
        }
        -- search the volume for an object category
        world.searchObjects(category, searchVolume, function(obj)
            if obj ~= nil and obj:getLife() > 0 and obj:isActive() and obj:getCoalition()== coalition then
                foundUnits[#foundUnits + 1] = obj
            end
        end)
    end

    if #foundUnits > 0 then
        return foundUnits
    end

    return nil
end

local function isPlayerDetected(enemy)
    if Unit.getController(Unit.getByName(enemy)) then
        local con = Unit.getController(Unit.getByName(enemy))
        local target
        if Unit.getByName("Player") then
            target = Unit.getByName("Player")
        else
            target = Unit.getByName("PlayerTest")
        end

        local m = {}
        for det, enum in pairs(Controller.Detection) do
            if con:isTargetDetected(target, enum) == true then
                trigger.action.outText("You have been spotted by " .. enemy .. ". Method : " .. det, 10)
                return true
            end
        end
    end
    return false
end

local enemyZone = ScanZone(Object.Category.UNIT, coalition.side.RED, "themap")

-- simple check to print the found units or no found units
if enemyZone ~= nil then
    for _, object in pairs(enemyZone) do
        if object:getName() then
            if isPlayerDetected(object:getName()) == true then
                trigger.action.setUserFlag("6", true)
            end
        end
    end
end

 

Can anybody point me in the right direction?

Edited by d0ppler

A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H

Posted

The error is on line 32 which is     if Unit.getController(Unit.getByName(enemy)) then

Basically Unit.getByName(enemy) is returning nil and then you are passing that to Unit.getController(nil), thus the error. 

 

There are a few things you can do to make it more efficient. 

local function isPlayerDetected(enemy)
     local con = Unit.getController(enemy)
     if con then
        local target = Unit.getByName("Player") -- define as this
        if not target then  -- if nil then get the other unit
            target = Unit.getByName("PlayerTest")
        end

        local m = {}
        -- could add an if statement for target ; if target then 
        for det, enum in pairs(Controller.Detection) do
            if con:isTargetDetected(target, enum) == true then
                trigger.action.outText("You have been spotted by " .. enemy .. ". Method : " .. det, 10)
                return true
            end
        end
    end
    return false
end

if enemyZone ~= nil then
    for _, object in pairs(enemyZone) do
        if object:isExist() == true then	-- this checks to make sure the object exists
            if isPlayerDetected(object) == true then   -- you were getting the name of the object, from the object, and then using Unit.getByName to get the object again
                trigger.action.setUserFlag("6", true)
            end
        end
    end
end

 

  • Like 1

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Posted

Thank you! All your comments make very much sense!

I just needed to edit a tiny error:

trigger.action.outText("You have been spotted by " .. enemy .. ". Method : " .. det, 10)

to

 

trigger.action.outText("You have been spotted by " .. enemy:getName() .. ". Method : " .. det, 10)

A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...