KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
UDelayTaskManager.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved. Unauthorized copying, modification, or distribution of this file, via any medium is strictly prohibited. Proprietary and confidential.
2
7#include "UDelayTaskManager.h"
8#include "Engine/World.h"
9#include "TimerManager.h"
10
11void UDelayTaskManager::Initialize(FSubsystemCollectionBase& Collection)
12{
13 Super::Initialize(Collection);
14}
15
17{
18 if (auto World = GetWorld())
19 {
20 FTimerManager& TM = World->GetTimerManager();
21
22 for (const auto& KVP : Entries)
23 {
24 FTimerHandle TempHandle = KVP.Value.TimerHandle;
25 World->GetTimerManager().ClearTimer(TempHandle);
26 }
27 }
28 Entries.Empty();
29 OwnerIndex.Empty();
30
31 Super::Deinitialize();
32}
33
34FDelayHandle UDelayTaskManager::DelayCallBack(UObject* Owner, const float Seconds, FDelayCallback Callback)
35{
36 return Delay(Owner, Seconds, [Callback]() {
37 if (Callback.IsBound())
38 Callback.Execute();
39 });
40}
41
42FDelayHandle UDelayTaskManager::Delay(UObject* Owner, const float Seconds, TFunction<void()> Action)
43{
44 FDelayHandle Out;
45 if (!GetWorld() || Seconds < 0.f || !Action)
46 return Out;
47
48 const uint64 SeqIndex = NextSeqIndex++;
49
50 FTimerHandle Handle;
51 FTimerDelegate D;
52 D.BindUObject(this, &UDelayTaskManager::ExecuteById, SeqIndex); // 핸들 대신 Seq 바인딩
53
54 GetWorld()->GetTimerManager().SetTimer(Handle, D, Seconds, /*bLoop=*/false);
55
56 FEntry& E = Entries.Add(SeqIndex);
57 E.Owner = Owner;
58 E.Action = MoveTemp(Action);
59 E.SeqIndex = SeqIndex;
60 E.TimerHandle = Handle;
61
62 if (Owner)
63 {
64 OwnerIndex.Add(Owner, SeqIndex);
65 }
66
67 Out.SeqIndex = SeqIndex;
68 return Out;
69}
70
71FDelayHandle UDelayTaskManager::NextTick(UObject* Owner, TFunction<void()> Action)
72{
73 FDelayHandle Out;
74 if (!GetWorld() || !Action)
75 return Out;
76
77 const uint64 SeqIndex = NextSeqIndex++;
78
79 // 다음 틱 보장 & 취소 가능성을 위해 0초 타이머 사용(엔진은 다음 프레임에 실행)
80 FTimerHandle Handle;
81 FTimerDelegate D;
82 D.BindUObject(this, &UDelayTaskManager::ExecuteById, SeqIndex);
83
84 GetWorld()->GetTimerManager().SetTimer(Handle, D, /*Rate=*/0.0f, /*bLoop=*/false);
85
86 FEntry& E = Entries.Add(SeqIndex);
87 E.Owner = Owner;
88 E.Action = MoveTemp(Action);
89 E.SeqIndex = SeqIndex;
90 E.TimerHandle = Handle;
91
92 if (Owner)
93 {
94 OwnerIndex.Add(Owner, SeqIndex);
95 }
96
97 Out.SeqIndex = SeqIndex;
98 return Out;
99}
100
102{
103 if (!GetWorld() || !Handle.IsValid())
104 return false;
105
106 FEntry Entry;
107 if (Entries.RemoveAndCopyValue(Handle.SeqIndex, Entry))
108 {
109 // 엔진 타이머도 정리
110 GetWorld()->GetTimerManager().ClearTimer(Entry.TimerHandle);
111 OwnerIndex.RemoveSingle(Entry.Owner, Handle.SeqIndex);
112 return true;
113 }
114
115 return false;
116}
117
119{
120 if (!GetWorld() || !Owner)
121 return;
122
123 TArray<uint64> Ids;
124 OwnerIndex.MultiFind(Owner, Ids);
125 if (Ids.Num() == 0)
126 return;
127
128 FTimerManager& TM = GetWorld()->GetTimerManager();
129
130 for (uint64 Seq : Ids)
131 {
132 FEntry Entry;
133 if (Entries.RemoveAndCopyValue(Seq, Entry))
134 {
135 TM.ClearTimer(Entry.TimerHandle);
136 OwnerIndex.RemoveSingle(Entry.Owner, Seq);
137 }
138 }
139}
140
142{
143 FEntry Entry;
144 if (!Entries.RemoveAndCopyValue(SeqIndex, Entry))
145 {
146 return;
147 }
148
149 const bool bOwnerValid = Entry.Owner.IsValid();
150 if (bOwnerValid && Entry.Action )
151 {
152 Entry.Action();
153 }
154
155 OwnerIndex.RemoveSingle(Entry.Owner, SeqIndex);
156}
157
159{
160 if (FEntry* Found = Entries.Find(SeqIndex))
161 {
162 OwnerIndex.RemoveSingle(Found->Owner, SeqIndex);
163 Entries.Remove(SeqIndex);
164 }
165}
UDelayTaskManager 클래스를 선언합니다.
bool Cancel(const FDelayHandle &Handle)
void RemoveEntryById(uint64 SeqIndex)
void ExecuteById(uint64 SeqIndex)
void CancelAll(UObject *Owner)
TMultiMap< TWeakObjectPtr< UObject >, uint64 > OwnerIndex
virtual void Deinitialize() override
FDelayHandle DelayCallBack(UObject *Owner, float Seconds, FDelayCallback Callback)
FDelayHandle Delay(UObject *Owner, float Seconds, TFunction< void()> Action)
FDelayHandle NextTick(UObject *Owner, TFunction< void()> Action)
TMap< uint64, FEntry > Entries
virtual void Initialize(FSubsystemCollectionBase &Collection) override
UDelayTaskManager에 의해 생성된 지연 작업을 식별하고 제어하기 위한 핸들입니다.
bool IsValid() const
TWeakObjectPtr< UObject > Owner