How to broadcast delegates from static functions? And how to singletons/managers?

Static Delegates are possible, they can work fine :slight_smile:


.h
/* Events for Creation of GameObjects */
DECLARE_MULTICAST_DELEGATE_OneParam(FGameObjectCreated, UBZGame_GameObjectComponent*);

/* Object Creation Delegate Event */
static FGameObjectCreated ObjectCreationDelegate;



.cpp
FGameObjectCreated UBZGame_GOCManager::ObjectCreationDelegate;


Of course, you need to be able to access the class that the Delegate is declared in so that you can bind something to it - and therefore need an instance of that class in the game.



UBZGame_GameInstance::GetInstance(this)->ObjectCreationDelegate.Broadcast(NewTreeObject);


As far as Singletons / Managers are concerned - your best bet is to initialize them as part of the UGameInstance. I used ObjectInitializer to create them in the Constructor:



UBZGame_GameInstance::UBZGame_GameInstance(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	GameObjectManager = ObjectInitializer.CreateDefaultSubobject<UBZGame_GOCManager>(this, TEXT("GameObjectManager"));
	WorldFXManager = ObjectInitializer.CreateDefaultSubobject<UBZGame_WorldFXManager>(this, TEXT("WorldFXManager"));
	RadarManager = ObjectInitializer.CreateDefaultSubobject<UBZGame_RadarManager>(this, TEXT("RadarManager"));
}


5 Likes