source-engine/game/shared/GameEventListener.h

70 lines
1.5 KiB
C
Raw Normal View History

2023-10-03 14:23:56 +00:00
//========= Copyright (c) 1996-2006, Valve Corporation, All rights reserved. ============//
2020-04-22 16:56:21 +00:00
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef GAME_EVENT_LISTENER_H
#define GAME_EVENT_LISTENER_H
#ifdef _WIN32
#pragma once
#endif
#include "igameevents.h"
extern IGameEventManager2 *gameeventmanager;
// A safer method than inheriting straight from IGameEventListener2.
// Avoids requiring the user to remove themselves as listeners in
// their deconstructor, and sets the serverside variable based on
// our dll location.
class CGameEventListener : public IGameEventListener2
{
public:
CGameEventListener() : m_bRegisteredForEvents(false)
{
2023-10-03 14:23:56 +00:00
m_nDebugID = EVENT_DEBUG_ID_INIT;
2020-04-22 16:56:21 +00:00
}
~CGameEventListener()
{
2023-10-03 14:23:56 +00:00
m_nDebugID = EVENT_DEBUG_ID_SHUTDOWN;
2020-04-22 16:56:21 +00:00
StopListeningForAllEvents();
}
void ListenForGameEvent( const char *name )
{
m_bRegisteredForEvents = true;
#ifdef CLIENT_DLL
bool bServerSide = false;
#else
bool bServerSide = true;
#endif
2023-10-03 14:23:56 +00:00
gameeventmanager->AddListener( this, name, bServerSide );
2020-04-22 16:56:21 +00:00
}
void StopListeningForAllEvents()
{
// remove me from list
if ( m_bRegisteredForEvents )
{
2023-10-03 14:23:56 +00:00
gameeventmanager->RemoveListener( this );
2020-04-22 16:56:21 +00:00
m_bRegisteredForEvents = false;
}
}
// Intentionally abstract
virtual void FireGameEvent( IGameEvent *event ) = 0;
2023-10-03 14:23:56 +00:00
int m_nDebugID;
virtual int GetEventDebugID( void ) { return m_nDebugID; }
2020-04-22 16:56:21 +00:00
private:
// Have we registered for any events?
bool m_bRegisteredForEvents;
};
#endif