OFPEC Forum
Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting General => Topic started by: F2kSel on 22 Jul 2009, 17:55:18
-
I'm trying to check if an object has been hit using and eventhandler, I can get it to Display a hit using a hint but I need it to set a variable and get an actual result.
_rtarget addEventHandler ["hit", {hint "hit"}];
or something like is as far as I can get, I'm not at my right PC will display the hit but how can I use it to register it in a variable.
I've also tried
eh =_rtarget addEventHandler ["hit", 0];
but that only returns -1 all the time hit or no hit.
Anyone Cheers.
-
Try digesting the Eventhandlers information from this (http://www.ofpec.com/COMREF/index.php?action=read&id=72) page.
And the addEventhandler (http://www.ofpec.com/COMREF/index.php?action=details&id=6&game=All) command from the comref.
Planck
-
You'll be better served to have the desired event result in this fashion (which you already used):
UnitName addEventHandler ["hit", {HitVariable = HitVariable + 1}];
Or you could farm the whole job out to a specially made script:
UnitName addEventHandler ["hit", {nul = this execVM "HitEventScriptname.sqf"}];
As you will see by reading this reference:
http://community.bistudio.com/wiki/ArmA:_Event_Handlers#Hit (http://community.bistudio.com/wiki/ArmA:_Event_Handlers#Hit)
the "this" variable contains these informational nuggets:
[unit, causedBy, damage]
so you can access these nuggets in your script by starting it like this (you probably already know this but I include it for completion):
HitEventScriptname.sqf
_thisUnit - _this select 0;
_thisShooterUnit - _this select 1;
_thisInjuryLevel = _this select 2;
...rest of code...
hope that starts you off :)
-
While we're on the subject: If I use a Detected by trigger, can I get it to return who was detected and who saw the culprit respectively? Somethng along these lines...
_detected = _this select 0
_detector = _this select 1
I have no testingopportunities right now. I read Igor Drukovs EventHandler tutorial from the Ed Depot but failed to come up with an answer. Does anybody know?
:dunno:
-
Thanks for the input guys, I don't have time today to fire up Arma2.
I'm still a bit puzzled though
UnitName addEventHandler ["hit", {HitVariable = HitVariable + 1}];
That's what I tried first and the Hitvariable won't increment .
The only difference was it had the underscore at the front ie "_HitVariable" I'm also using it in an sqf script if that makes a difference. The "_HitVariable" worked perfectly outside the eventhandler so I know that part works.
I also tried my own version of calling a second script without success but I didn't know how the array worked.
I'm also not sure how I would get the variable back into the first script where I need it.
Using the two script version all I would need an _HitVariable = _HitVariable + 1
and then be able to send the result back. But wouldn't this cause an error as _HitVariable isn't defined in the second scrip?
If I do define it _HitVariable=0 it's always going to 1 on exit as it's always getting reset, I think that's where I got stuck last time.
Sorry if I don't make much sense.
-
If i understand you want to obtain information about something thats been hit,
Maybe try this
This addeventhandler ["Hit",{ whos = _this select 0; who = this select 1; hint format [" %1has been hit and was hit by %2",whos,who]}];
untested syntax but should return who was hit and who hit them or you can try with a damage it will return who was hit and where ;
This addeventhandler ["Dammaged",{ whos = _this select 0; where = this select 1; hint format [" %1 has been hit and was hit in the %2",whos,where]}];
not at my arma pc at mo but any probs i will try to support later. it maybe Damaged not Dammaged can never remeber if it changed in arma1
Edit totatlly misnderstood your post , bu i will leave this here for future reference anyw. seems a combination of DMarkwick and worldeaters solutions are what you need. good luck
-
UnitName addEventHandler ["hit", {HitVariable = HitVariable + 1}];
That's what I tried first and the Hitvariable won't increment .
The problem is most probably that you did not initialize HitVariable. So it defaults to nil and no matter what you add to nil the result is always nil.
The solution is simple. Set HitVariable to zero in the init.sqf:
HitVariable = 0;
-
Ok, thanks all.
I'll have another bash tonight and have a few things to tryout.
-
Ah yeah sorry, when I wrote "HitVariable" I meant any variable that you have earmarked for the task :) like when I write "UnitName" etc it just means any variable/name that you have decided to use.
-
Thanks for the help hitvar = hitvar+1 did work, when I tried it previously I used _hitvar = _hitvar+1.
Using the underscore keeps the variable from outputting the info.
Cheers.
-
hitvar would be a global variable and will work fine in init fields of triggers and waypoints ... etc.
_hitvar is a local variable and is limited in scope to the script it is used in, it won't work in init fields of triggers and waypoints ... etc.
Planck
-
If I can ask one more question, at the start of some eventhandlers I see something like this
_eh = _unit addeventhandler ["hit", {Hits= Hits+ 1;}];
I don't know what the "_eh" does, does anyone else?
-
_eh will contain the returned value from the command, in this case it is the index number of the added eventhandler which you can use to remove the eventhandler later in the script, should you wish to do so.
For example:
_unit removeEventhandler ["hit", _eh];
Planck
-
Cheers that makes sense.