Jump to content

How to run a script when a certain weapon is fired or a certain unit fires any weapon


Galwran

Recommended Posts

Hi.

I'm making a mission in which SAM units will shut down and relocate when ever a HARM or KH-58 is fired at them.

 

As you might have guessed from the title, I cannot set such a condition for the flag to turn TRUE. Everything else works just fine.

 

Sure, I can make the flag turn TRUE whenever SEAD planes are within a certain zone, but that is not the same thing. Also, I of course know the names of the SEAD units, so it would be OK for the SAMs to shut down even when the SEAD unit fires any weapon at any target.

 

 

Please help :helpsmilie:

Link to comment
Share on other sites

Hi.

I'm making a mission in which SAM units will shut down and relocate when ever a HARM or KH-58 is fired at them.

 

As you might have guessed from the title, I cannot set such a condition for the flag to turn TRUE. Everything else works just fine.

 

Sure, I can make the flag turn TRUE whenever SEAD planes are within a certain zone, but that is not the same thing. Also, I of course know the names of the SEAD units, so it would be OK for the SAMs to shut down even when the SEAD unit fires any weapon at any target.

 

 

Please help :helpsmilie:

 

A quick addition: it would be also be nice to override the radar shutdown if the said unit has fire a missile during a 20 second period (so that the shot is not lost)

Link to comment
Share on other sites

You probably want an world EventHandler that responds to S_EVENT_SHOT. Inside the handler function, you can check what type of weapon was fired, and where, and if it is a threat to the SAM site, you can set your flag True. I hhtink you will find this post helpful:

 

http://forums.eagle.ru/showpost.php?p=1623759&postcount=39

 

Thanks. Now I have two problems. The first one, and the knowledge that I suck at programming :smartass:

 

Tried this, didn't work. Looks like something is missing... this is supposed to set the flag 88 TRUE when a betab-500 is dropped.

 

 

do
-- THE EVENT HANDLER
   local shotHandler = function(event)
       if event.id == world.event.S_EVENT_SHOT then
           if event.weapon then 
               local wpn = LuaClass.createFor(Unit, event.weapon.id_)
               if wpn:isExist() then
                   local wpnName = wpn:getTypeName()
                   if wpnName and type(wpnName) == 'string' and (wpnName:find('BetAB_500')) then
				trigger.action.setUserFlag('88', true)
                       
                       end
                       
                   end
               end
           end
       end
   end

Link to comment
Share on other sites

you have to register the event handler with MIst so will run your function periodically. So, make sure you have done a doscriptfile on the mist lua, then in your script, after the code you have above, make sure to add:

mist.addEventHandler(shotHandler)

Link to comment
Share on other sites

you have to register the event handler with MIst so will run your function periodically. So, make sure you have done a doscriptfile on the mist lua, then in your script, after the code you have above, make sure to add:

mist.addEventHandler(shotHandler)

 

Will try in a couple days when I'm back, thanks

Link to comment
Share on other sites

If you do not want to deal with MIst, you can use the following code stand alone:

 

ARM_ShotHandler = {}
function ARM_ShotHandler:onEvent(event)
if event.id == world.event.S_EVENT_SHOT then
	local wpn = event.weapon
	local wpnType = wpn:getTypeName()
	if wpnType == “AGM-88” or wpnType == “Kh-58” then
		*do your code here* 
	end
end
end
world.addEventHandler(ARM_ShotHandler)

 

This will capture each time a AGM-88 or Kh-58 is shot and you can add your code what you want to happen then. Not sure if the type names "AGM-88" and "Kh-58" are correct like that (I assume), the spelling needs to be right though.

 

If you want to do specific actions depending on the targeted unit you could introduce event.target (in the same way as event.weapon) and then build additional conditions depending on this.


Edited by MBot
Link to comment
Share on other sites

Mbot, would you then address the unit, which caused the event with somthing like:

.
.
.
if event.id == world.event.S_EVENT_SHOT then

local wpnorigin = event.unit
lcoal wpnorigingrp = wpnorigin:getGroup()
.
.
.

Would this in case of the event, set wpnorigingrp to the group, which caused the event?

[sIGPIC][/sIGPIC]

 

Unsere Facebook-Seite

Link to comment
Share on other sites

event.initiator is the unit that caused a shot event.

 

event.initator:getGroup() should do the trick.

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

Link to comment
Share on other sites

Thank you 2, will do so.

 

Just had the idea to enforce ROE with this by trying the event händler:

 

ROEViolationHandler = {}
function ROEViolationHandler:onEvent(event)
if event.id == world.event.S_EVENT_SHOT 
 then
 local ROEViolation = event.initator:getGroup()
 if ROEInForce == "Weapons Safe" 
 then
  ROEViolation:destroy()
 end
end
end
world.addEventHandler(ROEViolationHandler)

 

And sorry Galwran for hijacking, but I thought my questions fit the thread.


Edited by SNAFU
then

[sIGPIC][/sIGPIC]

 

Unsere Facebook-Seite

Link to comment
Share on other sites

  • 3 years later...
If you do not want to deal with MIst, you can use the following code stand alone:

 

ARM_ShotHandler = {}
function ARM_ShotHandler:onEvent(event)
if event.id == world.event.S_EVENT_SHOT then
	local wpn = event.weapon
	local wpnType = wpn:getTypeName()
	if wpnType == “AGM-88” or wpnType == “Kh-58” then
		*do your code here* 
	end
end
end
world.addEventHandler(ARM_ShotHandler)

 

This will capture each time a AGM-88 or Kh-58 is shot and you can add your code what you want to happen then. Not sure if the type names "AGM-88" and "Kh-58" are correct like that (I assume), the spelling needs to be right though.

 

If you want to do specific actions depending on the targeted unit you could introduce event.target (in the same way as event.weapon) and then build additional conditions depending on this.

 

Im trying to find somekind of script to count the number of Mavericks fired in a mission by increasing a flag to the same value. Could this script be used for that somehow? In the DO SCRIPT box without MIST or MOOSE? I tried myself by adding a code where it says *do your code here* but I could not get it to work. Does anybody know how to do it?

Link to comment
Share on other sites

  • 5 weeks later...

this will not work for me:

 

ARM_ShotHandler = {}

function ARM_ShotHandler:onEvent(event)

if event.id == world.event.S_EVENT_SHOT then

local wpn = event.weapon

local wpnType = wpn:getTypeName()

if wpnType == “MG_131” then

MESSAGE:New("Plane shot Gun",15,"Alert!"):ToAll()

end

end

end

world.addEventHandler(ARM_ShotHandler)

Link to comment
Share on other sites

  • 2 weeks later...

[TABLE][sIGPIC][/sIGPIC]|

[/TABLE]

Link to comment
Share on other sites

  • Recently Browsing   0 members

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