Home   Help Search Login Register  

Author Topic: Returning data from a "fired" event handler  (Read 488 times)

0 Members and 1 Guest are viewing this topic.

Rocko Bonaparte

  • Guest
Returning data from a "fired" event handler
« on: 12 Jan 2005, 07:26:41 »
I wrote a function for use with the "fired" event handler that checks if a flare or other kind of signal projectile had been fired.  The function works well, but I can't get it to return a value for the life of me.

Maybe it's something stupid with the function:
Code: [Select]
private ["_shooter", "_weapon", "_muzzle", "_firemode", "_ammo", "_return_val", "_fired_flare"];

_ammo = (_this select 0) select 4;

_fired_flare = "false";

if(_ammo == "Flare" || _ammo == "FlareRed" || _ammo == "JAM_MarkerGrenadeammo") then {
   _fired_flare = "true";
};

_fired_flare

Note, no semicolon on that last line.  I originally added the handler in the "init=" line for the units in the mission.sqs file.  I moved it out into the Init.sqs file.  Assuming I have a player with text=Rescue1, this is what I put in Init.sqs:
Code: [Select]
Rescue1 addEventHandler ["fired", { _marker_fired = [_this] call fired_marker, _marker_shooter=_this select 4 }]When run, _marker_fired never changes state with the function.  I checked with some hint statements and found _fired_flare would be set to "true," and then _marker_flare would subsequently come up as "false."

I read about the "format[]" trick, but it doesn't seem to work.  Specifically, I tried:
Code: [Select]
Rescue1 addEventHandler ["fired", format[{ _marker_fired = [_this] call fired_marker, _marker_shooter=_this select 4 }]]So I'm at the end of my rope here, having blown off 3 hours on this.  What do I do?

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Returning data from a "fired" event handler
« Reply #1 on: 12 Jan 2005, 08:55:07 »
I see a couple potential problems here, but htye might not be it.

First off, I assume that you are loading the file into the variable fired_marker? Sorta like this:

fired_marker = preprocessfile "function.sqf"

If not, then OFP doesn't even know what "fired_marker" is. Next, I see a comma in your fired EH code, where there should be a semicolon. Is that how it is in your mission?

One big thing that I notice is that you are using local variables in your eventhandler, instead of global ones. So unless there is more code in your eventhandler that you haven't posted, then all that happens is a variable is created, then destroyed without being used for anything.

And last, I have no idea what you are trying to do with the format command. I assume you don't know what you are trying to do with it either, so I suggest you stop trying to get it to work that way ;)

That is as much as I can help without knowing what you are trying to do. A little more explaination on that and maybe I can help you more :)
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!

Rocko Bonaparte

  • Guest
Re:Returning data from a "fired" event handler
« Reply #2 on: 12 Jan 2005, 17:37:25 »
Re: format, I was trying to follow your own advice out of desperation http://www.ofpec.com/yabbse/index.php?board=8;action=display;threadid=17724

I had the thought that it wasn't dynamically assigning anything in the string making up the EH code.  I used commas in the addEventHandler line because the code is in Init.sqs.  I will try semicolons this evening.

The fired_marker function has been pre-processed.  I do know it gets called.  The other variables are assigned ahead of time to defaults in the Init.sqs.  

What I'm trying to do is discover if somebody has fired a flare or laid a smoke signal.  The code I have at the moment has some conceptual bugs: the code checking the "marker_fired" variable runs every 5 seconds.  If something else was shot in that time, it could change.  I want to get it working minimally before I fix that.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Returning data from a "fired" event handler
« Reply #3 on: 12 Jan 2005, 20:17:33 »
How are you testing to see if the function returns a value? Are you adding a hint into the eventhandler, or in a script outside the EH? Because I'm sure the function is working and returning a value just fine. However, you are putting that value into a LOCAL variable inside the EH. That local variable can only be used/seen inside of that EH, so outside script will not be able to detect it. Your outside script is likely creating it's own local variables, which are then never changed, because you think the fired EH will change them. If you put an underscore in front of a variable name, it will be a local variable.

You need to use a GLOBAL variable in this case, because those variables can be seen/changed by ALL scripts. To make a global variable, just start its name with a letter instead of an underscore. Eg:

_marker_fired -> LOCAL variable visible/usable only within the script or codestring (the string passed to an eventhandler) that it was declared in. Other scripts can have local vars with the same name, but different values.

marker_fired -> GLOBAL variable visible/usable by any script or codestring. Changing the value in a script will change the value in every other script as well.
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!

Rocko Bonaparte

  • Guest
Re:Returning data from a "fired" event handler
« Reply #4 on: 13 Jan 2005, 00:20:15 »
This local/global confusion is probably the problem.  I will try that first thing I get to it, but I'm imagining that's about it.  That's a detail I never had explained to me about the scripting language; I only recently ran into globals when trying to get handles to groups in the mission.sqs.