Jump to content
jazzraill

if (unit fires a weapon) exitwith {}

Recommended Posts

greetings. im working on a script which has to terminate when unit fires a weapon.. looking everywhere for hours, can't use fired eh because this line is in an eh already.. 

		_EHkilledIdx = _this addEventHandler ["killed", 
			{
			if (((_this select 0)==(_this select 1)) || (_this select 1) == player) exitwith {};
			systemChat format ["%1 was killed by %2 from %3m with %4",(name (_this select 0)),(name (_this select 1)),((_this select 0)distance (_this select 1)),(parseText (getText (configFile >> "CfgWeapons" >> (currentWeapon (_this select 1)) >> "displayname")))];
			_nearestEnemy = (_this select 1) findNearestEnemy (_this select 1);
			_isSafe = (((_this select 1) distance _nearestEnemy) > 35);
			waitUntil {sleep 10;_isSafe};
			(_this select 1) doMove (getPos (_this select 0));
			>> what im trying to do here:: if ((_this select 1) fires a weapon) exitwith {};
			so that below waitUntil doesnt last forever 
			waitUntil {sleep 3;(((_this select 0) distance (_this select 1))< 5) };

 

Share this post


Link to post
Share on other sites

Who says you can't add the event handler in the eh execution?

That'd be like you can't put toast in a toaster and expect just non toasted toast

  • Like 1

Share this post


Link to post
Share on other sites

1 - suspension (waitUntil) is not allowed in the direct code which is triggered by EH. You need to spawn or execVM it:

     myUnit addEventHandler ["killed", { _this spawn {  waituntil { bla bla}; some code  }  }];

2 - you can filter what you want if some conditions on parameters but also added variables like counter;

3 - EH killed is something like... a single event. Not sure it will fire twice on same guy (except after respawn)

4 - on the other hand, EH "fired", you should use it, can be triggered multiple times (each bullet...)

 

what is the expected "final state"?

  • Like 1

Share this post


Link to post
Share on other sites
6 minutes ago, pierremgi said:

 

what im trying to do is :

unit kills another unit -- eh fires

unit moves victims position, --problems occur here

grabs his gun,

moves on

 

after 

waitUntil {sleep 3;(((_this select 0) distance (_this select 1))< 5) };

it goes on like this;

			{(_this select 1) disableAI _x} forEach ["TARGET","MOVE","FSM"];
			(_this select 1) playActionNow "Medic";
			//Sleep 0.2;
			if (((currentWeapon (_this select 0))) in Lvl2Weapons ) then
				{
				(_this select 1) removeWeapon (primaryWeapon(_this select 1));
				//remove magazine here
				(_this select 1) addWeapon (primaryWeapon (_this select 0));
				(_this select 1) addmagazine (getArray(configFile >> "CfgWeapons" >> (primaryWeapon (_this select 1)) >> "magazines") select 0);
				};
			{(_this select 1) enableAI _x} forEach ["TARGET","MOVE","FSM"];
			(_this select 1) domove waypointPosition [group (_this select 1),1];
			(_this select 0) removeAllEventHandlers "killed";
			}];

the problem is if unit gets distracted when moving victims position, and goes somewhere else instead,

 

waitUntil {sleep 3;(((_this select 0) distance (_this select 1))< 5) };

takes forever and script doesnt terminate 

Share this post


Link to post
Share on other sites

a "waituntil slept 3" is not CPU demanding. So there is no trouble waiting for hours. But, you're right, it's always better to make some script ends.

I suggest to add another condition like:

waitUntil {sleep 3;((_this select 0) distance (_this select 1) < 5) or !alive (_this select 1) or ((_this select 0) distance (_this select 1) > 100 ))};

then:

if ((_this select 0) distance (_this select 1)< 5) then {your code};

 

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, pierremgi said:

a "waituntil slept 3" is not CPU demanding. So there is no trouble waiting for hours. But, you're right, it's always better to make some script ends.

I suggest to add another condition like:

 


waitUntil {sleep 3;(((_this select 0) distance (_this select 1))< 5) or !alive (_thisthen: select 1) or  ((_this select 0) distance (_this select 1) > 100 ))};

wow.. never thought of that. thanks man :yay:

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

×