Implementing a game menu system in C++

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:

  1. Create a new header file called 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:
    
    };
  2. Now that we have our listener class declared, lets implement the 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" ) )
      {
      }
    }
  3. As you can see, we now have a game menu system that allows us to respond to various UI events. Now, let's implement the menu display logic. We simply need to show/hide the menus depending on which button is clicked. The code should look as follows:
    #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();
      }
    }

    Note

    Notice how we retrieve the various menus using the GetUIElement() method from the IFlashUI interface. Also, we show/hide menus by simply calling the SetVisible() method of IUIElement.

    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.

  4. Now that our game menu system is created, we need to perform one last step. In order for our game menu system to actually receive UI events, we need to register the UI elements with it. Navigate to the 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;
    };
  5. Next, we need to instantiate and delete our game menu system when the time comes. Instantiate the game menu system in the 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 );
    }
  6. Lastly, we need to register the UI elements to our game menu system and show the main menu when the application starts. The code is as follows:
    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;
      }
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.199.227