Jump to content
Sign in to follow this  
Fuzzy Bandit

Adding an Event Handler to every spawned unit

Recommended Posts

Hello there fellas. I come with another brain-tingling problem.

I'm looking for a way to add an Event Handler (killed, in this particular case) to every unit spawned (via scripts using createVehicle and the like). I'm aware I can do this using config.cpp, but am I right in thinking that config.cpp is only present in addons and therefore I can't add event handlers with that method unless I create an addon?

So, is there a nice easy way that I'm missing or is this a tricky one?

Thanks!

Share this post


Link to post
Share on other sites
_newUnit = _grp createUnit ["B_Soldier_F", [0,0,0], [], 0, "NONE"];
_newUnit addEventHandler ["killed", {}];

http://community.bistudio.com/wiki/addEventHandler

-_- I'm very aware that I can add event handlers using a command, though I'd rather avoid having to add one every single time I create a unit. Is there no way to automate the process?

Share this post


Link to post
Share on other sites

something like

while {true} do 
{
	{
		if (!(_x getVariable ["added_EV",false])) then {
			_x  addEventHandler ["killed", {}];
			_x setVariable ["added_EV",true];
		};
	}forEach allUnits;
sleep 10;
};

Share this post


Link to post
Share on other sites
something like

while {true} do 
{
	{
		if (!(_x getVariable ["added_EV",false])) then {
			_x  addEventHandler ["killed", {}];
			_x setVariable ["added_EV",true];
		};
	}forEach allUnits;
sleep 10;
};

Aye, but that would be rather heavy when dealing with a lot of units, plus it'd miss the rare moment where something dies quite quickly after spawning.

Quoting from http://community.bistudio.com/wiki/ArmA_2:_Event_Handlers#Init...

Triggered on mission start or when a vehicle is created on the fly using createVehicle.

So supposedly this event handler can be used every time createVehicle is used? How do I use this functionality?

Share this post


Link to post
Share on other sites

I don't know if that command even works anymore, nor what the purpose of it was. A guess is that it was before setVehicleInit and publicVariable existed.

I found some old example related to .sqs scripting but nothing else, I've never seen anyone use it so I'd say it's deprecated. As far as I can tell, it was used on the object that was just created.

_guy = createUnit [...]

_guy addEventHandler ["init",{}];

What's the issue of adding the EV to your spawning script?

Edited by cuel

Share this post


Link to post
Share on other sites
What the issue of adding the EV adding to your spawning script?

A good point. I do have a function that handles the mass creation of attacking / defending forces already and that does include adding event handlers to each unit created; that's fine, as I just have to call a function. I will also in various places, however, be using createVehicle to add individual objects here and there for which the same EH should apply.

Maybe that's the ugly way round it, then? Create my own createVehicle function...

_obj = ["B_Heli_Some_Model", [123, 456, 789]] call AW_fnc_createVehicle;

Which calls...

_type = _this select 0;
_pos = _this select 1;

_obj = _type createVehicle _pos;
_obj addEventHandler ["killed" { /* do something fantastic */ }];

_obj

Share this post


Link to post
Share on other sites
Yep. BIS had a function in A2, not sure if it works in A3.

Aye I already use that in some of my spawning functions; it does work in A3. Still messier that I'd like, but I guess it's what I'll have to do!

Share this post


Link to post
Share on other sites

I'm looking for a way to add an Event Handler (killed, in this particular case) to every unit spawned (via scripts using createVehicle and the like).

So, is there a nice easy way that I'm missing or is this a tricky one?

Thanks!

try implementing:

///////////////////////////////////////////////////
// BOMSF Utilities
// fnc_ehkilled.sqf
//
// in init.sqf: [] call compile preprocessfilelinenumbers "scripts\fncs\fnc_ehkilled.sqf";
//
// call the fnc with a periodic while loop or in any script that creates an east unit
//
///////////////////////////////////////////////////
if (isServer or isDedicated) then {
fnc_ehkilledeast = {
	east_units = [];

	{
		if ((side _x) == east) then {
			east_units = east_units + [_x];
			{_x removeAllEventHandlers "killed"} forEach east_units; 
		}; 
	} foreach allUnits;

	{_x addEventHandler ["killed", {//insert code here }]} forEach east_units; 
};
};

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  

×