Jump to content

Recommended Posts

Posted (edited)

edit:

ah, I got it. Turns out that the DUMMY unit cannot be marked "dead" or else the Unit.getByName() returns nil. That was the problem. So I created immortal standard unit and now it's ok. I knew it had to be something stupid like this :-)

 

So this now works ok. I expanded it a bit so it also reports hits. Thanks for the debug tips though guys!

 

local StaticTargetName = "DUMMY"
local DetectionRange = 500
local ShotEventHandler = {}
local HitEventHandler = {}
local isHit = "----"
local lastDistance = DetectionRange
function HitEventHandler:onEvent(event)
   if event.id == world.event.S_EVENT_HIT then
       if event.target == Unit.getByName(StaticTargetName) then
           isHit = "HIT!"
           local _ordnanceName = ""
           if event.weapon ~= nil then
               local _ordnance = event.weapon
               _ordnanceName = _ordnance:getTypeName()
           end
               
           local _initname = ""
           if event.initiator ~= nil then
               local _initiator = event.initiator
                   _initname = _initiator:getPlayerName()
               if _initname == nil then
                   _initname = _initiator:getName()
               end
           end            
           
           trigger.action.outText(isHit.."     Who: ".._initname.."     What: ".._ordnanceName.."     Distance: "..lastDistance, 10, true)
       end
   end
end

function ShotEventHandler:onEvent(event)
   if event.id == world.event.S_EVENT_SHOT then
       isHit = "----"
       lastDistance = DetectionRange
       local _ordnance = event.weapon
       local _ordnanceName = _ordnance:getTypeName()
       local _initiator = event.initiator
       local _initname = _initiator:getPlayerName()
       if _initname == nil then
           _initname = _initiator:getName()
       end

       local TrackShot = {}
       function TrackShot()
           if _ordnance:isExist() == true then
               local _sP = _ordnance:getPoint()
               local _staticTarget = Unit.getByName(StaticTargetName)
               local _tP = _staticTarget:getPoint()
               local _distance = math.floor(math.sqrt(math.pow((_sP.x - _tP.x), 2) + math.pow((_sP.y - _tP.y), 2) + math.pow((_sP.z - _tP.z), 2)))
               if _distance < lastDistance then
                   lastDistance = _distance
               end
               
               if _distance < DetectionRange then
                   trigger.action.outText(isHit.."     Who: ".._initname.."     What: ".._ordnanceName.."     Distance: "..lastDistance, 10, true)
               end

               return timer.getTime() + 0.005
           end
       end
       timer.scheduleFunction(TrackShot, nil, timer.getTime() + 0.5)        
   end
end

world.addEventHandler(ShotEventHandler)
world.addEventHandler(HitEventHandler)

Edited by lanmancz

[sIGPIC][/sIGPIC]

 

Gigabyte Aorus Z390 Elite, Intel i9 9900K, Fractal Design Kelvin S36, Zotac GTX 1070 8GB AMP Extreme, 32GB DDR4 HyperX CL15 Predator Series @ 3000 MHz, Kingston SSD 240GB (OS), Samsung 970 EVO 1TB M.2 NVMe (sim), Fractal Design Define R5 Black Window, EVGA SuperNOVA 750 G2, Win 10 Home x64, Thrustmaster Warthog HOTAS, Saitek Pro Flight Rudder Pedals, Thrustmaster MFD Cougar Pack, TrackIR (DelanClip), 3x 27" BenQ EW2740L, Oculus Rift S

 

  • Recently Browsing   0 members

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