Hey there!
I am using unreals delegate system to create an event manager for my game.
Everything is rigged up and working well, but I have one small issue.
If I call my SubscribeToEvent function in the constructor of my class (a pawn), the bound function is called twice. However, if I call my SubscribeToEvent function in Begin Play, it is called once.
Here is my subscribe function (called in the constructor or begin play)
template < class UserClass >
static FDelegateHandle Subscribe(UserClass * InUserObject, void(UserClass::*InFunc)(class HGEvent* receivedEvent))
{
FEventManagerDelegate Listener;
Listener.BindUObject(InUserObject, InFunc);
return m_Event.Add(Listener);
}
and here is my send event function, called once in beginplay for testing.
void EventManager::SendEvent(HGEvent* givenEvent)
{
m_Event.Broadcast(givenEvent);
}
just in case, here is the function I use for testing:
void APlayerCameraPawn::BeginPlay()
{
Super::BeginPlay();
SubscribeToEvent(this, &APlayerCameraPawn::HandleEvent);
CenterCameraEvent* newEvent = new CenterCameraEvent(FVector(0, 0, 0));
newEvent->Send();
}
However, it is called twice if SubscribeToEvent is called in the constructor instead of begin play.
Hope you can help! cheers,