A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
StreamingLevelManager.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "VRBaseCharacter.h"
5#include "Engine/LevelStreaming.h"
11#include "Kismet/GameplayStatics.h"
12
19
21{
22 static const FString ContextString(TEXT("StreamingLevels"));
23
25 {
26 TArray<FName> RowNames = LevelStreamingDataTable->GetRowNames();
27 if (RowNames.Num() == 0)
28 {
29 UE_LOG(LogTemp, Error, TEXT("LevelStreamingDataTable is empty!"))
30 return TArray<FName>();
31 }
32
33 for (const auto RowName : RowNames)
34 {
35 FStreamingLevel* FoundLevels = LevelStreamingDataTable->FindRow<FStreamingLevel>(RowName, ContextString);
36 if (FoundLevels->StepName.EqualTo(StepName))
37 {
40 {
42 }
43 return FoundLevels->ActiveLevels;
44 }
45 }
46 }
47 else
48 UE_LOG(LogTemp, Error, TEXT("LevelStreamingDataTable is invalid!"));
49
50 return TArray<FName>();
51}
52
54{
55 //Check levelsToLoad array
56 if (levelsToLoad.Num() == 0)
57 {
58 return;
59 }
60 //Reset defaults
61 LevelsToLoad.Empty();
63
64 for (FName level : levelsToLoad)
65 {
66 if (LoadedLevels.Contains(level))
67 {
68 LevelsToUnload.Remove(level);
69 }
70 else
71 {
72 LevelsToLoad.Add(level);
73 }
74 }
75}
76
77void UStreamingLevelManager::LoadStreamingLevels(TArray<FName> levelsToLoad, bool onlyFadeOut, float FadeTime)
78{
79 if (levelsToLoad.Num() != 0)
80 {
81 TimeForFades = FadeTime;
82
83 FilteredArraysToLoadUnload(levelsToLoad);
84 if (LevelsToUnload.Num() != 0)
85 {
87 {
88 auto FadeInFinishedEvent = [this]()
89 {
91 };
92
93 if (!onlyFadeOut)
94 {
95 EventLogService::NewEvent<UFadeInEvent>(nullptr, FadeInFinishedEvent, TimeForFades);
96 }
97 else
98 {
99 EventLogService::NewEvent<UFadeInEvent>(nullptr, FadeInFinishedEvent, 0.0f);
100 }
101 }
102 else
103 {
105 }
106 }
107 else if (LevelsToLoad.Num() != 0)
108 {
110 {
111 auto FadeInFinishedEvent = [this]()
112 {
114 };
115
116 if (!onlyFadeOut)
117 {
118 EventLogService::NewEvent<UFadeInEvent>(nullptr, FadeInFinishedEvent, TimeForFades);
119 }
120 else
121 {
122 EventLogService::NewEvent<UFadeInEvent>(nullptr, FadeInFinishedEvent, 0.0f);
123 }
124 }
125 else
126 {
128 }
129 }
130 LoadedLevels = levelsToLoad;
131 }
132 else
133 UE_LOG(LogTemp, Log, TEXT("Levels to load array is empty!"));
134}
135
136void UStreamingLevelManager::LoadStreamingLevelsAndTeleportPlayer(TArray<FName> Levels, FTransform PlayerTransform, bool OnlyFadeOut, float FadeTime)
137{
139 PlayerNewTransform = PlayerTransform;
140
141 LoadStreamingLevels(Levels, OnlyFadeOut, FadeTime);
142}
143
144void UStreamingLevelManager::SetLevelsVisibility(TArray<FName> Levels, bool Visible)
145{
146 auto SetVisibilityEvent = [this, Visible, Levels]()
147 {
148 for (FName Level : Levels)
149 {
150 UGameplayStatics::GetStreamingLevel(GetWorld(), Level)->SetShouldBeVisible(Visible);
151 }
152 };
153
155 {
156 EventLogService::NewEvent<UFadeInEvent>(nullptr, SetVisibilityEvent, TimeForFades);
157 }
158 else
159 {
160 SetVisibilityEvent();
161 }
162}
163
168
173
175{
176 UnloadingIndex = IndexToUnload;
177 if (IndexToUnload > (LevelsToUnload.Num() - 1))
178 {
179 if (LevelsToLoad.Num() != 0)
180 {
182 }
183 else
184 {
185 EventLogService::NewEvent<UStreamingLevelsActionFinishedEvent>();
186
188 {
189 AVRBaseCharacter* Character = Cast<AVRBaseCharacter>( UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
190
191 if (Character)
192 {
193 Character->SetActorLocationAndRotationVR(PlayerNewTransform.GetLocation(), PlayerNewTransform.Rotator());
194 }
195 }
196
198 {
199 EventLogService::NewEvent<UFadeOutEvent>(nullptr, nullptr, TimeForFades);
200 }
201 }
202
203 return;
204 }
205
206 FLatentActionInfo LatentInfo;
207 LatentInfo.CallbackTarget = this;
208 LatentInfo.ExecutionFunction = "LevelUnloaded";
209 LatentInfo.Linkage = 1;
210 LatentInfo.UUID = IndexToUnload;
211
212 UGameplayStatics::UnloadStreamLevel(this, LevelsToUnload[IndexToUnload], LatentInfo, false);
213}
214
216{
217 LoadingIndex = IndexToLoad;
218 if (IndexToLoad > (LevelsToLoad.Num() - 1))
219 {
220 EventLogService::NewEvent<UStreamingLevelsActionFinishedEvent>();
221
223 {
224 AVRBaseCharacter* Character = Cast<AVRBaseCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
225
226 if (Character)
227 {
228 Character->SetActorLocationAndRotationVR(PlayerNewTransform.GetLocation(), PlayerNewTransform.Rotator());
229 }
230 }
231
233 {
234 EventLogService::NewEvent<UFadeOutEvent>(nullptr, nullptr, TimeForFades);
235 }
236
237 return;
238 }
239
240 FLatentActionInfo LatentInfo;
241 LatentInfo.CallbackTarget = this;
242 LatentInfo.ExecutionFunction = "LevelLoaded";
243 LatentInfo.Linkage = 1;
244 LatentInfo.UUID = IndexToLoad;
245
246 UGameplayStatics::LoadStreamLevel(this, LevelsToLoad[IndexToLoad], true, true, LatentInfo);
247}
248
250{
251 LoadStreamingLevels(GetLevelsBasedOnStep(Event->CurrentStep.StepName));
252}
253
255{
256 LoadStreamingLevels(GetLevelsBasedOnStep(Event->CurrentStep.StepName), true);
257}
#define DEFINE_EVENT_HANDLER(TEventType)
Definition BaseManager.h:13
FVector SetActorLocationAndRotationVR(FVector NewLoc, FRotator NewRot, bool bUseYawOnly=true, bool bAccountForHMDRotation=true, bool bTeleport=false)
UFUNCTION(BlueprintCallable, Category = "BaseVRCharacter|VRLocations")
void HandleEvent_Impl(class UBaseEvent *Event)
Event called when new step is loaded.
Event called when new story is loaded.
void SetLevelsVisibility(TArray< FName > Levels, bool Visible)
UFUNCTION(BlueprintCallable, Category = "LevelStreamingManager")
TArray< FName > LevelsToLoad
Array storing the levels to load.
TArray< FName > LoadedLevels
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "LevelStreamingManager")
TArray< FName > LevelsToUnload
Array storing the levels to unload.
void LoadStreamingLevels(TArray< FName > levelsToLoad, bool onlyFadeOut=false, float FadeTime=0.5f)
Calling due to LevelStreamingDataTable, but you also can call it manually. In addition,...
void LoadStreamingLevelsAndTeleportPlayer(TArray< FName > Levels, FTransform PlayerTransform, bool OnlyFadeOut=false, float FadeTime=0.5f)
Call it manually, if you want to teleport player to specific location after loading levels.
void UnloadStreamingLevels(int32 IndexToUnload)
This function unloads a specific streaming level based on the provided index.
void FilteredArraysToLoadUnload(TArray< FName > levelsToLoad)
This function takes an array of levels to load and filters the arrays LevelsToLoad and LevelsToUnload...
TArray< FName > GetLevelsBasedOnStep(FText StepName)
UFUNCTION(BlueprintCallable, Category = "LevelStreamingManager")
UDataTable * LevelStreamingDataTable
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelStreamingManager")
bool AutoFadeInAndOut
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelStreamingManager")
int32 LoadingIndex
Index for tracking the loading progress.
FTransform PlayerNewTransform
Transform representing the new location for the player after loading levels.
int32 UnloadingIndex
Index for tracking the unloading progress.
float TimeForFades
Time duration for fade effects.
bool TeleportPlayerToSpecificLocation
Determines whether to teleport the player to a specific location after loading levels.
Structure representing streaming levels to load based on step. Also contains info about new player po...
bool TeleportPlayerToSpecificLocation
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray< FName > ActiveLevels
UPROPERTY(EditAnywhere, BlueprintReadOnly)
FText StepName
UPROPERTY(EditAnywhere, BlueprintReadOnly)
FTransform PlayerNewLocation
UPROPERTY(EditAnywhere, BlueprintReadWrite)