Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -15,44 +15,46 @@ 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);

GetActivityInstance()->SetTaggedData(UICurrentValueTag, Activations);

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UArcAsyncAction_ListenForEvent> ActivityMessageListener;
};