Home   Help Search Login Register  

Author Topic: Using format to pass additional info to event handler  (Read 2030 times)

0 Members and 1 Guest are viewing this topic.

RonMcAsskick

  • Guest
i am using an ondammaged event handler and have heard that you can use the format ["",etc] command to pass additional information to an eventhandler.  Any clarification on this will be most helpful

Ron

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Using format to pass additional info to event handler
« Reply #1 on: 01 Jun 2004, 00:15:38 »
Lets say you are adding an eventhandler to a unit during a script. Lets say it looks like this:

_unit addeventhandler ["fired", {(_this select 0) setpos [3000,100,0]}]

This eventhandler is just nonsense (it teleports a person after he fires), but go with the example anyway. Lemme separate the actual code that is executed when the eventhandler is fired, from the rest of the code above:

(_this select 0) setpos [3000,100,0]

What if you want to change the value of that array (the place where the person will be teleported to), depending on something else in the script? Lets say you want to do something SORTA like this in a script (only this wouldn't really work):

Code: [Select]
#start
~60
_pos = getpos Vehicle1
_unit addeventhandler ["fired", {(_this select 0) setpos _pos}]
Notice that what I'm trying to do here is to record the position of Vehicle1 at this specific instant in time, and then pass that position to the eventhandler. So it doesn't matter where Vehicle1 is when _unit fires; _unit will still be teleported to wherever Vehicle1 was when the EH was added. Notice that the following WOULD NOT do the same thing:

_unit addeventhandler ["fired", {(_this select 0) setpos getpos Vehicle1}]

Because Vehicle1 could be in a different position when the EH is fired than it was when the EH was added.

------------------------------------------
If you don't yet understand what the heck I'm talking about, then chances are you don't need to use the format command to pass extra info to the EH, and you shouldn't worry about trying to figure it out until you DO need to know it. However, if you have run into situations where you have been like, "damn, I won't be able to do what I want with that EH", then chances are you will be able to use the information I am giving you.
------------------------------------------

Now, let me show you how to get around the problem described above:

Code: [Select]
#start
~60
_pos = getpos Vehicle1
_unit addeventhandler ["fired", format[ {(_this select 0) setpos %1}, _pos] ]

Notice that when you add an eventhandler to a unit, you have to pass it a string, which is then executed as code when the EH fires. Here, the format command is used to dynamically create the string when the script is run. So, instead of you having to type in the string when creating the script:

"unit setpos [0,100,0]"

You can have the string change depending on the code:

format["unit setpos %1", _pos]

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

I have used the above technique in combination with the "removeeventhandler" command, in order to make a fired EH that will only launch ONCE, and then "remove itself", WITHOUT removing ALL of the "fired" EH's on the unit:

Code: [Select]
_idx = _unit addeventhandler ["fired", {<insert code here>}]
_unit addeventhandler ["fired", format [{(_this select 0) removeeventhandler [{fired}, %1]}, _idx]]
Again, if you don't have any clue what I'm saying, then you don't need to know this stuff anyway. But if you do understand, then you can use this technique to do things with EH's that you never thought possible.
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!