Jump to content
Sign in to follow this  
LSValmont

Opening and Closing Dialogs with CBA (addKeyHandler)

Recommended Posts

For the longest time I've been using a Spawn for opening and closing dialogs IE:

Spoiler



[player] spawn {
	disableSerialization;
	waitUntil {sleep 0.1; !isNull(findDisplay 46)};
	(findDisplay 46) displayAddEventHandler ["KeyDown", {
	if(_this select 1 == 0x21) // key F		DIK_F 
	then {


 

 

But I've since been migrating all my dialogs and even scripts to a function based unscheduled environment using both CBA and ACE 3 guidelines. ( https://ace3mod.com/wiki/development/coding-guidelines.html#72-scheduled-vs-unscheduled ).

 

I am currently facing a problem finding the most optimized, simple and  unscheduled way of opening my custom UI by using:

vMenu_keyHandlerID = [33,[false, false, false],vMenuOpen] call CBA_fnc_addKeyHandler; //33 = F key

Instead of:

displayAddEventHandler ["KeyDown",

So here is my whole previous working code:(Executed via: [player] execVM "vScripts\vMenu\vMenuOpen.sqf";)

Spoiler



// Open Radial Menu Script
disableSerialization;
[player] spawn {
	disableSerialization;
	waitUntil {sleep 0.1; !isNull(findDisplay 46)};
	(findDisplay 46) displayAddEventHandler ["KeyDown", {
	if(_this select 1 == 0x21) // key F		DIK_F 
	then {
	_Show_RMenu_ctrl = (_this select 0) displayCtrl 9001;
	if(isNull(_Show_RMenu_ctrl)) then {
	_Show_RMenu_ctrl = (_this select 0) ctrlCreate ["RscText", 9001];
	_Show_RMenu_ctrl ctrlShow false;
	};
	_shown = ctrlShown _Show_RMenu_ctrl;
	(if(_shown)then{
	closeDialog 2;	
	#define IDC_CANCEL 2
	closeDialog IDC_CANCEL;
	private ["_vehicle","_noEscort"];
	_vehicle = cursorObject;
	_noEscort = count attachedObjects player isEqualTo 0;
	if (isNull _vehicle && _noEscort) then {
	0 = createDialog "val_menu_1";
	} else 
	{
		switch (true) do
		{
			case (_vehicle distanceSqr player > 4^2 && _noEscort): {0 = createDialog "val_menu_1"; systemchat "To far"};
			case (!alive _vehicle && _noEscort): {0 = createDialog "val_menu_1"; systemchat "Not Alive"};
			case (!_noEscort): {0 = createDialog "val_menu_2"; systemchat "Has Escort"};
			case (!isPlayer _vehicle && {_vehicle isKindOf "Man"}): {0 = createDialog "val_menu_2"; systemchat "AI"};
			case (isPlayer _vehicle && {_vehicle isKindOf "Man"}): {0 = createDialog "val_menu_2"; systemchat "PLAYER"};
			case (_vehicle isKindOf "LandVehicle") : {0 = createDialog "val_menu_3"; systemchat "LAND VEHICLE"};
			case (_vehicle isKindOf "Car") : {0 = createDialog "val_menu_3"; systemchat "CAR"};
			case (_vehicle isKindOf "Tank") : {0 = createDialog "val_menu_3"; systemchat "TANK"};
			case (_vehicle isKindOf "Motorcycle") : {0 = createDialog "val_menu_3"; systemchat "Motorcycle"};
			case (_vehicle isKindOf "Ship") : {0 = createDialog "val_menu_3"; systemchat "Ship"};
			case (_vehicle isKindOf "Helicopter") : {0 = createDialog "val_menu_3"; systemchat "Helicopter"};
			case (_vehicle isKindOf "StaticWeapon") : {0 = createDialog "val_menu_3"; systemchat "Static Weapon"};
			case (_vehicle isKindOf "Plane") : {0 = createDialog "val_menu_3"; systemchat "Plane"};
			case (_vehicle isKindOf "Building") : {systemchat "Building"};
			case (_vehicle isKindOf "Wreck") : {systemchat "Wreck"};
			case (_vehicle isKindOf "ReammoBox_F") : {systemchat "A BOX"};
			case (_vehicle isKindOf "MineGeneric") : {systemchat "A Mine"};
			default {};
		};
	};
	//hint "vMenu worked!";
	}else{
	//________________ Menu already open ________________
	closeDialog 2;
	#define IDC_CANCEL 2
	closeDialog IDC_CANCEL;
	}); 
	_Show_RMenu_ctrl ctrlShow !_shown;
	};
}];
};


 

 

So what could be a more optimized way of doing this?

 

Like a CBA unscheduled way (more akin to a Event Handler rather than a spawn/ExecVM).

 

PS: Also, since I am using the "F" key for opening the menu, is there a way to auto unbind the previous "F" so that letter is exclusive to opening the menu?

 

PS2: Thanks in advanced guys!

Share this post


Link to post
Share on other sites

Also they have their waitUntilAndExecute function to keep everything sweetly in unscheduled environment. If you just need display 46 and are doing this from a mod and might need this in many scripts I suggest you create a XEHpostinit called after init and call everything from this. Browse the ace 3 github to have an example.

  • Thanks 1

Share this post


Link to post
Share on other sites
1 hour ago, Mr H. said:

Also they have their waitUntilAndExecute function to keep everything sweetly in unscheduled environment. If you just need display 46 and are doing this from a mod and might need this in many scripts I suggest you create a XEHpostinit called after init and call everything from this. Browse the ace 3 github to have an example.

 

Thank you Mr. H.

 

I am opening the dialog just from the mission folder (no mods).

 

I've been searching for examples in the ACE 3 github but their macros make it hard for me to understand anything.

 

Everything I've learned comes from the Overthrow and Dynamic Civil Wars githubs. Both missions are amazingly coded but I cannot find the specific functions that they use for opening their custom menus.

 

What I could really use is an actual example given my particular situation and having the previous working script as a reference (posted of the first post).

Share this post


Link to post
Share on other sites

Ok

In your description.ext:
 

class Extended_PostInit_EventHandlers
{

   class TAG_MyKeyBindsInit
   {
      init = "call compile preProcessFileLineNumbers 'CBAKeys.sqf'";
   };    
	   
};

in CBAKeys.sqf
 

#include "\a3\editor_f\Data\Scripts\dikCodes.h"
["TAG_Your_Mission","TAG_setting_openYourMenu" ,["Open my very cool menu", ""],
{
	call TAG_MyMenu_fnc;

}},{},[DIK_NUMPADPLUS , [true, true, false]]] call CBA_fnc_addKeybind; // CTRL + SHIFT + NUMPAD+ will open your menu change to whatever, will appear in cba options once in game so can be custom set

in wherever you declare your functions:

TAG_MyMenu_fnc = {


	disableSerialization;

	_Show_RMenu_ctrl = (findDisplay 46) displayCtrl 9001;
	if(isNull(_Show_RMenu_ctrl)) then {
	_Show_RMenu_ctrl = (findDisplay 46) ctrlCreate ["RscText", 9001];
	_Show_RMenu_ctrl ctrlShow false;
	};
	_shown = ctrlShown _Show_RMenu_ctrl;
	(if(_shown)then{
	closeDialog 2;	
	#define IDC_CANCEL 2
	closeDialog IDC_CANCEL;
	private ["_vehicle","_noEscort"];
	_vehicle = cursorObject;
	_noEscort = count attachedObjects player isEqualTo 0;
	if (isNull _vehicle && _noEscort) then {
	0 = createDialog "val_menu_1";
	} else 
	{
		switch (true) do
		{
			case (_vehicle distanceSqr player > 4^2 && _noEscort): {0 = createDialog "val_menu_1"; systemchat "To far"};
			case (!alive _vehicle && _noEscort): {0 = createDialog "val_menu_1"; systemchat "Not Alive"};
			case (!_noEscort): {0 = createDialog "val_menu_2"; systemchat "Has Escort"};
			case (!isPlayer _vehicle && {_vehicle isKindOf "Man"}): {0 = createDialog "val_menu_2"; systemchat "AI"};
			case (isPlayer _vehicle && {_vehicle isKindOf "Man"}): {0 = createDialog "val_menu_2"; systemchat "PLAYER"};
			case (_vehicle isKindOf "LandVehicle") : {0 = createDialog "val_menu_3"; systemchat "LAND VEHICLE"};
			case (_vehicle isKindOf "Car") : {0 = createDialog "val_menu_3"; systemchat "CAR"};
			case (_vehicle isKindOf "Tank") : {0 = createDialog "val_menu_3"; systemchat "TANK"};
			case (_vehicle isKindOf "Motorcycle") : {0 = createDialog "val_menu_3"; systemchat "Motorcycle"};
			case (_vehicle isKindOf "Ship") : {0 = createDialog "val_menu_3"; systemchat "Ship"};
			case (_vehicle isKindOf "Helicopter") : {0 = createDialog "val_menu_3"; systemchat "Helicopter"};
			case (_vehicle isKindOf "StaticWeapon") : {0 = createDialog "val_menu_3"; systemchat "Static Weapon"};
			case (_vehicle isKindOf "Plane") : {0 = createDialog "val_menu_3"; systemchat "Plane"};
			case (_vehicle isKindOf "Building") : {systemchat "Building"};
			case (_vehicle isKindOf "Wreck") : {systemchat "Wreck"};
			case (_vehicle isKindOf "ReammoBox_F") : {systemchat "A BOX"};
			case (_vehicle isKindOf "MineGeneric") : {systemchat "A Mine"};
			default {};
		};
	};
	//hint "vMenu worked!";
	}else{
	//________________ Menu already open ________________
	closeDialog 2;
	#define IDC_CANCEL 2
	closeDialog IDC_CANCEL;
	}); 
	_Show_RMenu_ctrl ctrlShow !_shown;
	


};

This is an example, be extra careful if you copy paste 'cause I only very briefly looked and modified your func, might be missing a few }; and some, but you'll get the idea...

  • Thanks 1

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  

×