Jump to content
Sign in to follow this  
das attorney

Adding eventHandler to joined units

Recommended Posts

I want to be able to add an eventhandler to units that have joined the players unit after the mission has started (like in the excellent Flashpoint Missions).

I was thinking that a way to do it would be to create a global variable which is an array of units in the players group in the init.sqf. Something like this:

ARRAY_UNITS = units (group player);

and then run a script which checks every 5 minutes or so which returns an array of the new units in the players group. The eventHandler would then be added to them:

while {alive player} do
{

    sleep 300;

    _array_current_units = units (group player);

    _array_new_units = _array_current_units - ARRAY_UNITS;

    ARRAY_UNITS = _array_current_units;

    {_x addEventHandler ["fired", {_this spawn MY_SCRIPT}]} forEach _array_new_units;

};

I havent tried it, but it should work. Does anyone know if there's a better way to do this? This seems pretty clunky and also only checks every once in a while (every 5 mins in this example).

I've tried searching the forums for information on this, but I couldn't find anything helpful. I do try and solve all of my scripting problems myself, but I'm stumped on this one.

Thanks

Share this post


Link to post
Share on other sites

Hey Das... do you mean join the players group as in Join In Progress in a multiplayer game.... or join as an existng soldier in the game simply joining the players group at sometime?

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

The latter :)

For example, I could be playing Warfare (online or SP) and purchase a couple of AI units. As soon as they join my group, I want the eventHandler to be added to them.

Share this post


Link to post
Share on other sites

Maybe something along these lines. I did run a little test and it seemed to work fine.

private ["_group","_origunits ","_oldcount","_newcount","_i","_unit"];

_group = _this select 0;

_origunits = units _group;
_oldcount = count _origunits;
_newcount = _oldcount;

while {alive player} do {
_newcount = count units _group;
switch (_newcount > _oldcount) do {
	case true: {
		_oldcount = _newcount;
		for "_i" from 0 to (count (units _group))-1 do {
			_unit = units _group select _i;
			if (not (_unit in _origunits)) then {
				_unit addEventHandler ["fired", {_this spawn MY_SCRIPT}];
			};
			sleep 0.01;
		};
	};
	case false: {
		if (_newcount < _oldcount) then {	//someone has died
			_oldcount = count units _group;
			_newcount = _oldcount;
		};
	};
};
sleep 1;
};

Run it on the group with:-

nul = [_group] execVM "whatever.sqf";

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Thanks for the script Twirly. I'll give it a whirl and let you know how it goes :)

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  

×