Jump to content
Sign in to follow this  
infiltrator_2k

Mission Lose Event Handler

Recommended Posts

I'd like to create an event handler to detect when Blufor kill three civilians and then end the mission with a 'lose' condition. I've not used event handlers before and at the moment I've created a group and I've grouped all the civilians to the group leader whilst running

Group leader's Init:

civ=group this;

({alive _x} count units civ) < 3 

However, this is very messy as it's cluttering up my workspace. What event handler and code will I have to use to get an event handler to perform the same check. I believe event handlers are better suited for these types checks/triggers as they save on resources - albeit minimal - I'd like to go down the event handler route as it's cleaner.

Cheers Ian

Share this post


Link to post
Share on other sites
I'd like to create an event handler to detect when Blufor kill three civilians and then end the mission with a 'lose' condition.

Group leader's Init:

civ=group this;

({alive _x} count units civ) < 3 

Does that code you have there actually do what you want?

It looks to me like it will trigger when there are 2 or fewer civilians LEFT not 3 killed, unless that is just how the numbers line up?

Anyway:

In the init of any unit that counts if its killed.


0 = this addEventHandler ["killed", {civsKilled = civsKilled + 1;}];
civsKilled = 0;

then set up a trigger (or equivalent) with the condition civsKilled >= 3

EDIT: You could use this in group leader: (This will also count only if its a BLUFOR that killed the civ.)

civ = group this;
civsKilled = 0;
{
0 = _x addEventHandler ["killed", {if (side (_this select 1) == west) then {civsKilled = civsKilled + 1;};}];

} forEach units civ;

Edited by TKTom
I added the blufor conditional

Share this post


Link to post
Share on other sites

Rule#1: If it can be done with an eventHandler, then do it! I'd get comfortable with the various eventhandlers. They make life much easier when they can be applied.

Share this post


Link to post
Share on other sites
That works great thank you. BTW, how long have you been scripting?

Since the release of OFP (2001) one way or another.

My personal progress has been a lot greater in the last few years than at any time before that, however.

Share this post


Link to post
Share on other sites
In the init of any unit that counts if its killed.

0 = this addEventHandler ["killed", {civsKilled = civsKilled + 1;}];
civsKilled = 0;

then set up a trigger (or equivalent) with the condition civsKilled >= 3

This works, but I think it's a good idea to explain why to prevent problems in the future. The "Killed" event handler will only fire on the machine where it was set. Inside the event handler you have a global variable which is also local to the machine it was defined/changed. Don't confuse a global variable with a public variable, public variables are broadcast over the network, global variables are not so each client can have a different value on their machines. Now, all of that stuff is local so how do all of the clients get the same result? When you put something in the unit init line, it will run on every client, that's how.

That's not to say this isn't good code, but it will cause issues with JIP.

Share this post


Link to post
Share on other sites
This works, but I think it's a good idea to explain why to prevent problems in the future. The "Killed" event handler will only fire on the machine where it was set. Inside the event handler you have a global variable which is also local to the machine it was defined/changed. Don't confuse a global variable with a public variable, public variables are broadcast over the network, global variables are not so each client can have a different value on their machines. Now, all of that stuff is local so how do all of the clients get the same result? When you put something in the unit init line, it will run on every client, that's how.

That's not to say this isn't good code, but it will cause issues with JIP.

This is true, he didn't ask for JIP capability though and I thought it was best to keep it simple.

If you want to have this JIP capable, the only real change necessary would be an if (!isServer) exitWith{}; preventing the loop on any machine but the server.

Then the event handlers would only execute on the server, the variable would only be on the server. Either set the trigger to send its effects over the network or publicvariable the "civsKilled" in order to make everyone's triggers activate.

That is:

if (!isServer) exitWith {};

civ = group this;
civsKilled = 0;
{
0 = _x addEventHandler ["killed", {if (side (_this select 1) == west) then {civsKilled = civsKilled + 1;publicVariable "civsKilled";};}];

} forEach units civ;

Edited by TKTom

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  

×