A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
ExperienceManager.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "GameFramework/PlayerController.h"
5#include "Engine/World.h"
6#include "PixoVRGameState.h"
14
15PRAGMA_DISABLE_OPTIMIZATION
16
21
23{
24 {
25 FRWScopeLock(Lock, SLT_Write);
26
27 for (TSubclassOf<UBaseManager> ListenerClass : SingleListenerClasses)
28 {
29 SingleListeners.Add(NewObject<UBaseManager>(this, ListenerClass));
30 }
31 }
32
33 EventLogService::NewEvent<UInitializationEvent>();
34}
35
37{
38 if (APixoVRGameState* GameState = GetWorld() != nullptr ? GetWorld()->GetGameState<APixoVRGameState>() : nullptr)
39 {
40 GameState->ServerStartGameSession();
41 EventLogService::NewEvent<UStartPlayEvent>();
42 }
43}
44
45void UExperienceManager::JoinExperience(uint8 PlayerIndex, APlayerController* NewPlayer)
46{
47 {
48 FRWScopeLock(Lock, SLT_Write);
49
50 for (TSubclassOf<UBaseManager> ListenerClass : PerPlayerListenerClasses)
51 {
52 PerPlayerListeners.Add(NewManager<UBaseManager>(NewPlayer, ListenerClass));
53 }
54 }
55
56 EventLogService::NewEvent<UPlayerJoinedEvent>(NewPlayer);
57}
58
60{
62}
63
64void UExperienceManager::LeaveExperience(AController* LeavingPlayer, uint8 PlayersLeft)
65{
66 {
67 FRWScopeLock(Lock, SLT_Write);
68
69 PerPlayerListeners.RemoveAllSwap([LeavingPlayer](const UBaseManager* Listener) -> bool
70 {
71 return Listener->GetPrivateOwner() == LeavingPlayer;
72 });
73 }
74 if (PlayersLeft == 0)
75 {
76 EventLogService::NewEvent<UClearExperienceEvent>();
77 }
78}
79
84
85UBaseManager* UExperienceManager::GetManagerRef_Implementation(TSubclassOf<UBaseManager> ManagerClass)
86{
87 for (const auto Manager : SingleListeners)
88 {
89 if (Manager->IsA(ManagerClass))
90 {
91 return Manager;
92 }
93 }
94 return nullptr;
95}
96
100
102{
103 FRWScopeLock(Lock, SLT_ReadOnly);
104
105 TArray<UBaseEvent*> OutEvents;
106 if (EventLogService::Events().GetAll(OutEvents))
107 {
108 for (UBaseEvent* Event : OutEvents)
109 {
112 }
113 }
114}
115
117{
118 if (ensureMsgf(Event != nullptr, TEXT("[UExperienceManager::DispatchEvent] Event is NULL")))
119 {
120 Event->Lock();
121 for (UBaseManager* Listener : SingleListeners)
122 {
123 Listener->HandleEvent(Event);
124 }
125 for (UBaseManager* Listener : PerPlayerListeners)
126 {
127 Listener->HandleEvent(Event);
128 }
129 Event->Unlock();
130 }
131}
132
133PRAGMA_ENABLE_OPTIMIZATION
APixoVRGameState is a custom game state class that extends AGameState.
static EventContainer & Events()
The UBaseEvent class serves as the base class for all events for our event-managers system....
Definition BaseEvent.h:83
It provides functionalities for handling events, sending events, and managing a private owner....
Definition BaseManager.h:32
APlayerController * GetPrivateOwner() const
Definition BaseManager.h:47
virtual void EndExperience()
This function handles any pending events in the game experience by calling the HandleEvents function.
TArray< TSubclassOf< UBaseManager > > PerPlayerListenerClasses
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Classes, DisplayName = MultipleInstanceLis...
virtual void JoinExperience(uint8 PlayerIndex, APlayerController *NewPlayer)
This function adds per-player listeners to the PerPlayerListeners array by creating new manager insta...
virtual void LeaveExperience(AController *LeavingPlayer, uint8 PlayersLeft)
This function removes the per-player listeners associated with the leaving player from the PerPlayerL...
void DispatchEvent(UBaseEvent *Event) const
This function dispatches the specified Event to the listeners. It ensures that the event is not null ...
TArray< UBaseManager * > SingleListeners
UPROPERTY()
virtual UBaseManager * GetManagerRef_Implementation(TSubclassOf< UBaseManager > ManagerClass) override
This function searches for a single-instance listener of the specified ManagerClass in the SingleList...
virtual void HandleEvent(UBaseEvent *Event)
void HandleEvents()
This function is responsible for handling pending events in the game experience. It retrieves the pen...
TArray< UBaseManager * > PerPlayerListeners
UPROPERTY()
TArray< TSubclassOf< UBaseManager > > SingleListenerClasses
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Classes, DisplayName = SingleInstanceListe...
virtual void TickExperience(float DeltaTime)
This function is called every frame to handle events in the game experience. It calls the HandleEvent...
virtual void StartExperience()
This function starts the game experience by calling the ServerStartGameSession function on the APixoV...
virtual void InitExperience()
This function initializes the game experience by creating and adding single-instance listeners to the...