Jump to content
Sign in to follow this  
rangerrambo

displayRemoveAllEventHandlers displayRemoveEventHandler displayAddEventHandler

Recommended Posts

Alrite, so step up in the scripting I need to modify a spectating script by Heinblood (from turning into seagul functionality to move up to object, use the action and spectate, then hit esc functionality)

His code, slightly modified by me and not bothered by all details

//gdtmod_seagull.sqf
//by HeinBloed
//http://www.gdt-server.net/
//
//===========================
private["_keydown_Help", "_keydown_NightVision", "_keydown_camconstruct", "_keydown_switchCamera", "_mousez", "_playableUnits", "_z"];

if (typeof player == "Citizen1") then{ 
}else{ 
[true] call acre_api_fnc_setSpectator;
};

sandi_spectating = true; 

gdtmod_seagull_nvg = false;
gdtmod_seagull_camera_on = true;
gdtmod_seagull_camera_help = false;

gdtmod_seagull_players = [];
{if (alive _x && _x != player) then {gdtmod_seagull_players = gdtmod_seagull_players + [_x]; }} foreach allUnits;
gdtmod_seagull_players_count = count gdtmod_seagull_players - 1;

gdtmod_seagull_target = hill;
gdtmod_seagull_players_select = 0;

gdtspec_on = true;


cutText ["GDT SPECTATOR\n----------------------------------------------------------------------------------------------\n\nPress your help button (Standard: H) for instructions","PLAIN"];
setAperture -1;
sleep 1;
gdtmod_seagull_camera = "camconstruct" camcreate [getPos hill select 0, (getPos hill select 1), (getPos hill select 2)];
gdtmod_seagull_camera setdir (getdir hill);	
gdtmod_seagull_camera camConstuctionSetParams [getPos hill, 20000, 10000];
gdtmod_seagull_camera cameraeffect ["internal","back"];
titleText ["", "BLACK IN", 0.2];

