Jump to content

How to keep laser on moving targe - scripting


Recommended Posts

Posted

Hello, guys. I'm doing some experimentation with scripting just for the fun of it and got stuck on a problem that I can't see an elegant solution. It's about updating the destiny point of an infrared pointer. I noticed the origin of the beam gets updated if you move the vehicle, but the end point does not. I was guessing to use trigger repetitive is a bad idea, so how I'd set up this? To keep things as simple as possible, I'll pass the reference from hoggit. Thanks for any directions.

local jtac = Unit.getByName('jtacBob')
local target = Unit.getByName('BMPAirDefenseSystemGroup1_unit1'):getPoint()

local ray = Spot.createInfraRed(jtac, {x = 0, y = 1, z = 0}, target)
Posted

What you need to do is (after you created the spot) update the spot's (ray in your code) target coordinates regularly (say every half second or so) with the target's point by invoking ray:setPoint()

  • Like 1
Posted

That's great, man. And how can I do that? like create a function and call it on a repetitive trigger or there's a better way?

Posted
1 hour ago, hmleao said:

That's great, man. And how can I do that? like create a function and call it on a repetitive trigger or there's a better way?

It is the way to do of the JTAC autolase script, available standalone or as part of CTLD ( all made by @Ciribob ). You can see inside this script how it is done (or use it !).

(CTLD thread is just above in "sticky threads" list)

 

  • Like 1
Posted
3 hours ago, hmleao said:

That's great, man. And how can I do that? like create a function and call it on a repetitive trigger or there's a better way?

https://wiki.hoggitworld.com/view/DCS_func_setPoint

  • Thanks 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
On 10/31/2021 at 5:46 PM, Grimes said:

Hey Grimes!

I tried that script on the wiki

 

   local jtac = Unit.getByName('jtacBob')
   local target = Unit.getByName('BMPAirDefenseSystemGroup1_unit1')
   local ray = Spot.createLaser(jtac, {x = 0, y = 1, z = 0}, target:getPoint(), 1337)
   local function updateRay()
       if target:getLife() > 0 then
           ray:setPoint(target:getPoint())
           timer.scheduleFunction(updateRay, {}, timer.getTime() + 0.5)
       else
           ray:destroy()
       end
   end
   timer.scheduleFunction(updateRay, {}, timer.getTime() + 0.5)

 

, and once the target gets killed I get a mission script error:

 

SCRIPTING: Mission script error: [string "   local jtac = Unit.getByName('jtacBob')..."]:5: Unit doesn't exist
stack traceback:
    [C]: ?
    [C]: in function 'getLife'
    [string "   local jtac = Unit.getByName('jtacBob')..."]:5: in function <[string "   local jtac = Unit.getByName('jtacBob')..."]:4>


jtacBob is still up there, the target (BMPAirDefenseSystemGroup1_unit1) is the one destroyed.  Not sure what's going on here, mabye getLife is weird?

Posted

Forgot that it tends to not like it if you do a deadObject:function. Changed it slightly to Object.isExist(target) which should get the desired results. Was actually a little curious to see if LST/LSS works and sure enough it does. Can even guide weapons via it fairly reliably. 

  • 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
6 hours ago, Grimes said:

Forgot that it tends to not like it if you do a deadObject:function. Changed it slightly to Object.isExist(target) which should get the desired results. Was actually a little curious to see if LST/LSS works and sure enough it does. Can even guide weapons via it fairly reliably. 

That did the trick!  Thanks!

Posted (edited)

Since we're talking about some weird behavior of some stuff, maybe Grimes could be so kind as to explain why some times you get some 'nil' units from detectedTargets(). I've made a script with your tip from before that auto marks targets and all is cool, but sometimes when killing some units this error show up. I could solve it without many problems just doing an "if target ~= nil". It's not a big deal, but I wonder if I've made some mistake because I wouldn't think that function would get me "nil" table units. My question is if this is normal or I may have made some mistake. The script only gets the detected targets and uses a for loop to do some stuff, and here and there when killing some of them I get the "nils". Thanks for any tips.

Edited by hmleao
Posted

A code example or the types of targets might help to clarify. Just a few thoughts though:

If you are calling getDetectedTargets once to build a table and then iterate over that table or getDetectedTargets is returning nil values on each run. If it is the latter then that is where knowing the targets matters. I believe there have been changes made where "dead" objects register at different times. Part of this is if using events you may see a unit_lost event before the unit is fully destroyed. It isn't the worst thing in the world to verify your object is still accessible before running code on it simply to avoid errors or extra processing. In your example if target then ought to be enough. If I wrote it differently on the wiki the "test" could just have been if Unit.getByName('BMPAirDefenseSystemGroup1_unit1') then as part of the function that gets called. Honestly don't know if one is faster than another because it takes microseconds to do, but technically stuff like that can add up. Either is fine for most use cases anyway. 

Through a quick and dumb test that some functions can still work on a "dead" unit while others will error. Initially I thought it could be a difference between unit:getLife() and Unit.getLife(unit), but that doesn't seem to be the case.  I would need to spend more time on this to get a thorough answer for which functions can still return something on dead objects and possible add that info to the wiki. At the basics isExist can be relied upon, but other than that I don't know.

  • Thanks 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

Sorry, I should have posted the function where I got this error. The targets are one group of trucks, one of IFVs, one of BMPs and one of tanks. just a mission quickly assembled in ME to test it. What I was doing is saving some group information in a table so I could smoke some unit of that group again after a set time.

-- adds unique detected groups to memory
local function addGroupsMemory (detectedTargets)
 
    for _, target in pairs(detectedTargets) do
 
        if target then
 
            local inMemory = false
            target = target.object
 
            local targetGroupName = target:getGroup():getName()
 
            for _, markedGroup in pairs(MARKED_GROUPS) do
 
                if markedGroup.name == targetGroupName then
                    inMemory = true
                    break
                end
            end
 
            if not inMemory then
 
                local groupInfo = {}
                groupInfo.name = targetGroupName
                groupInfo.timeMarked = timer.getTime()
 
                table.insert(MARKED_GROUPS, groupInfo)
            end
        end
 
    end
end

 

the detected targets parameter there is just called in another function, no other processing with it

local detectedTargets = Controller.getDetectedTargets(jtac)
addGroupsMemory (detectedTargets)

But you answered my question already. The function might give me some nil tables, so just verify with  if target then before running the rest of the script. Looked like a good idea, but actually a group can be pretty scattered xD. I'll try to come up with something to solve this. Thanks again, Grimes

  • Recently Browsing   0 members

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