Skip to content
Snippets Groups Projects
Commit 94a60516 authored by Simon Oehrl's avatar Simon Oehrl
Browse files

Merge branch 'feature/cpp_event_listener' into 'develop'

C++ event listener

See merge request VR-Group/unreal-development/ndisplayextensions!15
parents d1116151 167e10de
No related branches found
No related tags found
No related merge requests found
#include "ClusterSyncedMethodCaller.h"
#include "IDisplayCluster.h"
void UClusterSyncedMethodCaller::RegisterListener()
{
IDisplayClusterClusterManager* ClusterManager = IDisplayCluster::Get().GetClusterMgr();
if (ClusterManager && !ClusterEventListenerDelegate.IsBound())
{
ClusterEventListenerDelegate = FOnClusterEventListener::CreateUObject(this, &UClusterSyncedMethodCaller::HandleClusterEvent);
ClusterManager->AddClusterEventListener(ClusterEventListenerDelegate);
}
}
void UClusterSyncedMethodCaller::DeregisterListener() {
IDisplayClusterClusterManager* ClusterManager = IDisplayCluster::Get().GetClusterMgr();
if (ClusterManager && ClusterEventListenerDelegate.IsBound())
{
ClusterManager->RemoveClusterEventListener(ClusterEventListenerDelegate);
}
}
void UClusterSyncedMethodCaller::CallMethodSynced(FString UniqueIdentifier, TMap<FString, FString> Parameters)
{
check(SyncedFunctions.Contains(UniqueIdentifier))
IDisplayClusterClusterManager* const Manager = IDisplayCluster::Get().GetClusterMgr();
if (Manager)
{
if (Manager->IsStandalone()) {
//in standalone (e.g., desktop editor play) cluster events are not executed....
if (SyncedFunctions.Contains(UniqueIdentifier)) {
SyncedFunctions.FindChecked(UniqueIdentifier).Execute(Parameters);
}
}
else {
// else create a cluster event to react to
FDisplayClusterClusterEvent cluster_event;
cluster_event.Category = "ClusterSyncedMethodCaller";
cluster_event.Type = "ClusterSyncedMethodCaller";
cluster_event.Name = UniqueIdentifier;
cluster_event.Parameters = Parameters;
Manager->EmitClusterEvent(cluster_event, true);
}
}
}
void UClusterSyncedMethodCaller::RegisterSyncedMethod(FString UniqueIdentifier, FSyncMethod MethodToCall)
{
SyncedFunctions.Add(UniqueIdentifier, MethodToCall);
}
void UClusterSyncedMethodCaller::HandleClusterEvent(const FDisplayClusterClusterEvent & Event)
{
if (Event.Category.Equals("ClusterSyncedMethodCaller") && Event.Type.Equals("ClusterSyncedMethodCaller")) {
if (SyncedFunctions.Contains(Event.Name)) {
SyncedFunctions.FindChecked(Event.Name).Execute(Event.Parameters);
}
}
}
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Cluster/IDisplayClusterClusterManager.h"
#include "Cluster/DisplayClusterClusterEvent.h"
#include "Delegates/Delegate.h"
#include "ClusterSyncedMethodCaller.generated.h"
typedef TMap<FString, FString> FStringMap;
DECLARE_DELEGATE_OneParam(FSyncMethod, FStringMap)
//typedef FSyncMethod::FDelegate FSyncMethodDelegate;
UCLASS()
class DISPLAYCLUSTEREXTENSIONS_API UClusterSyncedMethodCaller : public UObject
{
GENERATED_BODY()
public:
//RegisterListener should be called in BeginPlay()
void RegisterListener();
//DeregisterListener() should be called in EndPlay()
void DeregisterListener();
//This will call a with UniqueIdentifier registered method in a synced manner with given parameters, also works in non-cluster mode.
//Notice that in the cluster case this call is synced between all nodes, but async to the call of this method!
void CallMethodSynced(FString UniqueIdentifier, TMap<FString, FString> Parameters);
//Register a method to be called in a synced manner, UniqueIdentifier should be unique to any call of this method of any object!
void RegisterSyncedMethod(FString UniqueIdentifier, FSyncMethod MethodToCall);
private:
FOnClusterEventListener ClusterEventListenerDelegate;
virtual void HandleClusterEvent(const FDisplayClusterClusterEvent& Event);
TMap<FString, FSyncMethod> SyncedFunctions;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment