Jump to content
Alert23

Remove displayAddEventHandler after x times used

Recommended Posts

Hi armaholic's!

 

i have this KeyUp EventHandler:

ShootRocket = findDisplay 46 displayAddEventHandler ["KeyUp", {  
   		if (_this select 1 == 19 && soldier1 == player) then {
	
				shell1 = createVehicle ["M_Vorona_HE", position car1, [], 0, "CAN_COLLIDE"];
				shell1 setPos (car1 modelToWorld [-0.02,3,0.2]);
				shell1 setdir getdir car1;
				shell1 setvelocity [100 * (sin (getdir car1)), 100 * (cos (getdir car1)), 3]};
}];

now my question is can someone please help me make this script work only 3 times

so after the 3rd time pressing the Key, the displayAddEventHandler will be removed via displayRemoveEventHandler.

thank you for any help.

Share this post


Link to post
Share on other sites

A simple counter should suffice. Script is not tested.

ShootRocket = findDisplay 46 displayAddEventHandler ["KeyUp", { 
	params ["_display", "_key", "_shift", "_ctrl", "_alt"];
	if (_key == 19 && soldier1 == player) then {
		if (isNil "shot_counter_missle") then {shot_counter_missle = 0};
		shell1 = createVehicle ["M_Vorona_HE", position car1, [], 0, "CAN_COLLIDE"];
		shell1 setPos (car1 modelToWorld [-0.02,3,0.2]);
		shell1 setdir getdir car1;
		shell1 setvelocity [100 * (sin (getdir car1)), 100 * (cos (getdir car1)), 3];
		if (shot_counter_missle >= 3) then {
			_display displayRemoveEventHandler ["KeyUp", ShootRocket];
			shot_counter_missle = nil;
			ShootRocket = nil;
		};
  		shot_counter_missle = shot_counter_missle +1;
	};
	false
}];

Also some other thoughts:

  1. Do you need to access the missile outside of this script? Otherwise it would be better to make the shell1 variable local (_shell1).
  2. Also: the global variable names are really simple. It might lead to problems when other scripts are using the same variable names (soldier1, car1, ShootRocket). Use a tag (TAG_soldier1) to prevent accidents.
  3. It is also smart to remove global variables from existence after not needing them anymore. Removing my (shot_counter_missle) variable is neccessary if you want to use the script more than once.
  4. As for the EH itself: Is there any reason you are not using the "KeyDown" EH as it fires as soon as the key is pressed leading to a more "natural" feel?
  5. The return for key handlers should be a boolean. True to overwrite engine behaviour and false to allow it. Returning neither defaults to false.

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
2 hours ago, 7erra said:

A simple counter should suffice. Script is not tested.


ShootRocket = findDisplay 46 displayAddEventHandler ["KeyUp", { 
	params ["_display", "_key", "_shift", "_ctrl", "_alt"];
	if (_key == 19 && soldier1 == player) then {
		if (isNil "shot_counter_missle") then {shot_counter_missle = 0};
		shell1 = createVehicle ["M_Vorona_HE", position car1, [], 0, "CAN_COLLIDE"];
		shell1 setPos (car1 modelToWorld [-0.02,3,0.2]);
		shell1 setdir getdir car1;
		shell1 setvelocity [100 * (sin (getdir car1)), 100 * (cos (getdir car1)), 3];
		if (shot_counter_missle >= 3) then {
			_display displayRemoveEventHandler ["KeyUp", ShootRocket];
			shot_counter_missle = nil;
			ShootRocket = nil;
		};
  		shot_counter_missle = shot_counter_missle +1;
	};
	false
}];

Also some other thoughts:

  1. Do you need to access the missile outside of this script? Otherwise it would be better to make the shell1 variable local (_shell1).
  2. Also: the global variable names are really simple. It might lead to problems when other scripts are using the same variable names (soldier1, car1, ShootRocket). Use a tag (TAG_soldier1) to prevent accidents.
  3. It is also smart to remove global variables from existence after not needing them anymore. Removing my (shot_counter_missle) variable is neccessary if you want to use the script more than once.
  4. As for the EH itself: Is there any reason you are not using the "KeyDown" EH as it fires as soon as the key is pressed leading to a more "natural" feel?
  5. The return for key handlers should be a boolean. True to overwrite engine behaviour and false to allow it. Returning neither defaults to false.

 

Thank you for your help, it does work.

1.yes will do what you recommended.

2.true.

3.rgr.

4.i find KeyUp better because with KeyDown if you keep the key pressed it will fire like FullAuto.

5. okay i understand (i hope so =] )

 

Share this post


Link to post
Share on other sites
2 hours ago, Alert23 said:

5. okay i understand (i hope so =] )

That point is not always important. There are some cases where the script returns true and suddenly you are not able to reload your weapon or similar. I only found that this is possible by searching through BI scripts as it is not documented on the UI EH BIKI site. I'd have to gather some more information before editing the entry with confidence in my knowledge.

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

×