Agrarsense
Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ATickManager Class Reference

#include <TickManager.h>

Inheritance diagram for ATickManager:
Inheritance graph
[legend]
Collaboration diagram for ATickManager:
Collaboration graph
[legend]

Static Public Member Functions

static FTickEntry AddTick (UObject *Object, std::function< void(float)> Function, ETickType Type)
 
static void RemoveTick (FTickEntry TickEntry)
 

Private Member Functions

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
void PreTick (UWorld *World, ELevelTick TickType, float DeltaSeconds)
 
void PostTick (UWorld *World, ELevelTick TickType, float DeltaSeconds)
 
void FrameCleanup ()
 
void TickFunctionsParallel (std::vector< FTickEntry > &functions, const float DeltaTime)
 

Static Private Member Functions

static void AddTickInternal (std::vector< FTickEntry > &to, const FTickEntry &newTickEntry)
 
static void RemoveTickInternal (std::vector< FTickEntry > &from, FTickEntry tickEntry)
 

Private Attributes

FDelegateHandle OnPreTickDelegate
 
FDelegateHandle OnPostTickDelegate
 
FGraphEventRef FullFrameTaskBeforeLateTick
 
FGraphEventRef FullFrameTask
 

Static Private Attributes

static ATickManagerInstance = nullptr
 
static std::vector< FTickEntryFullFrameTaskFunctions
 
static std::vector< FTickEntryFullFrameTaskBeforeLateTickFunctions
 
static std::vector< FTickEntryEarlyTickFunctions
 
static std::vector< FTickEntryLateTickFunctions
 
static std::vector< FTickEntryTicksToRemove
 

Detailed Description

ATickManager is an actor class responsible for ticking of sensor actors (or any added Actor) in parallel. How this works:

– Start of a new frame –

1) OnWorldPreActorTick (PreTick):

2) Regular AActor::Tick occurs.

3) OnWorldPostActorTick (PostTick):

– End of the frame –

Usage (Note: "MyActor::MyFunction" must take a float parameter): FTickEntry entry = ATickManager::AddTick(this, &MyActor::MyFunction, ETickType::FullFrameTaskParallel); ATickManager::RemoveTick(entry);

Definition at line 101 of file TickManager.h.

Member Function Documentation

◆ AddTick()

FTickEntry ATickManager::AddTick ( UObject *  Object,
std::function< void(float)>  Function,
ETickType  Type 
)
static

Adds a tick function to the ATickManager.

Parameters
TickFunctionThe tick function to be added. It should take a single float parameter representing DeltaTime.
TypeThe type of tick to which the function should be added. Valid types are:
  • ETickType::FullFrameTaskParallel: The function will be executed during FullFrameTaskParallel tick.
  • ETickType::FullFrameTaskBeforeLateTickParallel: The function will be executed during FullFrameTaskBeforeLateTickParallel tick.
  • ETickType::EarlyTickParallel: The function will be executed during EarlyTickParallel tick.
  • ETickType::LateTickParallel: The function will be executed during LateTickParallel tick.

Definition at line 55 of file TickManager.cpp.

56{
57 if (Instance == nullptr)
58 {
59 if (GEngine || GEngine->GameViewport)
60 {
61 UWorld* World = GEngine->GameViewport->GetWorld();
62 World->SpawnActor<ATickManager>();
63 }
64 }
65
66 FTickEntry TickEntry(Object, Function, Type);
67
68 switch (Type)
69 {
70 case ETickType::FullFrameTaskParallel:
72 break;
73 case ETickType::FullFrameTaskBeforeLateTickParallel:
75 break;
76 case ETickType::EarlyTickParallel:
78 break;
79 case ETickType::LateTickParallel:
81 break;
82 default:
83 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Unsupported ETickType in AddTick."));
84 break;
85 }
86
87 return TickEntry;
88}
static std::vector< FTickEntry > LateTickFunctions
Definition: TickManager.h:154
static std::vector< FTickEntry > FullFrameTaskBeforeLateTickFunctions
Definition: TickManager.h:152
static std::vector< FTickEntry > EarlyTickFunctions
Definition: TickManager.h:153
static void AddTickInternal(std::vector< FTickEntry > &to, const FTickEntry &newTickEntry)
Definition: TickManager.h:157
static ATickManager * Instance
Definition: TickManager.h:150
static std::vector< FTickEntry > FullFrameTaskFunctions
Definition: TickManager.h:151

