Jump to content
Sign in to follow this  
Big Dawg KS

Beginners Guide to Event Handlers

Recommended Posts

So you can already find a lot of information about event handlers on the Wiki, so I'm not going to repeat all of that information. But let's start with a quick overview of what they are.

Event Handlers allow you to execute code every time a specific event occurs. There are a number of different event handler types, all of which you can find on the Wiki. Every object can have any number of event handlers of any type assigned to them.

Adding Event Handlers

It couldn't be simpler.

player addEventHandler ["killed",{hint "You are dead!"}];

Here we added a killed event handler to the player. The type, "killed" here, can be any of the ones listed on the Wiki. The code we executed was 'hint "You are dead!"'. You can call any code you want inside of an event handler.

Indices

When you add an event handler to an object via addEventHandler, it returns an index. You can use the index to reference the event handler (usually for removal). These indices are different for each object and each type of event handler. The indices start at 0.

killedEH = player addEventHandler ["killed",{hint "You are dead!"}]; // returns 0
firedEH = player addEventHandler ["fired",{_this execVM "myScript.sqf"}]; // returns 0
killed2EH = player addEventHandler ["killed",{_this execVM "playerKilled.sqf"}]; // returns 1
fired2EH = tank1 addEventHandler ["fired",{_this execVM "tankFired.sqf"}]; // returns 0

Removing Event Handlers

Simply refer to the type and index:

player removeEventHandler ["killed",0];

Removing an event handler will decrease the indices of the other event handlers of that type. So 0 will always refer to the next event handler, until the object has no more event handlers of that type.

You can also remove all event handlers of a given type.

player removeAllEventHandlers "killed"

Arguments

Like most things in SQF, event handlers reserve a special variable called _this. _this contains a set of arguments (in an array) that differs for each event handler type. Usually (and I'm pretty sure for all of the current existing event types), the first argument is the object that the event handler is attached to. The arguments are very useful, and allow you to get information specific to the cirumstances of the event that you otherwise could not.

You can pass these arguments on to a script as seen in the above indices examples.

"Fired" Event Handler

One of the most (perhaps the most) commonly used event handlers is the "fired" event handler. It can be attached on vehicles or infantry units, and fires (no pun intended) every time the unit or vehicle fires any weapon. This includes hand grenades, mines, and satchel charges. It does not include car horns, and will only execute when a weapon is actually fired (so it excludes empty weapon "clicks").

There fired event handler produces 5 arguments:

1. Object that the event handler is attached to (ie the firing object)

2. The classname of the weapon that was fired

3. The classname of the muzzle that was fired

4. The classname of the mode that was fired

5. The classname of the ammo that was fired

In many cases, when we use a fired event handler, we want to get the actual projectile object that is created. You can easily do this by using the first and fifth arguments and the nearestObject command.

_projectile = nearestObject[_this select 0,_this select 4];

Now, depending on the speed of the projectile, sometimes you can "miss" it with nearestObject (which only searches in a 50m radius around the object it is called on) by the time the script reaches the command. You can avoid this by either calling your script as a function (I won't go into detail about this here) or by grabbing the projectile right inside the event handler's initial scope, and then passing it to your script as an additional argument:

player addEventHandler ["fired",{_this+[nearestObject[_this select 0,_this select 1]] execVM "myScript.sqf"}];

This appends the projectile object to the end of the _this array, so you can access it in your script like so:

_projectile = _this select 5;

Of course, if you the code you call is a function (which does not run in parallel with the engine), you don't have to worry about this.

This is it for now, will post more information about event handlers in general or specific event handler types at a later time, or if someone else wishes to add anything feel free to do so.

Edited by Big Dawg KS
  • Like 2

Share this post


Link to post
Share on other sites

Nice, good start! Well, written, and with some really helpful advice in there that I know bothered me a lot back in the days. :)

Share this post


Link to post
Share on other sites

There's only one error in your guide (beside missing locality hints for MP EH handling)...

The indices are not decreased if you remove an EH of the same type. They will keep the same value as before.

If the index of an EH is 5 it will stay 5, no matter if you remove other EHs or this EH.

Xeno

  • Like 1

Share this post


Link to post
Share on other sites
The indices are not decreased if you remove an EH of the same type. They will keep the same value as before.

If the index of an EH is 5 it will stay 5, no matter if you remove other EHs or this EH.

Xeno

Thanks for sharing. It says in the comref that they are decreased, but who knows how accurate that is. Can you verify this for sure? I'll make a note of it in my post.

(beside missing locality hints for MP EH handling)...

That's because I'm not sure about the locality of each event handler (since it may vary from type to type). This info should be on the wiki, and I can't check right now.

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites
Thanks for sharing. It says in the comref that they are decreased, but who knows how accurate that is. Can you verify this for sure? I'll make a note of it in my post.

They are decreased, sorry for the confusion (mixed it up with addAction).

Guess that happens when you loose more and more interest in modifying this game. Am happy to use a real programming language again for some other project :)

