Jump to content
Sign in to follow this  
Duke101

Eventhandler help/shots fired

Recommended Posts

Hello Guys,

Hoping someone may be to help.

I am creating a stealth mission and trying to add a condition that when ex number of shots are fired it will trigger reinforcements on a hunt script.

This is what I have:

Trigger condition:

 shotIsFired 

on act:

Eventhandler

 {_x addEventHandler ["fired", "{something that counts an amount of fired shots, 20 or whatever} forEach units groupname;"]} forEach units groupname; 

I tried some code to count shots, but can't get it to work. Or can someone think of an alternative.

Thanks.

Share this post


Link to post
Share on other sites

Add this to the init.sqf or to any units init:

shotsFired = 0; 

Then add this to any unit:

this addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if (shotsFired > 5) then {hint "CONDITION FULLFILLED!"}}];

Or this to the leader of a group's init:

{_x addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if (shotsFired > 5) then {hint "CONDITION FULLFILLED!"}}]} foreach units (group this);

Change 5 to whatever number of shots would meet the condition and change hint "CONDITION FULLFILLED!" to whatever you want to happen after those shots are fired like a global variable changing from false to true. etc

EDIT: Sorry I just saw that you only need to check whether a single shot is fired in which case:

this addEventHandler ["FIRED",{shotsFired = true}];

Or

{_x addEventHandler ["FIRED",{shotsFired = true}]} foreach units (group this);

You may also want to remove the eventhandlers after a shot is fired:

this addEventHandler ["FIRED",{(_this select 0) removeAllEventHandlers "FIRED";shotsFired = true}];

or

{_x addEventHandler ["FIRED",{(_this select 0) removeAllEventHandlers "FIRED";shotsFired = true}]} forEach units (group this);

Edited by Rejenorst
  • Like 1

Share this post


Link to post
Share on other sites

Thanks for your help Rejenorst. it's appreciated.

You were the first time, it's for multiple shots.

This works great, but how can I call a script when the condition is met? i.e. when 5 shots or whatever are fired? Sorry if I misunderstood that or didn't explain properly.

Share this post


Link to post
Share on other sites

You can execute a script by replacing 'hint "CONDITION FULLFILLED!"' with the script you want for example:

this addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if (shotsFired > 5) then {_null = [] execVM "myscript.sqf"}}];

Share this post


Link to post
Share on other sites
You can execute a script by replacing 'hint "CONDITION FULLFILLED!"' with the script you want for example:

this addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if (shotsFired > 5) then {_null = [] execVM "myscript.sqf"}}];

Thanks. Oh dear I'm dumb sometimes. How would I do that so that so it's on all playable units or players group? Unfortunately it's for an MP coop. So essentially if a group of 8 players collectively fire, lets say 20 shots, the condition is met and a script is called?

I'm trying to adapt what you have kindly shared with something like:

 forEach units groupname 

but can't get my head around it.

Thanks so much for help.

Share this post


Link to post
Share on other sites

{_x addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if ((shotsFired > 5) && (shotsFired < 7)) then {_null = [] execVM "myscript.sqf"}}]} forEach units (group this);

Try that in the group leaders init or any other unit of the group. Only use it once per group though.

I've just noticed a problems as well. This script will continue to be launched every time a unit fires after the 5 shots have been fired. I've edited it so that it will only trigger the script on the 6th shot (hopefully).

Additionally you will want to remove all FIRE eventhandlers in your launched script. So add this inside your script (the one your launching):

{_x removeAllEventHandlers "FIRED"} foreach allUnits;

Edited by Rejenorst

Share this post


Link to post
Share on other sites
{_x addEventHandler ["FIRED",{shotsFired = shotsFired + 1; if ((shotsFired > 5) && (shotsFired < 7)) then {_null = [] execVM "myscript.sqf"}}]} forEach units (group this);

Try that in the group leaders init or any other unit of the group. Only use it once per group though.

I've just noticed a problems as well. This script will continue to be launched every time a unit fires after the 5 shots have been fired. I've edited it so that it will only trigger the script on the 6th shot (hopefully).

Additionally you will want to remove all FIRE eventhandlers in your launched script. So add this inside your script (the one your launching):

{_x removeAllEventHandlers "FIRED"} foreach allUnits;

Works brilliantly! Thanks so much for your help, it really is appreciated. I also learnt something from this and how you solved it. So again, thanks.

This will just add a bit of spice and discipline in a stealth mission, where if you get "trigger happy", then they'll be a cost to it :D

Share this post


Link to post
Share on other sites

I can't believe I didn't see such a fun script to write until late at night. In Rejenorst's example, this will make it so that if any unit fires more than X times, something will happen. While there's nothing wrong with doing it that way, if you wanted to make it so the entire team can only shoot X number of times, then you'll need to go a different route:

initServer.sqf (this will only be run by the server)

totalShots = 0;
JIPplayer = "";
"JIPplayer" addPublicVariableEventHandler
{
(_this select 1) addEventHandler ["Fired",
{
	totalShots = totalShots + 1;
}];
};

waitUntil {totalShots == 20};
[] execVM "myScript.sqf";

init.sqf (put this at the very top, before anything else)

waitUntil {player == player};
JIPplayer = player;
publicVariableServer "JIPplayer";
JIPplayer = nil;

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  

×