Hi guys, I’m currently implementing an event manager (which is not in the scene, it’s pure c++) that must call blueprints events / functions.
What seemed the most easy to use was a function pointer on a function created in cpp. The issue with this is that in our project (currently a student) our gamedesigners have to be able to create their own blueprints that will be called by the event manager.
I’m not used to UE, so there’s maybe an issue with what’s next.
With that, I assumed I had to use unreal’s delegate (so it’s blueprintcallable).
Since I cant pass DelegateName as a function parameter, where DelegateName is defined like this :
Code:
DECLARE_DELEGATE(DelegateName);
I assumed I’d finally have to use UDelegateFunction* as a parameter. But, the issue is :
when I do this :
void AManagerInterface::EM_ListenEvent(FString _eventName, UDelegateFunction* _delegate, AActor* _actor)
{
_actor->_delegate();
}
it doesnt compile.
I guess there’s a point I missed (maybe declare the function pointer’s type somewhere or something else).
Full code below (I Hope you dont have to read it to find the issue I currently have)
ManagerInterface.h :
#pragma once
#include "GameFramework/Actor.h"
#include "ManagerInterface.generated.h"
DECLARE_DELEGATE(EventDelegate);
UCLASS()
class ALCATRAZ_API AManagerInterface : public AActor
{
GENERATED_BODY()
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void EndPlay(EEndPlayReason::Type _reason);
public:
AManagerInterface();
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable)
void EM_CallEvent(FString _eventName);
UFUNCTION(BlueprintCallable)
void EM_ListenEvent(FString _eventName, UDelegateFunction* _delegate, AActor* _actor);
UFUNCTION(BlueprintCallable)
void EM_StopListeningEvent(UObject* _pObject);
UFUNCTION(BlueprintCallable)
void ResetManagers();
};
ManagerInterface.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Alcatraz.h"
#include "ManagerInterface.h"
#include "LevelManager/LevelManager.h"
#include "EventManager/EventManager.h"
#include "SaveManager/SaveManager.h"
// Sets default values
AManagerInterface::AManagerInterface()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AManagerInterface::BeginPlay()
{
Super::BeginPlay();
}
void AManagerInterface::EndPlay(EEndPlayReason::Type _reason)
{
ResetManagers();
}
// Called every frame
void AManagerInterface::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
//Call an event with it's name
void AManagerInterface::EM_CallEvent(FString _eventName)
{
Singleton<EventManager>::Instance().CallEvent(_eventName);
}
void AManagerInterface::EM_ListenEvent(FString _eventName, UDelegateFunction* _delegate, AActor* _actor)
{
_delegate();
}
//Indicate that the aactor stops listening. This is mandatory when you destroy an aactor that listen to an event
void AManagerInterface::EM_StopListeningEvent(UObject* _pObject)
{
Singleton<EventManager>::Instance().StopListening(_pObject);
}
//Mandatory on a level reset on on a stop play
void AManagerInterface::ResetManagers()
{
Singleton<EventManager>::Instance().Reset();
Singleton<LevelManager>::Instance().Reset();
}
The interesting function here is ListenEvent
I couldnt fine any help yet, that’s why I post it here. If there’s already a thread about it, I apologize but I didnt found it yet.
Thanks,
Arkhain