That's because I'm not sure about the locality of each event handler (since it may vary from type to type). This info should be on the wiki, and I can't check right now.

Info is on the Wiki (though not for all EH types).

For example the hit and killed EHs are only fired where the unit is local in MP.

Xeno

  • Like 1

Share this post


Link to post
Share on other sites
Info is on the Wiki (though not for all EH types).

For example the hit and killed EHs are only fired where the unit is local in MP.

Xeno

Well, if you have info that's not on the Wiki you can always add it. :rolleyes:

Share this post


Link to post
Share on other sites

Hi

So i have successfully created an eventhandler "FIRED", but i have a problem with IED, because i just have to place it on the ground to activate my eventhandler ( wich is {addrating -10000} ), how to activate it when i detonate the IED?

Share this post


Link to post
Share on other sites

Hey Big Dawg KS and Xeno this is Avibird 1. I am locked out of my old acc since the last forum update): working to get it back but I have a question for you both?

I am using this Reinforcements Script by lucilk for a domination mission edit I have been working on for a while. The script has an EH that works for a while but after a few times the player gets killed the EH does not work and the script can not be called by the squad leader. I can't understand why. Need some help being working on it for three weeks.

Here was the Instructions:

1. To use the script copy from editor the US Flag (this will execute the scripts and position the HQ)

2. Copy the lkscripts folder

3. Copy the -------US Support---- content from the following files:

briefing.sqf - it contains the briefing AND a important line for adding the menu back after you die

Description.ext - it contains the CfgSounds for tomahawk

init.sqf - it will initialize the briefing and the support menu

This is what the briefing code looks like

// ---US SUPPORT SCRIPT-------

if ( isNil{player getVariable "mk_killedEHadded"} ) then

{

player addEventHandler ["killed",

{

[] spawn {

waitUntil { alive player }; // waitUntil player has respawned

if (playerSide == west && rank player == "CAPTAIN") then {null = [] execVM "lkscripts\menu\close_menu.sqf";}

};

}];

player setVariable ["mk_killedEHadded", true];

};

// ---US SUPPORT SCRIPT---------

This is what the init code looks like

// ---US SUPPORT SCRIPT-------------------------------------------------------------

setPitchBank = compile preprocessfile "lkscripts\tomahawk\setPitchBank.sqf";

player execVM "lkscripts\Dscripts\respawn_player.sqf"; // infinite loop, controls respawning

//player addeventhandler ["Killed", {_this execVM "lkscripts\Dscripts\respawn_player.sqf"}]; // would rather use event handler

if ( (!isServer) && (player != player) ) then

{

waitUntil {player == player};

waitUntil {time > 10};

};

if !(isnull player) then {

null = [] execvm "briefing.sqf";

if (playerSide == west && rank player == "CAPTAIN") then {null = [] execVM "lkscripts\menu\close_menu.sqf"};

};

Can you guys give me any input why and what I can do to make the EH work for the whole mission. Domination is still a great mission to play(:

Share this post


Link to post
Share on other sites

When I am test playing the mission while on LAN and thru the editor I have no issues at all but when playing on internet thru the editor that is when after some time playing and a few respawns the EH stops and the script can not be used again. Why. I am still a little confused about how to get things working after a player gets killed and the whole EH thing.

Share this post


Link to post
Share on other sites

hello, i would need help to add an eventhandler into a domination mission.

i'm playing the Xeno domination ACE TVT. and we often use attack helicopter (Apache/cobra) to shot hellfire missile in pair with soflam laser designator. the problem is when I am firing hellfire (in pilot seat, alone in the chopper shooting in LOAL mode (tracking a soflam)) at AI units or at enemy humans players the scoring is not triggered and I don't get bonus point.

i would need a solution to give to the helicopter pilot bonus points each time he achieve a kill with Hellfire via soflam guidance.

"There fired event handler produces 5 arguments:"

1. Object that the event handler is attached to (ie the firing object) = AH64D

2. The classname of the weapon that was fired = ACE_6Rnd_Hellfire

3. The classname of the muzzle that was fired = HellfireLauncher

4. The classname of the mode that was fired = ? (manual fire ?)

5. The classname of the ammo that was fired = M_Hellfire_AT

http://browser.six-projects.net/cfg_magazines/1769637

what would be the code ? taking into account that the scoring system in Domination seems to be contained in the files

x_client/x_checkkill

x_client/x_checkkilleast

x_client/x_checkkillwest

Edited by cychou

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
Sign in to follow this  

×