UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Ticker.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
8#include "CoreTypes.h"
10#include "Templates/Function.h"
12
13#include <atomic>
14
22
27{
28private:
29 struct FElement;
30
31public:
33
36
46
56 CORE_API FDelegateHandle AddTicker(const TCHAR* InName, float InDelay, TUniqueFunction<bool(float)>&& InFunction);
57
67
81 CORE_API void Tick(float DeltaTime);
82
86 CORE_API void Reset();
87
88private:
90 struct FElement
91 {
93 double FireTime;
95 float DelayTime;
98
99 static constexpr uint64 DefaultState = 0;
100 static constexpr uint64 RemovedState = 1;
101 // The element can be in 4 states: "idle", "executing", "idle and removed" and "executing and removed"
102 // Often right after `RemoveTicket()` call resources that are used by the delegate are destroyed. We must ensure that
103 // these resources are not accessed by the delegate after `RemoveTicket()` returns.
104 // `State` is used to remove the element safely, if removal happens in the middle of the delegate execution, removal will not return
105 // until the execution is finished.
106 // `State` constists of two 32 bits parts: lower part is 1 if the delegate is removed, otherwise it's 0. the higher part contains thread id if the
107 // delegate being executed, otherwise it's 0
108 std::atomic<uint64> State{ DefaultState };
109
111 [[nodiscard]] CORE_API FElement();
113 [[nodiscard]] CORE_API FElement(double InFireTime, float InDelayTime, const FTickerDelegate& InDelegate);
115 [[nodiscard]] CORE_API FElement(double InFireTime, float InDelayTime, TUniqueFunction<bool(float)>&& InFunction);
116
118 CORE_API bool Fire(float DeltaTime);
119 };
120
121 using FElementPtr = TSharedPtr<FElement>;
122
123 // all added delegates are initially stored in a separate thread-safe queue and then in the next Tick are moved to the main not thread-safe
124 // container
125 TMpscQueue<FElementPtr> AddedElements;
126
128 std::atomic<double> CurrentTime{ 0.0 };
130 TArray<FElementPtr> Elements;
131};
132
137{
138public:
140
148
151
158 virtual bool Tick(float DeltaTime) = 0;
159
160private:
163};
164
165// Schedules execution of the given functor on the game thread at a particular moment of a frame when interference with other systems
166// is minimal.
167template<typename FunctorType>
168void ExecuteOnGameThread(const TCHAR* DebugName, FunctorType&& Functor)
169{
170 FTSTicker::GetCoreTicker().AddTicker(DebugName, 0.0f,
171 [Functor = MoveTemp(Functor)] (float) mutable
172 {
173 Functor();
174 return false;
175 }
176 );
177}
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DELEGATE_RetVal_OneParam(ReturnValueType, DelegateName, Param1Type)
Definition DelegateCombinations.h:54
const bool
Definition NetworkReplayStreaming.h:178
void ExecuteOnGameThread(const TCHAR *DebugName, FunctorType &&Functor)
Definition Ticker.h:168
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition Ticker.h:137
UE_NONCOPYABLE(FTSTickerObjectBase)
virtual CORE_API ~FTSTickerObjectBase()
Definition Ticker.cpp:189
virtual bool Tick(float DeltaTime)=0
Definition Ticker.h:27
static CORE_API void RemoveTicker(FDelegateHandle Handle)
Definition Ticker.cpp:39
static CORE_API FTSTicker & GetCoreTicker()
Definition Ticker.cpp:8
CORE_API FDelegateHandle AddTicker(const FTickerDelegate &InDelegate, float InDelay=0.0f)
Definition Ticker.cpp:14
CORE_API void Reset()
Definition Ticker.cpp:146
Definition Array.h:670
Definition MpscQueue.h:18
Definition SharedPointer.h:692
Definition FunctionFwd.h:19