References AddTickInternal(), EarlyTickFunctions, FullFrameTaskBeforeLateTickFunctions, FullFrameTaskFunctions, Instance, and LateTickFunctions.

Referenced by ALidarManager::AddRaycastLidar(), ACamera::BeginPlay(), and ATransformSensor::BeginPlay().

◆ AddTickInternal()

static void ATickManager::AddTickInternal ( std::vector< FTickEntry > &  to,
const FTickEntry newTickEntry 
)
inlinestaticprivate

Definition at line 157 of file TickManager.h.

158 {
159 bool found = std::find_if(to.begin(), to.end(), [&](const FTickEntry& entry) {
160 return (entry.Owner == newTickEntry.Owner && entry.TickType == newTickEntry.TickType);
161 }) != to.end();
162
163 if (!found)
164 {
165 to.emplace_back(newTickEntry);
166 }
167#if WITH_EDITOR
168 else
169 {
170 UE_LOG(LogTemp, Warning, TEXT("TickManager.h: This entry already exists. Returning."));
171 }
172#endif
173 }

Referenced by AddTick().

◆ BeginPlay()

void ATickManager::BeginPlay ( )
overrideprivatevirtual

Definition at line 20 of file TickManager.cpp.

21{
22 Super::BeginPlay();
23
24 if (Instance == nullptr)
25 {
26 Instance = this;
27
28 // Subscribe to pre and post tick actor delegates
29 OnPreTickDelegate = FWorldDelegates::OnWorldPreActorTick.AddUObject(this, &ATickManager::PreTick);
30 OnPostTickDelegate = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &ATickManager::PostTick);
31 }
32 else
33 {
34 Destroy();
35 }
36}
FDelegateHandle OnPreTickDelegate
Definition: TickManager.h:137
void PreTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
Definition: TickManager.cpp:96
void PostTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
FDelegateHandle OnPostTickDelegate
Definition: TickManager.h:140

References Destroy, Instance, OnPostTickDelegate, OnPreTickDelegate, PostTick(), and PreTick().

◆ EndPlay()

void ATickManager::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprivatevirtual

Definition at line 38 of file TickManager.cpp.

39{
40 Super::EndPlay(EndPlayReason);
41
42 if (Instance == this)
43 {
46 EarlyTickFunctions.clear();
47 LateTickFunctions.clear();
48 TicksToRemove.clear();
49 FWorldDelegates::OnWorldPreActorTick.Remove(OnPreTickDelegate);
50 FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickDelegate);
51 Instance = nullptr;
52 }
53}
static std::vector< FTickEntry > TicksToRemove
Definition: TickManager.h:155

References EarlyTickFunctions, FullFrameTaskBeforeLateTickFunctions, FullFrameTaskFunctions, Instance, LateTickFunctions, OnPostTickDelegate, OnPreTickDelegate, and TicksToRemove.

◆ FrameCleanup()

void ATickManager::FrameCleanup ( )
private

Definition at line 173 of file TickManager.cpp.

174{
175 if (TicksToRemove.size() != 0)
176 {
177 for (const FTickEntry& tickEntry : TicksToRemove)
178 {
179 ETickType Type = tickEntry.TickType;
180 switch (Type)
181 {
182 case ETickType::FullFrameTaskParallel:
184 break;
185 case ETickType::FullFrameTaskBeforeLateTickParallel:
187 break;
188 case ETickType::EarlyTickParallel:
190 break;
191 case ETickType::LateTickParallel:
193 break;
194 default:
195 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Unsupported ETickType in RemoveTick."));
196 break;
197 }
198 }
199 TicksToRemove.clear();
200 }
201
202 // Destroy this Actor if we have no ticks
203 // TODO: Should we just keep this alive?
204 if (FullFrameTaskFunctions.size() == 0 &&
206 EarlyTickFunctions.size() == 0 &&
207 LateTickFunctions.size() == 0)
208 {
209 if (Instance != nullptr)
210 {
211#if WITH_EDITOR
212 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Destroying as there are no more ticks."));
213#endif
214 Instance->Destroy();
215 Instance = nullptr;
216 }
217 }
218}
ETickType
Definition: TickManager.h:24
static void RemoveTickInternal(std::vector< FTickEntry > &from, FTickEntry tickEntry)
Definition: TickManager.h:175

