Agrarsense
TickManager.h
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "CoreMinimal.h"
9#include "GameFramework/Actor.h"
10#include "UObject/Object.h"
11#include "Delegates/IDelegateInstance.h"
12#include "Async/TaskGraphInterfaces.h"
13#include "GenericPlatform/GenericPlatformMisc.h"
14#include "Engine/World.h"
15
16#include <functional>
17#include <algorithm>
18#include <vector>
19
20#include "TickManager.generated.h"
21
22UENUM(BlueprintType)
23enum class ETickType : uint8
24{
29 FullFrameTaskParallel UMETA(DisplayName = "FullFrameTaskParallel"),
30
35 FullFrameTaskBeforeLateTickParallel UMETA(DisplayName = "FullFrameTaskBeforeLateTickParallel"),
36
40 EarlyTickParallel UMETA(DisplayName = "EarlyTickParallel"),
41
46 LateTickParallel UMETA(DisplayName = "LateTickParallel"),
47
51 NONE UMETA(DisplayName = "NONE")
52};
53
54USTRUCT()
56{
57 GENERATED_BODY()
58
59 UPROPERTY()
60 UObject* Owner;
61
62 std::function<void(float)> Tick;
63
64 ETickType TickType = ETickType::NONE;
65
66 FTickEntry() : Owner(nullptr) {}
67
68 FTickEntry(UObject* InOwner, std::function<void(float)> InTickFunction, ETickType InType)
69 : Owner(InOwner), Tick(InTickFunction), TickType(InType) {}
70
71 bool operator==(const FTickEntry& Other) const
72 {
73 return Owner == Other.Owner && TickType == Other.TickType;
74 }
75};
76
100UCLASS()
101class AGRARSENSE_API ATickManager : public AActor
102{
103 GENERATED_BODY()
104
105public:
106
117 static FTickEntry AddTick(UObject* Object, std::function<void(float)> Function, ETickType Type);
118
130 static void RemoveTick(FTickEntry TickEntry);
131
132private:
133
134 virtual void BeginPlay() override;
135 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
136
137 FDelegateHandle OnPreTickDelegate;
138 void PreTick(UWorld* World, ELevelTick TickType, float DeltaSeconds);
139
140 FDelegateHandle OnPostTickDelegate;
141 void PostTick(UWorld* World, ELevelTick TickType, float DeltaSeconds);
142
143 void FrameCleanup();
144
145 inline void TickFunctionsParallel(std::vector<FTickEntry>& functions, const float DeltaTime);
146
148 FGraphEventRef FullFrameTask;
149
151 static std::vector<FTickEntry> FullFrameTaskFunctions;
152 static std::vector<FTickEntry> FullFrameTaskBeforeLateTickFunctions;
153 static std::vector<FTickEntry> EarlyTickFunctions;
154 static std::vector<FTickEntry> LateTickFunctions;
155 static std::vector<FTickEntry> TicksToRemove;
156
157 inline static void AddTickInternal(std::vector<FTickEntry>& to, const FTickEntry& newTickEntry)
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 }
174
175 inline static void RemoveTickInternal(std::vector<FTickEntry>& from, FTickEntry tickEntry)
176 {
177 from.erase(std::remove(from.begin(), from.end(), tickEntry), from.end());
178 }
179};
180
181template <typename ObjectType, typename FunctionType>
182static auto BindTick(ObjectType* Object, FunctionType Function)
183{
184 return [Object, Function](float DeltaTime)
185 {
186 (Object->*Function)(DeltaTime);
187 };
188}
static auto BindTick(ObjectType *Object, FunctionType Function)
Definition: TickManager.h:182
ETickType
Definition: TickManager.h:24
FDelegateHandle OnPreTickDelegate
Definition: TickManager.h:137
static std::vector< FTickEntry > LateTickFunctions
Definition: TickManager.h:154
static std::vector< FTickEntry > TicksToRemove
Definition: TickManager.h:155
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
FGraphEventRef FullFrameTask
Definition: TickManager.h:148
FDelegateHandle OnPostTickDelegate
Definition: TickManager.h:140
static ATickManager * Instance
Definition: TickManager.h:150
FGraphEventRef FullFrameTaskBeforeLateTick
Definition: TickManager.h:147
static std::vector< FTickEntry > FullFrameTaskFunctions
Definition: TickManager.h:151
static void RemoveTickInternal(std::vector< FTickEntry > &from, FTickEntry tickEntry)
Definition: TickManager.h:175
bool operator==(const FTickEntry &Other) const
Definition: TickManager.h:71
UObject * Owner
Definition: TickManager.h:60
FTickEntry(UObject *InOwner, std::function< void(float)> InTickFunction, ETickType InType)
Definition: TickManager.h:68
ETickType TickType
Definition: TickManager.h:64