Home   Help Search Login Register  

Author Topic: remove the EH  (Read 878 times)

0 Members and 1 Guest are viewing this topic.

Offline 456820

  • Contributing Member
  • **
remove the EH
« on: 23 Oct 2005, 18:23:36 »
i checked the tutorial in the ed depot about Event handlers but it doesnt say how to remove them
i know how to remove all current event handlers but how do i remove one fron the script the event handler called (the EH i want to remove is the one which called the script from where i want to remove it from)

hope that makes sense

Offline Terox

  • Former Staff
  • ****
  • Follow the Sappers!
    • zeus-community.net
Re:remove the EH
« Reply #1 on: 23 Oct 2005, 18:57:24 »
give the eventhandler a global var name then use that name as the reference

eg

tx_event1 = player addEventHandler ["killed", {_this exec "playerkilled.sqs"}]

Player removeEventhandler tx_Event1

i also think you can do the following, if you dont want to attach a global variable


Player removeEventhandler  ["killed", {_this exec "playerkilled.sqs"}]
Zeus ARMA2 server IP = 77.74.193.124 :2302
Teamspeak IP = 77.74.193.123

Offline 456820

  • Contributing Member
  • **
Re:remove the EH
« Reply #2 on: 23 Oct 2005, 19:59:52 »
cool thanks alot ill try that out straight away

Offline 456820

  • Contributing Member
  • **
Re:remove the EH
« Reply #3 on: 23 Oct 2005, 20:04:36 »
hang on that may just not work since in my situation
i have one script which is ran through many different soldiers when they are hit i only want it to be ran once per soldier so i need to delete the eh to a certain soldier since im going to have to have

eh1 = pa10 AddEventHandler ["hit",{_this exec "hit.sqs"}]
eh2 = pa11 AddEventHandler ["hit",{_this exec "hit.sqs"}]

etc etc

in my script i could put
_unit removeEventhandler eh1

but if the soldier didnt have eh1 assigned to him it wouldnt get rid of that event handler unless i put

_unit removeEventhandler eh1
_unit removeEventhandler eh2

etc etc

but it might cause some problems or wouldnt it ?

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:remove the EH
« Reply #4 on: 23 Oct 2005, 21:11:18 »
Unfortunately, EHs can be hard to remove like this, as you are finding out. Unlike actions, which pass their index to the action script, EH's don't do that. Here's a trick I've used to get around this sort of problem. I'll write a line of code in italics, then explain it, and so on:

-------------------------------------

_idx = pa10 addeventhandler ["hit", {}]
Here you add a "dummy" EH to the unit, in order to get it's index

pa10 removeeventhandler ["hit", _idx]
Next we remove the dummy EH. Note that this means that the next EH we add will have the same index as this one we just removed (more on this later)

_code = format [{(_this select 0) removeEventHandler ["hit", %1]; _this exec "hit.sqs"}, _idx]
pa10 addeventhandler ["hit", _code]
 
Here we are using the 'format' command to 'insert' the index we found above into the EH's code. Notice that when the format command is reached, OFP will turn that whole thing into a string, and instead of %1 it will have the value of _idx. So when the EH is run, whatever we found for _idx will be used in the 'removeeventhandler' command.

That's the idea behind it, you just gotta repeat for all the units you want to add the EH to.
-------------------------------------

There is one problem with this method (or any method to remove EHs), however. Let's say you have a unit with 4 'hit' EHs on him:

index         code
0             {hint "1st EH added"}
1             {hint "2nd EH added"}
2             {hint "3rd EH added"}
3             {hint "4th EH added"}

Let's say you remove the EH with index 1 from the above list. Now the list of EH's on the unit will look like this:

index         code
0             {hint "1st EH added"}
1             {hint "3rd EH added"}
2             {hint "4th EH added"}

Note that OFP doesn't just remove the one EH, it also changes the index of all the EHs added after that one. So let's say that when you created each EH, you stored their indexes like so:

idx0 = _unit addeventhandler ["hit", {hint "1st EH added"}]
idx1 = _unit addeventhandler ["hit", {hint "2nd EH added"}]
idx2 = _unit addeventhandler ["hit", {hint "3rd EH added"}]
idx3 = _unit addeventhandler ["hit", {hint "4th EH added"}]

When you remove one of the EH's like in the above example, suddenly these variables will NOT the index of the EH they added in, since those EH's changed indexes.


Hopefully that explaination made sense, but the short of it is: you might run into problems if you remove other eventhandlers of the same type from the unit. Or if other scripts do. Or if mods like the ECP do. For that reason I usually only remove eventhandlers shortly after they are added. But as long as you KNOW that other EHs won't be removed, you should be fine.

HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline 456820

  • Contributing Member
  • **
Re:remove the EH
« Reply #5 on: 25 Oct 2005, 10:37:29 »
okay i think i kind of get this i understand how you add the eh but not quite sure how you remove it the second time you do it where you remove the actual eh not the dummy one
cheers for the hlp though ill have another few reads through it see if i understand it any more
thanks