From 80d2714c7bf00154870dd7fea0eb7a2850ba0366 Mon Sep 17 00:00:00 2001 From: Ingarnm <121302342+Ingarnm@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:19:19 +0300 Subject: [PATCH] Replace activity event lstener --- .../Activity/ArcObjectiveTracker_Event.cpp | 44 ++++++++++--------- .../Activity/ArcObjectiveTracker_Event.h | 9 +++- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/ArcActivityExample/Source/ArcActivityExample/Private/Activity/ArcObjectiveTracker_Event.cpp b/ArcActivityExample/Source/ArcActivityExample/Private/Activity/ArcObjectiveTracker_Event.cpp index 9632875..e52c228 100644 --- a/ArcActivityExample/Source/ArcActivityExample/Private/Activity/ArcObjectiveTracker_Event.cpp +++ b/ArcActivityExample/Source/ArcActivityExample/Private/Activity/ArcObjectiveTracker_Event.cpp @@ -2,8 +2,8 @@ #include "Activity/ArcObjectiveTracker_Event.h" -#include "ArcActivityWorldSubsystem.h" #include "ArcActivityInstance.h" +#include "Events/ArcAsyncAction_ListenForEvent.h" void UArcObjectiveTracker_Event::BeginPlay_Implementation() { @@ -15,35 +15,26 @@ void UArcObjectiveTracker_Event::BeginPlay_Implementation() Activations = 0; - if (UArcActivityWorldSubsystem* WorldSubsystem = GetSubsystem()) - { - auto Callback = [this](FGameplayTag Tag, const UScriptStruct* ScriptStruct, const void* Data) - { - if (IsValid(this)) - { - MessageHandler(Tag); - } - }; - ListenerHandle = WorldSubsystem->UNSAFE_DANGER_RegisterListenerInternal(EventTagToListenFor, Callback, EventPayloadStruct, EventMatchType); - - GetActivityInstance()->SetTaggedData(UIMaxValueTag, MaxCount); - GetActivityInstance()->SetTaggedData(UICurrentValueTag, 0); - } + // Create the async action + ActivityMessageListener = UArcAsyncAction_ListenForEvent::ListenForActivityMessage(this, EventTagToListenFor, EventPayloadStruct, EventMatchType); + // Bind delegates + ActivityMessageListener->OnMessageReceived.AddDynamic(this, &ThisClass::MessageHandler); + ActivityMessageListener->Activate(); + + GetActivityInstance()->SetTaggedData(UIMaxValueTag, MaxCount); + GetActivityInstance()->SetTaggedData(UICurrentValueTag, 0); } void UArcObjectiveTracker_Event::EndPlay_Implementation(bool Canceled) { //We don't want to hear completions anymore from this - if (ListenerHandle.IsValid()) - { - ListenerHandle.Unregister(); - } + ClearActivityMessageListener(); GetActivityInstance()->ClearTaggedData(UIMaxValueTag); GetActivityInstance()->ClearTaggedData(UICurrentValueTag); } -void UArcObjectiveTracker_Event::MessageHandler(FGameplayTag Tag) +void UArcObjectiveTracker_Event::MessageHandler(UArcAsyncAction_ListenForEvent* ProxyObject, FGameplayTag ChannelTag) { Activations = FMath::Min(Activations + 1, MaxCount); @@ -51,8 +42,19 @@ void UArcObjectiveTracker_Event::MessageHandler(FGameplayTag Tag) if (Activations >= MaxCount) { - ListenerHandle.Unregister(); + ClearActivityMessageListener(); + MarkSuccess(); return; } } + +void UArcObjectiveTracker_Event::ClearActivityMessageListener() const +{ + //We don't want to hear completions anymore from this + if (ActivityMessageListener) + { + ActivityMessageListener->Cancel(); + ActivityMessageListener->SetReadyToDestroy(); + } +} \ No newline at end of file diff --git a/ArcActivityExample/Source/ArcActivityExample/Public/Activity/ArcObjectiveTracker_Event.h b/ArcActivityExample/Source/ArcActivityExample/Public/Activity/ArcObjectiveTracker_Event.h index 5891ab5..8aefb4b 100644 --- a/ArcActivityExample/Source/ArcActivityExample/Public/Activity/ArcObjectiveTracker_Event.h +++ b/ArcActivityExample/Source/ArcActivityExample/Public/Activity/ArcObjectiveTracker_Event.h @@ -42,5 +42,12 @@ class ARCACTIVITYEXAMPLE_API UArcObjectiveTracker_Event : public UArcActivityTas private: FArcActivityMessageListenerHandle ListenerHandle; - void MessageHandler(FGameplayTag Tag); + + UFUNCTION() + void MessageHandler(UArcAsyncAction_ListenForEvent* ProxyObject,FGameplayTag ChannelTag); + + void ClearActivityMessageListener() const; + + UPROPERTY() + TObjectPtr ActivityMessageListener; };