_keydown_help = (finddisplay 46) displayaddeventhandler ["keydown", "
if ((_this select 1) in [35]) then {
	if (not gdtmod_seagull_camera_help) then {
		cutText ['Mousewheel:                     Camera jumps to player positions\nLeft mouse button:                                     Freelock\nRight mouse button:                     Camera locked on Player\nMovement keys and mouse:                          Move camera\n\nQ:                                              Move camera up\nY or Z:                                      Move camera down\nSHIFT Left:                                      Slow movement\n\nNightvision key:                              Nightvision On/Off\n\nWhile the camera is locked on a player you can change between\n1st/3rd person, optics and group view.\nPress the help button to exit the help screen.', 'BLACK OUT'];
		gdtmod_seagull_camera_help = true;
	}
	else {
		cutText ['Mousewheel:                     Camera jumps to player positions\nLeft mouse button:                                     Freelock\nRight mouse button:                     Camera locked on Player\nMovement keys and mouse:                          Move camera\n\nQ:                                              Move camera up\nY or Z:                                      Move camera down\nSHIFT Left:                                      Slow movement\n\nNightvision key:                              Nightvision On/Off\n\nWhile the camera is locked on a player you can change between\n1st/3rd person, optics and group view.\nPress the help button to exit the help screen.', 'BLACK IN'];
		gdtmod_seagull_camera_help = false;
	};
};
"];
_keydown_nightVision = (finddisplay 46) displayaddeventhandler ["keydown", "
if ((_this select 1) in (actionkeys 'NightVision')) then {
	if (gdtmod_seagull_nvg) then {
	  camUseNVG false;
		gdtmod_seagull_nvg = false;
	}	else {
	  camUseNVG true;
		gdtmod_seagull_nvg = true;
	};
 };
"];
_keydown_camConstruct = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
if (((_this select 1) == 0) and (not gdtmod_seagull_camera_on)) then {
	gdtmod_seagull_camera cameraeffect ['internal', 'back'];
	gdtmod_seagull_camera setpos [((position gdtmod_seagull_target) select 0) + ((sin (getdir gdtmod_seagull_target)) * ( - 10)), ((position gdtmod_seagull_target) select 1) + ((cos (getdir gdtmod_seagull_target)) * (- 10)), ((position gdtmod_seagull_target) select 2) + 2];
	gdtmod_seagull_camera setdir getdir gdtmod_seagull_target;		
	gdtmod_seagull_camera_on = true;
 };
"];
_keydown_switchCamera = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
if (((_this select 1) == 1) and gdtmod_seagull_camera_on) then {
	if (not (alive (gdtmod_seagull_players select gdtmod_seagull_players_select)) || (gdtmod_seagull_players select gdtmod_seagull_players_select) distance specPen < 200) then {
		while {not (alive (gdtmod_seagull_players select gdtmod_seagull_players_select)) || (gdtmod_seagull_players select gdtmod_seagull_players_select) distance specPen < 200} do {
			gdtmod_seagull_players_select = gdtmod_seagull_players_select + 1;
			if (gdtmod_seagull_players_select > gdtmod_seagull_players_count) then {gdtmod_seagull_players_select = 0};
		};
		gdtmod_seagull_target = vehicle (gdtmod_seagull_players select gdtmod_seagull_players_select);
	};
	gdtmod_seagull_camera cameraeffect ['Terminate', 'back'];
	cutText [format['                   %1', name gdtmod_seagull_target],'PLAIN DOWN'];
	gdtmod_seagull_target switchCamera 'EXTERNAL';
	gdtmod_seagull_camera_on = false;
	gdtmod_seagull_camera_help = false;
 };
"];
_keydown_mouseZ = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
if (count gdtmod_seagull_players > 0) then {
	_z = _this select 1;
	if (_z  < 0) then {
		gdtmod_seagull_players_select = gdtmod_seagull_players_select - 1;
		if (gdtmod_seagull_players_select < 0) then {gdtmod_seagull_players_select = gdtmod_seagull_players_count};
		while {not (alive (gdtmod_seagull_players select gdtmod_seagull_players_select)) || (gdtmod_seagull_players select gdtmod_seagull_players_select) distance specPen < 200} do {
			gdtmod_seagull_players_select = gdtmod_seagull_players_select - 1;
			if (gdtmod_seagull_players_select < 0) then {gdtmod_seagull_players_select = gdtmod_seagull_players_count};
		};
		gdtmod_seagull_target = vehicle (gdtmod_seagull_players select gdtmod_seagull_players_select);		
	}
	else {
		gdtmod_seagull_players_select = gdtmod_seagull_players_select + 1;
		if (gdtmod_seagull_players_select > gdtmod_seagull_players_count) then {gdtmod_seagull_players_select = 0};
		while {not (alive (gdtmod_seagull_players select gdtmod_seagull_players_select)) || (gdtmod_seagull_players select gdtmod_seagull_players_select) distance specPen < 200} do {
			gdtmod_seagull_players_select = gdtmod_seagull_players_select + 1;
			if (gdtmod_seagull_players_select > gdtmod_seagull_players_count) then {gdtmod_seagull_players_select = 0};
		};
		gdtmod_seagull_target = vehicle (gdtmod_seagull_players select gdtmod_seagull_players_select);
	};
	cutText [format['                   %1', name gdtmod_seagull_target], 'PLAIN DOWN'];
	if (gdtmod_seagull_camera_on) then {
	  gdtmod_seagull_camera setpos [((position gdtmod_seagull_target) select 0) + ((sin (getdir gdtmod_seagull_target)) * ( - 10)), ((position gdtmod_seagull_target) select 1) + ((cos (getdir gdtmod_seagull_target)) * (- 10)), ((position gdtmod_seagull_target) select 2) + 2];
	  gdtmod_seagull_camera setdir getdir gdtmod_seagull_target;
	}	else {
	  gdtmod_seagull_target switchCamera 'EXTERNAL';
	};
	gdtmod_seagull_camera_help = false;
};
"];
_keydown_esc = (finddisplay 46) displayaddeventhandler ["keydown", "
if ((_this select 1) in [1]) then {
	gdtmod_seagull_camera cameraeffect [""terminate"",""back""];
	(finddisplay 46) displayRemoveAllEventHandlers ""KeyDown"";
};
"];



while {true} do {

gdtmod_seagull_players = [];
{if ((alive _x) && (_x != player) && (isPlayer _x) && (vehiclevarname _x != "camman")) then {gdtmod_seagull_players = gdtmod_seagull_players + [_x]; }} foreach allUnits;
gdtmod_seagull_players_count = count gdtmod_seagull_players - 1;

 sleep 0.25;
};

What I did there is added for player the ability to press escape to get back out from the spectating script (which works), the problem was that all added display event handlers would work, which is not what we want, we want to remove them.

_keydown_esc = (finddisplay 46) displayaddeventhandler ["keydown", "
if ((_this select 1) in [1]) then {  
	gdtmod_seagull_camera cameraeffect [""terminate"",""back""];
	(finddisplay 46) displayRemoveAllEventHandlers ""KeyDown"";
};
"];

so I added

(finddisplay 46) displayRemoveAllEventHandlers ""KeyDown"";

But that just crashes the game. What to do?

http://community.bistudio.com/wiki/displayAddEventHandler

http://community.bistudio.com/wiki/displayRemoveAllEventHandlers

http://community.bistudio.com/wiki/displayRemoveEventHandler

Edited by rangerrambo

Share this post


Link to post
Share on other sites

why are you using all those ""double"" qoutes?

unless the code is inside "qoutes" just use "regular". also in some cases ' works better than ""

Share this post


Link to post
Share on other sites

Ok cool didnt know about '. Thought you have to use "" when code is inside "whatever" and it needs another "".

_keydown_esc = (finddisplay 46) displayaddeventhandler ["keydown", " whatever 
"];

	if ((_this select 1) in [1]) then {  
	gdtmod_seagull_camera cameraeffect [""terminate"",""back""];
	(finddisplay 46) displayRemoveAllEventHandlers ""KeyDown"";
};

---------- Post added at 11:39 PM ---------- Previous post was at 11:18 PM ----------

Weird

		(finddisplay 46) displayRemoveAllEventHandlers 'MouseButtonDown';
	(finddisplay 46) displayRemoveAllEventHandlers 'mousezchanged';

arent causing any crash just the

(finddisplay 46) displayRemoveAllEventHandlers ""KeyDown"";

On are unrelated topic I have 2 questions that might be important.

1. I am calling the script from a object like so:

this addAction ["Spectate", "rscripts\gdtmod_spectator.sqf"];

What is the difference between that and

this addAction ["Spectate", "[] execVM rscripts\gdtmod_spectator.sqf"];

2. Would it be wise to somehow terminate that script once player is no longer spectating? With http://community.bistudio.com/wiki/terminate?

Can you run the terminate command within the script to terminate itself?

Edited by rangerrambo

Share this post


Link to post
Share on other sites

please send the crash files to dwarden or create a CIT ticket

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  

×