Jump to content
Sign in to follow this  
dreadedentity

How exactly do I use event handlers?

Recommended Posts

I have a piece of code uses event handlers to check if a squad has been killed, for performance. However, it seems that I have no idea how to use them. I wrote up a small test script to try to understand more about them, but I'm just not getting it. Can somebody explain to me how to use these please? (btw, if you are going to test that code in a map those coordinates are just above and to the left of the 'a' in 'airfield' on stratis island.

unitArray = createGroup west;

missionNamespace setVariable ["1", group1 createUnit ["B_Soldier_F", [1690,5575,0], [], 0, "NONE"]] addEventHandler ["killed", hint "you killed the first unit."];
//originally, I had the addEventHandler's underneath the createUnit but they don't work. somehow I merged them with createUnit without getting a script error but they still don't work.
//(unitArray select 1) addEventHandler ["killed", {[this] joinSilent grpNull;}];

missionNamespace setVariable ["2", group1 createUnit ["B_Soldier_F", [1690,5575,0], [], 0, "NONE"]] addEventHandler ["killed", {[this] joinSilent grpNull;}];
//(unitArray select 2) addEventHandler ["killed", {[this] joinSilent grpNull;}]; //commented this out because for some reason it throws
//"Error zero divisor". When I researched the issue, I saw that the most relevant reason this error is thrown is if you try to access an
//array index that doesn't exist. But this one does exist so I have no idea why an error gets thrown here.

while {true} do 
{
hintSilent format["%1", units group1];
[(units group1) select 1] joinSilent grpNull; //this works, the units get taken out of my squad correctly.
};

The hint is shown as soon as I preview. I assume normal function of hint is supposed to go to all players, however, the unit was not killed so I don't understand why it's run. For the second unit, I assume that code is not run in the local scope of the unit, explaining why "this" doesn't work. I find that unnecessarily complicated and ridiculous.

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

I'm not sure if the code below is what you are looking for so may I suggest that you give it a try in A3 Editor Debug Console.

player setCaptive true;

_sideArray = [WEST, "West", "BLU_F"];
_grpType = ["Infantry"];
_infGrp = ["BUS_InfTeam","BUS_ReconSentry","BUS_InfTeam_AA","BUS_InfSquad"] call BIS_fnc_selectRandom;


_grp = [getpos player, (_sideArray select 0), (configFile >> "CfgGroups" >> (_sideArray select 1) >> (_sideArray select 2) >> (_grpType select 0) >> _infGrp)] call BIS_fnc_SpawnGroup;
{
_x addeventhandler ["killed", {

Hint "You killed a unit, he will be deleted after 10 sec";
(_this select 0) spawn {sleep 10; deleteVehicle _this; Hint "";}

}];

} foreach units _grp;

What the code does: It will spawn an infantry group at your current location. Kill a member of the group and a hint will show up. After 10sec the dead unit will be deleted.

Share this post


Link to post
Share on other sites

Thanks for the help, I just got my code to work. What I ended up doing was writing a custom spawn function and calling that within my script.

FNC_createWithEH =
{
//taken parameters [unit, position, group]
_object = (_this select 2) createUnit [(_this select 0), (_this select 1), [], 0, "NONE"];
_object addEventHandler ["killed", {[_this select 0] joinSilent grpNull;}];

_object //returns object
};

This will take 3 parameters (just the parameters that are required to spawn units), create a unit, then apply the "killed" event handler to it. If the unit gets killed, it leaves the group. The function returns the object so you can name it whatever you want.

An example of its use would be something like:

_group = createGroup west;
_number = 5
for "_i" from 1 to _number do
{
missionNamespace setVariable [format["%1", _i], ["B_Soldier_F", position player, _group] call FNC_createWithEH];
};

Edited by DreadedEntity

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  

×