References EarlyTickFunctions, FullFrameTaskBeforeLateTickFunctions, FullFrameTaskFunctions, Instance, LateTickFunctions, RemoveTickInternal(), and TicksToRemove.

Referenced by PostTick().

◆ PostTick()

void ATickManager::PostTick ( UWorld *  World,
ELevelTick  TickType,
float  DeltaSeconds 
)
private

Definition at line 125 of file TickManager.cpp.

126{
127 // Wait till all FullFrameTaskBeforeLateTick ticks are completed
128 if (FullFrameTaskBeforeLateTick.IsValid())
129 {
130 FTaskGraphInterface::Get().WaitUntilTaskCompletes(FullFrameTaskBeforeLateTick);
131 }
132
133 // Tick all added late ticks in parallel
134 if (LateTickFunctions.size() != 0)
135 {
137 }
138
139 // Wait till all FullFrameTask ticks are completed
140 if (FullFrameTask.IsValid())
141 {
142 FTaskGraphInterface::Get().WaitUntilTaskCompletes(FullFrameTask);
143 }
144
145 FrameCleanup();
146}
FGraphEventRef FullFrameTask
Definition: TickManager.h:148
FGraphEventRef FullFrameTaskBeforeLateTick
Definition: TickManager.h:147
void FrameCleanup()
void TickFunctionsParallel(std::vector< FTickEntry > &functions, const float DeltaTime)

References FrameCleanup(), FullFrameTask, FullFrameTaskBeforeLateTick, LateTickFunctions, and TickFunctionsParallel().

Referenced by BeginPlay().

◆ PreTick()

void ATickManager::PreTick ( UWorld *  World,
ELevelTick  TickType,
float  DeltaSeconds 
)
private

Definition at line 96 of file TickManager.cpp.

97{
98 // Start FullFrameTask that runs the entire frame
99 // if the task is small, it can finish at any time
100 if (FullFrameTaskFunctions.size() != 0)
101 {
102 FullFrameTask = FFunctionGraphTask::CreateAndDispatchWhenReady([this, DeltaSeconds]()
103 {
105 }, TStatId(), nullptr, ENamedThreads::AnyBackgroundHiPriTask);
106 }
107
108 // Start FullFrameTaskBeforeLateTick that finishes before calling LateTickFunctions
109 // if the task is small, it can finish at any time
111 {
112 FullFrameTaskBeforeLateTick = FFunctionGraphTask::CreateAndDispatchWhenReady([this, DeltaSeconds]()
113 {
115 }, TStatId(), nullptr, ENamedThreads::AnyBackgroundHiPriTask);
116 }
117
118 // Tick all added early ticks in parallel before continuing
119 if (EarlyTickFunctions.size() != 0)
120 {
122 }
123}

References EarlyTickFunctions, FullFrameTask, FullFrameTaskBeforeLateTick, FullFrameTaskBeforeLateTickFunctions, FullFrameTaskFunctions, and TickFunctionsParallel().

Referenced by BeginPlay().

◆ RemoveTick()

void ATickManager::RemoveTick ( FTickEntry  TickEntry)
static

Removes a tick function from the ATickManager.

Parameters
TickFunctionThe tick function to be removed. It should match the exact function previously added using AddTick.
TypeThe type of tick from which the function should be removed. Valid types are the same as for AddTick:
  • ETickType::FullFrameTaskParallel
  • ETickType::FullFrameTaskBeforeLateTickParallel
  • ETickType::EarlyTickParallel
  • ETickType::LateTickParallel
Note
If the specified function was not previously added for the given tick type, this function has no effect.

Definition at line 90 of file TickManager.cpp.

