Jump to content
SlovakianLynx

How to force eventHandler to run only once?

Recommended Posts

Hello, I have an chopper, which I want to broadcast a radio message when it gets hit.
Before I was able to do this on infantry with hitPart eventHandler and calling another script through it:

this addEventHandler["hitPart", {execVM "trafeny.sqf"}];

in the heli's init field.

 

Sometimes the script executed two, or three times, probably because the unit was hit by an automatic weapon.
Now the problem is, that the chopper gets hit by M163, which has very high rate of fire and the script executes almost every time 10, or more times at once.

Is there a way to force the eventHandler to run only once, or is there an alternative for detecting if a unit was hit?

 

Here's the script, that gets executed by the eventhandler (with unimportant lines deleted, so it isn't a spoiler):

mi24_1 removeEventHandler ["hitPart", 0];
mi24_1 setDamage 0.8;
mi24_1 setFuel 0;
sleep 0.5;
playSound "Ot013";
mi24_1D sideChat "FUCK, WE'VE BEEN HIT! WE ARE GOING DOWN!";

 

 

Share this post


Link to post
Share on other sites
this addEventHandler["hitPart",
{
 private _target = (_this select 0 select 0);
 _target removeEventHandler [_thisEvent, _thisEventHandler];
 [_target] execVM "trafeny.sqf";
}];

 

private _target = _this select 0;
_target setDamage 0.8;
_target setFuel 0;
sleep 0.5;
playSound "Ot013";
_target sideChat "FUCK, WE'VE BEEN HIT! WE ARE GOING DOWN!";

not tested.

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

I've tested that three times and it seems to work. No more repeating. Thank you very much for the fast reply.

  • Like 1

Share this post


Link to post
Share on other sites

You already have a nice solution from sarogahtyp but I wanted to add a bit more detail on why this works for people to understand how "HitPart" Event Handler (EH) works.

 

Most probably, the EHs were called more than once because of bullet's penetration. This led to the bullet hitting more than one parts of the chopper (or any other entity for that purpose), which triggered the EH for each part. Now, if you remove the EH from the entity when it is first triggered, it won't be called again for the rest of the parts that possible get hit by the bullet.

 

I hope this shed some light to why the provided solution works and how to "handle" such cases, or use this specific event handler more efficiently to achieve what you want.

  • Like 3

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×