Now that we have all of the Flash UI content created and ready to go, it's time to connect it to the C++ layer of CRYENGINE via a game menu system. This system is very basic; however, it is quite powerful when communicating with ActionScript, such as when responding to events (fscommands). All we need is a small class that will listen to all of the events that Flash dispatched to us via fscommands. We will then use this listener mechanic to respond to various events, helping us develop a framework to display our various game menus.
So let's see how this is done:
CGameUIListener.h
that will be used to declare our game's menu system (Flash UI listener). Create a class called CGameUIListener
that publicly inherits from IUIElementEventListener
. This will allow us to receive all events from particular UI elements. The code should look as follows:#pragma once #include <IFlashUI.h> /////////////////////////////////////////////////////////// /// <summary> /// A Game UI Listener Class. Used To Listen For Flash UI Events For Specific UI Elements. /// </summary> /// <seealso cref="T:IUIElementEventListener"/> /////////////////////////////////////////////////////////// class CGameUIListener : public IUIElementEventListener { public: CGameUIListener(); ~CGameUIListener(); ////////////////////IUIElementEventListener////////////// ///////////////////////////////////////////////////////// /// <summary> /// Called Automatically When A UI Element Event Has Occurred. Used To Handle Logic For UI Element /// Events. /// </summary> /// <param name="pSender"> [in,out] The UI Element That Sent The Event. </param> /// <param name="event"> The UI Event That Occurred. </param> /// <param name="args"> The Arguments That Are Tied To The Event. </param> ///////////////////////////////////////////////////////// virtual void OnUIEvent( IUIElement* pSender, const SUIEventDesc& event, const SUIArguments& args ); ////////////////////CGameUIListener///////////////// private: };
OnUIEvent()
method. It should look as follows:#include "stdafx.h" #include "CGameUIListener.h" CGameUIListener::CGameUIListener() { } CGameUIListener::~CGameUIListener() { } ////////////////////IUIEventListener///////////////// void CGameUIListener::OnUIEvent( IUIElement* pSender, const SUIEventDesc& event, const SUIArguments& args ) { //If The Start Game Button Was Clicked, Then We Should Hide The Main Menu And Load The First Map. if( string( event.sDisplayName ) == string( "Start_Game_Button_Clicked" ) ) { } //If The Quit Game Button Was Clicked, Then We Should Hide The Main Menu And Shutdown The Game And Application. else if( string( event.sDisplayName ) == string( "Quit_Game_Button_Clicked" ) ) { } //If The Main Menu Button Was Clicked, Then We Should Hide The End Game Menu And Show The Main Menu. else if( string( event.sDisplayName ) == string( "Main_Menu_Button_Clicked" ) ) { } //If The Quit Button Was Clicked, Then We Should Hide The End Game Menu And Shutdown The Game And Application. else if( string( event.sDisplayName ) == string( "Quit_Button_Clicked" ) ) { } }
#include "stdafx.h" #include "CGameUIListener.h" #include <IGame.h> #include <IGameFramework.h> CGameUIListener::CGameUIListener() { } CGameUIListener::~CGameUIListener() { } ////////////////////IUIEventListener///////////////// void CGameUIListener::OnUIEvent( IUIElement* pSender, const SUIEventDesc& event, const SUIArguments& args ) { //If The Start Game Button Was Clicked, Then We Should Hide The Main Menu And Load The First Map. if( string( event.sDisplayName ) == string( "Start_Game_Button_Clicked" ) ) { gEnv->pFlashUI->GetUIElement( "Main_Menu" )->SetVisible( false ); gEnv->pConsole->ExecuteString( "map Level_1", false, true ); } //If The Quit Game Button Was Clicked, Then We Should Hide The Main Menu And Shutdown The Game And Application. else if( string( event.sDisplayName ) == string( "Quit_Game_Button_Clicked" ) ) { gEnv->pFlashUI->GetUIElement( "Main_Menu" )->SetVisible( false ); gEnv->pGame->GetIGameFramework()->Shutdown(); } //If The Main Menu Button Was Clicked, Then We Should Hide The End Game Menu And Show The Main Menu. else if( string( event.sDisplayName ) == string( "Main_Menu_Button_Clicked" ) ) { gEnv->pFlashUI->GetUIElement( "End_Menu" )->SetVisible( false ); gEnv->pFlashUI->GetUIElement( "Main_Menu" )->SetVisible( true ); } //If The Quit Button Was Clicked, Then We Should Hide The End Game Menu And Shutdown The Game And Application. else if( string( event.sDisplayName ) == string( "Quit_Button_Clicked" ) ) { gEnv->pFlashUI->GetUIElement( "End_Menu" )->SetVisible( false ); gEnv->pGame->GetIGameFramework()->Shutdown(); } }
As you can see, when either of the "quit" buttons are clicked, we tell IGameFramework
to use Shutdown()
. This will shut down the game and close the application.
CGASGame.h
file and add a member variable named m_pGameUIListener
of type CGameUIListener*
to the CGASGame
class:class CGASGame : public IGame, public ISystemEventListener, public IPlatformOS::IPlatformListener, public IGameFrameworkListener { public: .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. private: .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. /// <summary> The Listener For The Games User Interface. </summary> CGameUIListener* m_pGameUIListener; };
Init()
method of the CGASGame
class, and then delete the game menu system in the destructor of the CGASGame
class. The code should look as follows:bool CGASGame::Init( IGameFramework *pFramework ) { .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. //Create The Game's UI Event Listener. m_pGameUIListener = new CGameUIListener(); //Initialization Was Successful. return true; } CGASGame::~CGASGame() { .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. //Delete The Game Menu Listener If It Exists. if( m_pGameUIListener ) delete m_pGameUIListener; m_pGameUIListener = nullptr; //Very Important That This Method Gets Called. Notifies All CRYENGINE Systems That Our Game Has Ended. GetISystem()->SetIGame( nullptr ); }
void CGASGame::OnSystemEvent( ESystemEvent event, UINT_PTR wparam, UINT_PTR lparam ) { switch( event ) { case ESYSTEM_EVENT_GAME_POST_INIT_DONE: { //Register The Game's UI Event Listener And Show The Main Menu. auto pUIElement = gEnv->pFlashUI->GetUIElement( "Main_Menu" ); pUIElement->AddEventListener( m_pGameUIListener, "Game UI Listener" ); pUIElement->SetVisible( true ); gEnv->pFlashUI->GetUIElement( "End_Menu" )->AddEventListener( m_pGameUIListener, "Game UI Listener" ); } break; .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. default: break; } }
18.191.199.227