91{
92 // Add tick entry to remove into std::vector. These get deleted at the end of the frame.
93 TicksToRemove.push_back(TickEntry);
94}

References TicksToRemove.

Referenced by ACamera::EndPlay(), ALidarManager::EndPlay(), ATransformSensor::EndPlay(), and ALidarManager::RemoveRaycastLidar().

◆ RemoveTickInternal()

static void ATickManager::RemoveTickInternal ( std::vector< FTickEntry > &  from,
FTickEntry  tickEntry 
)
inlinestaticprivate

Definition at line 175 of file TickManager.h.

176 {
177 from.erase(std::remove(from.begin(), from.end(), tickEntry), from.end());
178 }

Referenced by FrameCleanup().

◆ TickFunctionsParallel()

void ATickManager::TickFunctionsParallel ( std::vector< FTickEntry > &  functions,
const float  DeltaTime 
)
inlineprivate

Definition at line 148 of file TickManager.cpp.

149{
150 bool foundNull = false;
151 //std::vector<FTickEntry> functionsCopy = functions;
152 const int size = functions.size();
153 ParallelFor(size, [&](int32 index)
154 {
155 FTickEntry& tickEntry = functions[index];
156 if (tickEntry.Owner != nullptr)
157 {
158 tickEntry.Tick(DeltaTime);
159 }
160 else
161 {
162 foundNull = true;
163 }
164 }, EParallelForFlags::Unbalanced);
165
166 if (foundNull)
167 {
168 functions.erase(std::remove_if(functions.begin(), functions.end(), [](const FTickEntry& Entry) { return Entry.Owner == nullptr; }), functions.end());
169 }
170}
UObject * Owner
Definition: TickManager.h:60

References FTickEntry::Owner.

Referenced by PostTick(), and PreTick().

Member Data Documentation

◆ EarlyTickFunctions

std::vector< FTickEntry > ATickManager::EarlyTickFunctions
staticprivate

Definition at line 153 of file TickManager.h.

Referenced by AddTick(), EndPlay(), FrameCleanup(), and PreTick().

◆ FullFrameTask

FGraphEventRef ATickManager::FullFrameTask
private

Definition at line 148 of file TickManager.h.

Referenced by PostTick(), and PreTick().

◆ FullFrameTaskBeforeLateTick

FGraphEventRef ATickManager::FullFrameTaskBeforeLateTick
private

Definition at line 147 of file TickManager.h.

Referenced by PostTick(), and PreTick().

◆ FullFrameTaskBeforeLateTickFunctions

std::vector< FTickEntry > ATickManager::FullFrameTaskBeforeLateTickFunctions
staticprivate

Definition at line 152 of file TickManager.h.

Referenced by AddTick(), EndPlay(), FrameCleanup(), and PreTick().

◆ FullFrameTaskFunctions

std::vector< FTickEntry > ATickManager::FullFrameTaskFunctions
staticprivate

Definition at line 151 of file TickManager.h.

Referenced by AddTick(), EndPlay(), FrameCleanup(), and PreTick().

◆ Instance

ATickManager * ATickManager::Instance = nullptr
staticprivate

Definition at line 150 of file TickManager.h.

Referenced by AddTick(), BeginPlay(), EndPlay(), and FrameCleanup().

◆ LateTickFunctions

std::vector< FTickEntry > ATickManager::LateTickFunctions
staticprivate

Definition at line 154 of file TickManager.h.

Referenced by AddTick(), EndPlay(), FrameCleanup(), and PostTick().

◆ OnPostTickDelegate

FDelegateHandle ATickManager::OnPostTickDelegate
private

Definition at line 140 of file TickManager.h.

Referenced by BeginPlay(), and EndPlay().

◆ OnPreTickDelegate

FDelegateHandle ATickManager::OnPreTickDelegate
private

Definition at line 137 of file TickManager.h.

Referenced by BeginPlay(), and EndPlay().

◆ TicksToRemove

std::vector< FTickEntry > ATickManager::TicksToRemove
staticprivate

Definition at line 155 of file TickManager.h.

Referenced by EndPlay(), FrameCleanup(), and RemoveTick().


The documentation for this class was generated from the following files: