Jump to content
Rithan

Helicopter extraction script spawns one more helicopter each time it is used

Recommended Posts

Hello!
 

I'm trying to make a script that will create a helicopter that will fly to the player, land and transport the player back to base when the player throws orange smoke.

Most of the script is working fine, but if the player wants to extract a second time after getting back to base, two helicopters will spawn. If done one more time a third will spawn etc.

Here is the code that I'm currently using:

player addAction ["Request transport", {[] execVM "Scripts\extract.sqf"}, nil, 0.1, false]; // This is in another file to the rest of the script


systemChat "Mark your position with orange smoke!";
player addItem "SmokeShellOrange";

player addEventHandler ["Fired",
{
	if ((_this select 4) == "SmokeShellOrange") then
	{
		(_this select 6) spawn	//spawn will create 1 extra helo every time smoke is thrown
		{
			waitUntil {sleep 1, (speed _this <= 0) || (!alive _this)};
			_smoke = getPos _this;
			_pickup = createSimpleObject ["Headgear_H_WirelessEarpiece_F", _smoke];
			hideObject _pickup;
			_marker = 1;
			waitUntil { sleep 1, _marker == 1};
			_radius = getPos player;
			_helo = createVehicle ["B_Heli_Transport_01_F", _radius, [], 2500, "FLY"];
			createVehicleCrew _helo;
			systemChat "A helicopter is en-route to extract you!";
			_zone = getPos _pickup;
			_helo move _zone;
			waitUntil {sleep 1, ((_helo distance _zone) < 150)};
			doStop _helo;
			_helo land "GET IN";
			{_helo animateDoor [_x, 1]} forEach ["door_back_L","door_back_R","door_L","door_R"];
			waitUntil {sleep 1, player in _helo};
			{_helo animateDoor [_x, 0]} forEach ["door_back_L","door_back_R","door_L","door_R"];
			_helo move (getMarkerPos "start");
			sleep 11;
			[0, "BLACK", 3] spawn BIS_fnc_fadeEffect;
			sleep 3;
			player setPos (getMarkerPos "service");
			player setDamage 0;
			deleteVehicle _helo;
			skipTime 2;
			[1, "BLACK", 2] spawn BIS_fnc_fadeEffect;
			sleep 2;
			curobj allowDamage true;
			curobj enableSimulation true;
			deleteVehicle curobj;
			deleteVehicle veh;
			[] execVM "startupgui_fnc.sqf";	// Allows the player to set new mission parameters and usually only runs on mission start
		};
	};
}];

Not only does this spawn two helicopters, it also broadcasts the chat message one time for every helicopter that spawns. This leads me to believe that the script is somehow running twice (?).

Can anyone tell me what I am doing wrong and how to fix it?

Share this post


Link to post
Share on other sites

Probably the reason is that the spawning of heli is defined within the addEventHandler. The second time you run the addAction the previous addEventHandler is still active and spaws a consequence a heli. Each time you run the script you generate additional addEventHandler each one spawns heli.

The same for the system chat.

I'm not sure if this is the reason but probably the addEventHandler should be defined only one time.

Try to move the heli createvehicle outside the addEventHandler.

Share this post


Link to post
Share on other sites

Thanks for pointing out that the eventHandler was constantly running and adding a new one each time.

 

I fixed the issue by adding

player removeEventHandler ["Fired", 0];

At the bottom of my current script

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, Rithan said:

Thanks for pointing out that the eventHandler was constantly running and adding a new one each time.

 

I fixed the issue by adding


player removeEventHandler ["Fired", 0];

At the bottom of my current script

That's not the very best solution.

you're removing the first EH "fired" type. That doesn't mean you're removing the good one!. If one day, you're scripting some heavy scenario with some other EH "fired", or even if you're playing with some advanced mods (with this kind of EHs), you can remove  a useful EH and keep the same problem of helo duplication.

 

The solutions are easy:

* If you remove the EH at the end of its own code (inside), you just have to write:

player removeEventHandler ["fired",_thisEventHandler];    (_thisEventHandler is a special variable inside the event code). player can be changed by (_this select 0) but I recommend the use of params like in BIKI example.

Alternate one (and even better):

* if you don't want to rewrite the EH for each addAction, just create a global variable to flag the existence of this code:
  if (isNil "myEH") then {myEH = TRUE; player addEventHandler [...] };
  This way, no double.

It's an example. You can set a variable on any unit:

if (isNil { _someUnit getVariable "myEH"} ) then { _someUnit setVariable ["myEH",true]; ....}

  • Like 2

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

×