diff --git a/Source/EventCenterPlugin/Private/EventSourceActor.cpp b/Source/EventCenterPlugin/Private/EventSourceActor.cpp index 05e9042..a29229b 100644 --- a/Source/EventCenterPlugin/Private/EventSourceActor.cpp +++ b/Source/EventCenterPlugin/Private/EventSourceActor.cpp @@ -4,8 +4,11 @@ DEFINE_LOG_CATEGORY(EventSourceLog); -AEventSourceActor::AEventSourceActor() +AEventSourceActor::AEventSourceActor() : + redis(nullptr) { + UE_LOG(EventSourceLog, Log, TEXT("CTOR()")); + // 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; @@ -21,21 +24,27 @@ AEventSourceActor::~AEventSourceActor() // TODO: Memory leak (somehow placing a BP derivation into the editor causes the below lines to segfault) //delete redis; //redis = nullptr; + /*if (redis != nullptr) { + delete redis; + redis = nullptr; + }*/ } void AEventSourceActor::BeginPlay() { - UE_LOG(EventSourceLog, Log, TEXT("BeginPlay()")); - Super::BeginPlay(); - UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis is null? %b"), redis != nullptr); + UE_LOG(EventSourceLog, Log, TEXT("BeginPlay()")); + + if (redis == nullptr) { + UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Creating redis instance")); + redis = NewObject(this, TEXT("RedisObject")); + } + + UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis is null? ? %d"), redis == nullptr); //UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis host: %s"), redisHost); UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis port: %d"), redisPort); //UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis password (no secrets): %s"), redisPassword); - - //redis->Init(redisHost, redisPort, redisPassword); - } void AEventSourceActor::BeginDestroy() @@ -51,8 +60,39 @@ void AEventSourceActor::Tick(float DeltaTime) //redis->Tick(DeltaTime); } +void AEventSourceActor::ConstructClient() +{ + if (redis != nullptr) { + UE_LOG(EventSourceLog, Log, TEXT("Construct(): Redis is NOT null")); + return; + } + + UE_LOG(EventSourceLog, Log, TEXT("construct(): redis = new Redis()")); + redis = NewObject(this, TEXT("RedisObject")); +} + + +void AEventSourceActor::Init() +{ + if (redis == nullptr) { + UE_LOG(EventSourceLog, Log, TEXT("Init(): Redis is null")); + return; + } + + UE_LOG(EventSourceLog, Log, TEXT("construct(): redis = new Redis()")); + redis = NewObject(this, TEXT("RedisObject")); + UE_LOG(EventSourceLog, Log, TEXT("Init(): redis->Init()")); + redis->Init(redisHost, redisPort, redisPassword); +} + void AEventSourceActor::Subscribe(const FString& Channel) { + if (redis == nullptr) { + UE_LOG(EventSourceLog, Log, TEXT("Subscribe(): Redis is null")); + return; + } + + UE_LOG(EventSourceLog, Log, TEXT("Subscribe(): redis->Subscribe()")); redis->Subscribe(Channel); } diff --git a/Source/EventCenterPlugin/Public/EventSourceActor.h b/Source/EventCenterPlugin/Public/EventSourceActor.h index 0da3807..a4d1ecc 100644 --- a/Source/EventCenterPlugin/Public/EventSourceActor.h +++ b/Source/EventCenterPlugin/Public/EventSourceActor.h @@ -41,6 +41,14 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "EventCenter") FString redisPassword; + /** Construct Redis Object.*/ + UFUNCTION(BlueprintCallable, Category = "EventCenter") + void ConstructClient(); + + /** Connect to Redis.*/ + UFUNCTION(BlueprintCallable, Category = "EventCenter") + void Init(); + /** Subscribe to a named Redis PubSub channel. */ UFUNCTION(BlueprintCallable, Category = "EventCenter") void Subscribe(const FString& Channel);