-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05_CoroUE5FadeOut.cpp
More file actions
104 lines (84 loc) · 3.28 KB
/
05_CoroUE5FadeOut.cpp
File metadata and controls
104 lines (84 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2024 Damian Nowakowski. All rights reserved.
// This is the example of coroutine in Unreal Engine 5.
// For more details check: https://github.com/zompi2/cppcorosample
#include <coroutine>
#include "Kismet/GameplayStatics.h"
// Forward declaration of the Promise so it can be used for a Handle definition
struct CoroPromise;
// Definition of the coroutine Handle using our Promise
struct CoroHandle : std::coroutine_handle<CoroPromise>
{
// Tell the handle to use our Promise
using promise_type = ::CoroPromise;
};
// Definition of the coroutine Promise
struct CoroPromise
{
// Called in order to construct the coroutine Handle
CoroHandle get_return_object() { return { CoroHandle::from_promise(*this) }; }
// Do not suspend when the coroutine starts
std::suspend_never initial_suspend() noexcept { return {}; }
// Do not suspend when the coroutine ends
std::suspend_never final_suspend() noexcept { return {}; }
// Called when co_return is used
void return_void() {}
// Called when exception occurs
void unhandled_exception() {}
};
// Definition of the coroutine Awaiter used for suspending a specific amount of time
class WaitSeconds
{
private:
// Time left to resume
float TimeRemaining;
// Coroutine Handle to resume after time
std::coroutine_handle<CoroPromise> Handle;
// Unreal ticker handle
FTSTicker::FDelegateHandle TickerHandle;
public:
// Awaiter's constructor which stores the amount of time to being suspended
WaitSeconds(float Time) : TimeRemaining(Time) {}
// Called when the coroutine has been resumed
void await_resume() {}
// Ignore suspension if the given time is invalid
bool await_ready() { return TimeRemaining <= 0.f; }
// Called when the coroutine has been suspended using this Awaiter
void await_suspend(std::coroutine_handle<CoroPromise> CoroHandle)
{
// Remember the coroutine Handle
Handle = CoroHandle;
// Start the Unreal ticker
TickerHandle = FTSTicker::GetCoreTicker().AddTicker(TEXT("CoroWaitSeconds"), 0.f, [this](float DeltaTime) -> bool
{
// When the ticker ticks for the desired amount of time...
TimeRemaining -= DeltaTime;
if (TimeRemaining <= 0.f)
{
// ... stop the Unreal ticker and resume the coroutine
FTSTicker::GetCoreTicker().RemoveTicker(TickerHandle);
Handle.resume();
}
return true;
});
};
};
// Definition of the coroutine function which fades out the camera
CoroHandle CoroFadeOut()
{
// Warning, there will be velociraptors: World should be obtained by a World Context Object,
// but just for the example sake we use nasty GWorld.
if (GWorld)
{
APlayerCameraManager* CameraManager = UGameplayStatics::GetPlayerCameraManager(GWorld, 0);
for (int32 Fade = 0; Fade <= 100; Fade += 10)
{
// Because the WaitSeconds can tick between worlds we can't be sure if the Camera Manager
// is valid all the time.
if (IsValid(CameraManager))
{
CameraManager->SetManualCameraFade((float)Fade * .01f, FColor::Black, false);
}
co_await WaitSeconds(.1f);
}
}
}