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 FullFrameTask
 

Static Private Attributes

static ATickManagerInstance = nullptr
 
static std::vector< FTickEntryFullFrameTaskFunctions
 
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 88 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::LateTickParallel: The function will be executed during LateTickParallel tick.

Definition at line 51 of file TickManager.cpp.

52{
53 if (Instance == nullptr)
54 {
55 if (GEngine || GEngine->GameViewport)
56 {
57 UWorld* World = GEngine->GameViewport->GetWorld();
58 World->SpawnActor<ATickManager>();
59 }
60 }
61
62 FTickEntry TickEntry(Object, Function, Type);
63
64 switch (Type)
65 {
66 case ETickType::FullFrameTaskParallel:
68 break;
69 case ETickType::LateTickParallel:
71 break;
72 default:
73 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Unsupported ETickType in AddTick."));
74 break;
75 }
76
77 return TickEntry;
78}
static std::vector< FTickEntry > LateTickFunctions
Definition: TickManager.h:134
static void AddTickInternal(std::vector< FTickEntry > &to, const FTickEntry &newTickEntry)
Definition: TickManager.h:137
static ATickManager * Instance
Definition: TickManager.h:132
static std::vector< FTickEntry > FullFrameTaskFunctions
Definition: TickManager.h:133

References AddTickInternal(), 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 137 of file TickManager.h.

138 {
139 bool found = std::find_if(to.begin(), to.end(), [&](const FTickEntry& entry) {
140 return (entry.Owner == newTickEntry.Owner && entry.TickType == newTickEntry.TickType);
141 }) != to.end();
142
143 if (!found)
144 {
145 to.emplace_back(newTickEntry);
146 }
147#if WITH_EDITOR
148 else
149 {
150 UE_LOG(LogTemp, Warning, TEXT("TickManager.h: This entry already exists. Returning."));
151 }
152#endif
153 }

Referenced by AddTick().

◆ BeginPlay()

void ATickManager::BeginPlay ( )
overrideprivatevirtual

Definition at line 18 of file TickManager.cpp.

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

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

◆ EndPlay()

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

Definition at line 36 of file TickManager.cpp.

37{
38 Super::EndPlay(EndPlayReason);
39
40 if (Instance == this)
41 {
43 LateTickFunctions.clear();
44 TicksToRemove.clear();
45 FWorldDelegates::OnWorldPreActorTick.Remove(OnPreTickDelegate);
46 FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickDelegate);
47 Instance = nullptr;
48 }
49}
static std::vector< FTickEntry > TicksToRemove
Definition: TickManager.h:135

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

◆ FrameCleanup()

void ATickManager::FrameCleanup ( )
private

Definition at line 129 of file TickManager.cpp.

130{
131 if (TicksToRemove.size() != 0)
132 {
133 for (const FTickEntry& tickEntry : TicksToRemove)
134 {
135 ETickType Type = tickEntry.TickType;
136 switch (Type)
137 {
138 case ETickType::FullFrameTaskParallel:
140 break;
141 case ETickType::LateTickParallel:
143 break;
144 default:
145 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Unsupported ETickType in RemoveTick."));
146 break;
147 }
148 }
149 TicksToRemove.clear();
150 }
151
152 // Destroy this Actor if we have no ticks
153 // TODO: Should we just keep this alive?
154 if (FullFrameTaskFunctions.size() == 0 &&
155 LateTickFunctions.size() == 0)
156 {
157 if (Instance != nullptr)
158 {
159#if WITH_EDITOR
160 UE_LOG(LogTemp, Warning, TEXT("TickManager.cpp: Destroying as there are no more ticks."));
161#endif
162 Instance->Destroy();
163 Instance = nullptr;
164 }
165 }
166}
ETickType
Definition: TickManager.h:24
static void RemoveTickInternal(std::vector< FTickEntry > &from, FTickEntry tickEntry)
Definition: TickManager.h:155

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

Referenced by PostTick().

◆ PostTick()

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

Definition at line 99 of file TickManager.cpp.

100{
101 // Tick all added late ticks in parallel
102 if (LateTickFunctions.size() != 0)
103 {
105 }
106
107 // Wait till all FullFrameTask ticks are completed
108 if (FullFrameTask.IsValid())
109 {
110 FTaskGraphInterface::Get().WaitUntilTaskCompletes(FullFrameTask);
111 }
112
113 FrameCleanup();
114}
FGraphEventRef FullFrameTask
Definition: TickManager.h:130
void FrameCleanup()
void TickFunctionsParallel(std::vector< FTickEntry > &functions, const float DeltaTime)

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

Referenced by BeginPlay().

◆ PreTick()

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

Definition at line 86 of file TickManager.cpp.

87{
88 // Start FullFrameTask that runs the entire frame
89 // if the task is small, it can finish at any time
90 if (FullFrameTaskFunctions.size() != 0)
91 {
92 FullFrameTask = FFunctionGraphTask::CreateAndDispatchWhenReady([this, DeltaSeconds]()
93 {
95 }, TStatId(), nullptr, ENamedThreads::AnyBackgroundHiPriTask);
96 }
97}

References FullFrameTask, 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::LateTickParallel
Note
If the specified function was not previously added for the given tick type, this function has no effect.

Definition at line 80 of file TickManager.cpp.

81{
82 // Add tick entry to remove into std::vector. These get deleted at the end of the frame.
83 TicksToRemove.push_back(TickEntry);
84}

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 155 of file TickManager.h.

156 {
157 from.erase(std::remove(from.begin(), from.end(), tickEntry), from.end());
158 }

Referenced by FrameCleanup().

◆ TickFunctionsParallel()

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

Definition at line 116 of file TickManager.cpp.

117{
118 const int32 size = functions.size();
119 ParallelFor(size, [&](int32 index)
120 {
121 FTickEntry& tickEntry = functions[index];
122 if (tickEntry.Owner != nullptr)
123 {
124 tickEntry.Tick(DeltaTime);
125 }
126 }, EParallelForFlags::Unbalanced);
127}
UObject * Owner
Definition: TickManager.h:49

References FTickEntry::Owner.

Referenced by PostTick(), and PreTick().

Member Data Documentation

◆ FullFrameTask

FGraphEventRef ATickManager::FullFrameTask
private

Definition at line 130 of file TickManager.h.

Referenced by PostTick(), and PreTick().

◆ FullFrameTaskFunctions

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

Definition at line 133 of file TickManager.h.

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

◆ Instance

ATickManager * ATickManager::Instance = nullptr
staticprivate

Definition at line 132 of file TickManager.h.

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

◆ LateTickFunctions

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

Definition at line 134 of file TickManager.h.

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

◆ OnPostTickDelegate

FDelegateHandle ATickManager::OnPostTickDelegate
private

Definition at line 123 of file TickManager.h.

Referenced by BeginPlay(), and EndPlay().

◆ OnPreTickDelegate

FDelegateHandle ATickManager::OnPreTickDelegate
private

Definition at line 120 of file TickManager.h.

Referenced by BeginPlay(), and EndPlay().

◆ TicksToRemove

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

Definition at line 135 of file TickManager.h.

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


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