UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ActorChannel.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
8#include "Misc/NetworkGuid.h"
9#include "Engine/Channel.h"
10#include "Net/DataReplication.h"
12#include "ActorChannel.generated.h"
13
14#ifndef NET_ENABLE_SUBOBJECT_REPKEYS
15#define NET_ENABLE_SUBOBJECT_REPKEYS 1
16#endif // NET_ENABLE_SUBOBJECT_REPKEYS
17
18// Enable the code allowing to validate replicate subobjects when converting from the legacy method to the explicit registration list
19#ifndef SUBOBJECT_TRANSITION_VALIDATION
20 #define SUBOBJECT_TRANSITION_VALIDATION !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
21#endif
22
23class UActorComponent;
24class AActor;
25class FInBunch;
27class FOutBunch;
28class UNetConnection;
29struct FObjectKey;
30
31namespace UE::Net
32{
33 struct FSubObjectRegistry;
34 class FNetConditionGroupManager;
35}
36
38{
39 None = 0,
40 SkipReplicatorCreation = (1 << 0),
41 SkipMarkActive = (1 << 1),
42};
43
45
76UCLASS(transient, customConstructor, MinimalAPI)
78{
80
81public:
82 friend class FObjectReplicator;
83
84 // Variables.
85 UPROPERTY()
86 TObjectPtr<AActor> Actor; // Actor this corresponds to.
87
88 FNetworkGUID ActorNetGUID; // Actor GUID (useful when we don't have the actor resolved yet). Currently only valid on clients.
89 float CustomTimeDilation;
90
91 // Variables.
92 double RelevantTime; // Last time this actor was relevant to client.
93 double LastUpdateTime; // Last time this actor was replicated.
94 uint32 SpawnAcked:1; // Whether spawn has been acknowledged.
95 uint32 bForceCompareProperties:1; // Force this actor to compare all properties for a single frame
96 uint32 bIsReplicatingActor:1; // true when in this channel's ReplicateActor() to avoid recursion as that can cause invalid data to be sent
97
101 uint32 bClearRecentActorRefs:1;
102 uint32 bHoldQueuedExportBunchesAndGUIDs:1; // Don't export QueuedExportBunches or QueuedMustBeMappedGuidsInLastBunch if this is true
103
104#if !UE_BUILD_SHIPPING
107#endif
108
109private:
110 uint32 bSkipRoleSwap:1; // true if we should not swap the role and remote role of this actor when properties are received
111
113 uint32 bActorIsPendingKill : 1;
114
119 uint32 bSuppressQueuedBunchWarningsDueToHitches : 1;
120
122 uint32 bIsForcedSerializeFromRPC:1;
123
124 uint16 ChannelSubObjectDirtyCount = 0;
125
126public:
127 bool GetSkipRoleSwap() const { return !!bSkipRoleSwap; }
128 void SetSkipRoleSwap(const bool bShouldSkip) { bSkipRoleSwap = bShouldSkip; }
129
131
133
134 // Async networking loading support state
135 TArray< class FInBunch * > QueuedBunches; // Queued bunches waiting on pending guids to resolve
136 double QueuedBunchStartTime; // Time when since queued bunches was last empty
137
138 TSet<FNetworkGUID> PendingGuidResolves; // These guids are waiting for their resolves, we need to queue up bunches until these are resolved
139
140 UE_DEPRECATED(5.1, "The CreateSubObjects array will be made private in future versions. Use GetCreatedSubObjects() instead")
141 UPROPERTY()
142 TArray< TObjectPtr<UObject> > CreateSubObjects; // Any sub-object we created on this channel
143
144 inline const TArray< TObjectPtr<UObject> >& GetCreatedSubObjects() const
145 {
147 return CreateSubObjects;
149 }
150
151 TArray< FNetworkGUID > QueuedMustBeMappedGuidsInLastBunch; // Array of guids that will async load on client. This list is used for queued RPC's.
152 TArray< class FOutBunch * > QueuedExportBunches; // Bunches that need to be appended to the export list on the next SendBunch call. This list is used for queued RPC's.
153
154
156
161
163
164public:
165
166 // UChannel interface.
167
168 ENGINE_API virtual void Init( UNetConnection* InConnection, int32 InChIndex, EChannelCreateFlags CreateFlags ) override;
169 ENGINE_API virtual void SetClosingFlag() override;
170 ENGINE_API virtual void ReceivedBunch( FInBunch& Bunch ) override;
171 ENGINE_API virtual void Tick() override;
172 ENGINE_API virtual bool CanStopTicking() const override;
173
174 ENGINE_API void ProcessBunch( FInBunch & Bunch );
175 ENGINE_API bool ProcessQueuedBunches();
176
177 ENGINE_API virtual void ReceivedNak( int32 NakPacketId ) override;
178 ENGINE_API virtual int64 Close(EChannelCloseReason Reason) override;
179 ENGINE_API virtual FString Describe() override;
180
182 ENGINE_API void BreakAndReleaseReferences();
183
184 ENGINE_API void ReleaseReferences(bool bKeepReplicators);
185
187 AActor* GetActor() const { return Actor; }
188
190 ENGINE_API int64 ReplicateActor();
191
193 ENGINE_API bool IsActorReadyForReplication() const;
194
200 ENGINE_API void SetChannelActor(AActor* InActor, ESetChannelActorFlags Flags);
201
202 ENGINE_API virtual void NotifyActorChannelOpen(AActor* InActor, FInBunch& InBunch);
203
205 UE_DEPRECATED(5.5, "Use GetAdditionalRequiredBunches")
206 ENGINE_API virtual void AppendExportBunches( TArray< FOutBunch* >& OutExportBunches ) override;
207
210
212 ENGINE_API virtual void AppendMustBeMappedGuids( FOutBunch* Bunch ) override;
213
215
216 static ENGINE_API void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
217
220
222 ENGINE_API void PrepareForRemoteFunction(UObject* TargetObj);
223
226
229
231 ENGINE_API void CleanupReplicators( const bool bKeepReplicators = false );
232
234 ENGINE_API void WriteContentBlockHeader( UObject* Obj, FNetBitWriter &Bunch, const bool bHasRepLayout );
235
237 UE_DEPRECATED(5.2, "This function will be made private in the future." )
238 ENGINE_API void WriteContentBlockForSubObjectDelete( FOutBunch & Bunch, FNetworkGUID & GuidToDelete);
239
241 ENGINE_API int32 WriteContentBlockPayload( UObject* Obj, FNetBitWriter &Bunch, const bool bHasRepLayout, FNetBitWriter& Payload );
242
244 ENGINE_API UObject* ReadContentBlockHeader( FInBunch& Bunch, bool& bObjectDeleted, bool& bOutHasRepLayout );
245
247 ENGINE_API UObject* ReadContentBlockPayload( FInBunch &Bunch, FNetBitReader& OutPayload, bool& bOutHasRepLayout );
248
251
254
256 ENGINE_API FNetFieldExportGroup* GetNetFieldExportGroupForClassNetCache( UClass* ObjectClass );
257
259 ENGINE_API FNetFieldExportGroup* GetOrCreateNetFieldExportGroupForClassNetCache( const UObject* Object );
260
262 ENGINE_API FObjectReplicator & GetActorReplicationData();
263
264 // --------------------------------
265 // Subobject Replication state
266 //
267 // Concepts:
268 // ObjID - this is an arbitrary identifier given to us by the game code.
269 // RepKey - this is an idenifier for the current replicated state.
270 //
271 // ObjID should be constant per object or "category". Its up to the game code. For example the game code could use 0 to determine if an entire array is dirty,
272 // then usen 1-N for each subobject in that list. Or it could have 5 arrays using 0-4, and then use 100*ArrayNum + idx for the items in the array.
273 //
274 // RepKey should change as the subobject changes. Each time a subobject is marked dirty, its RepKey should change.
275 //
276 // GameCode should call ::KeyNeedsToReplicate(ObjID, RepKey) to determine if it needs to replicate. For example:
277 //
278 //
279 /*
280
281 bool AMyActorClass::ReplicateSubobjects(UActorChannel *Channel, FOutBunch *Bunch, FReplicationFlags *RepFlags)
282 {
283 bool WroteSomething = false;
284
285 if (Channel->KeyNeedsToReplicate(0, ReplicatedArrayKey) ) // Does the array need to replicate?
286 {
287 for (int32 idx = 0; idx < ReplicatedSubobjects.Num(); ++idx )
288 {
289 UMyActorSubobjClass *Obj = ReplicatedSubObjects[idx];
290 if (Channel->KeyNeedsToReplicate(1 + idx, Obj->RepKey))
291 {
292 WroteSomething |= Channel->ReplicateSubobject<UMyActorSubobjClass>(Obj, *Bunch, *RepFlags);
293 }
294 }
295 }
296
297 return WroteSomething;
298 }
299
300 void UMyActorSubobjClass::MarkDirtyForReplication()
301 {
302 this->RepKey++;
303 MyOwningActor->ReplicatedArrayKey++;
304 }
305
306 */
307 //
308 // --------------------------------
309
314 static ENGINE_API void SetCurrentSubObjectOwner(AActor* SubObjectOwner);
315 static ENGINE_API void SetCurrentSubObjectOwner(UActorComponent* SubObjectOwner);
316
317#if SUBOBJECT_TRANSITION_VALIDATION
320#endif
321
323 ENGINE_API bool ReplicateSubobject(UObject* Obj, FOutBunch& Bunch, FReplicationFlags RepFlags);
324
326 ENGINE_API bool ReplicateSubobject(UActorComponent* ActorChannel, FOutBunch& Bunch, FReplicationFlags RepFlags);
327
329 virtual bool ReplicateSubobjectCustom(UObject* Obj, FOutBunch& Bunch, const FReplicationFlags& RepFlags) { return true; }
330
332 template<typename Type>
333 UE_DEPRECATED(5.1, "This function will be deleted. Register your subobjects using AddReplicatedSubObject instead.")
334 bool ReplicateSubobjectList(TArray<Type*> &ObjectList, FOutBunch &Bunch, const FReplicationFlags &RepFlags)
335 {
336 bool WroteSomething = false;
337 for (auto It = ObjectList.CreateIterator(); It; ++It)
338 {
339 Type* Obj = *It;
340 WroteSomething |= ReplicateSubobject(Obj, Bunch, RepFlags);
341 }
342
343 return WroteSomething;
344 }
345
346 void SetForcedSerializeFromRPC(bool bInFromRPC) { bIsForcedSerializeFromRPC = bInFromRPC; }
347
348#if NET_ENABLE_SUBOBJECT_REPKEYS
349 // Static size for SubobjectRepKeyMap. Allows us to resuse arrays and avoid dyanmic memory allocations
350 static const int32 SubobjectRepKeyBufferSize = 64;
351
359
360 // Maps ObjID to the current RepKey
362
363 // Maps packetId to keys in Subobject
365
366 // Keys pending in this bunch
368
369 // Returns true if the given ObjID is not up to date with RepKey
370 // this implicitly 'writes' the RepKey to the current out bunch.
372#endif // NET_ENABLE_SUBOBJECT_REPKEYS
373
374 // --------------------------------
375
376 ENGINE_API virtual void AddedToChannelPool() override;
377
378 static ENGINE_API bool CanSubObjectReplicateToClient(
379 const APlayerController* PlayerController,
380 ELifetimeCondition NetCondition,
384
385protected:
386
389
398 UE_DEPRECATED(5.2, "This function has been deprecated in favor of the one with a single parameter")
399 ENGINE_API TSharedRef<FObjectReplicator>* FindReplicator(UObject* Obj, bool* bOutFoundInvalid);
400
404 ENGINE_API TSharedRef<FObjectReplicator>& CreateReplicator(UObject* Obj);
405
419 UE_DEPRECATED(5.2, "This function has been deprecated in favor of the one with a single parameter")
421
430 ENGINE_API TSharedRef<FObjectReplicator>& FindOrCreateReplicator(UObject* Obj, bool* bOutCreated=nullptr);
431
432 ENGINE_API bool ObjectHasReplicator(const TWeakObjectPtr<UObject>& Obj) const; // returns whether we have already created a replicator for this object or not
433
435 ENGINE_API void MoveMappedObjectToUnmapped(const UObject* Object);
436
437 ENGINE_API void DestroyActorAndComponents(EChannelCloseReason Reason = EChannelCloseReason::Destroyed);
438
439 ENGINE_API virtual bool CleanUp( const bool bForDestroy, EChannelCloseReason CloseReason ) override;
440
442 ENGINE_API virtual void BecomeDormant() override;
443
445 ENGINE_API bool DoSubObjectReplication(FOutBunch& Bunch, FReplicationFlags& OutRepFlags);
446
448 {
449 Destroyed, // Delete operation that occurs when we detect that the original object on the authority became invalid.
450 TearOff, // The client's actor channel will remove references to this subobject
451 ForceDelete, // The subobject needs to be deleted on the client even if the original on the authority was not.
452 };
453
455 ENGINE_API void WriteContentBlockForSubObjectDelete(FOutBunch& Bunch, FNetworkGUID& GuidToDelete, ESubObjectDeleteFlag DeleteFlag);
456
458
459private:
460
462 ENGINE_API bool ReplicateRegisteredSubObjects(FOutBunch& Bunch, FReplicationFlags RepFlags);
463
465 ENGINE_API bool WriteSubObjectInBunch(UObject* Obj, FOutBunch& Bunch, FReplicationFlags RepFlags);
466
468 ENGINE_API bool WriteComponentSubObjects(UActorComponent* Component, FOutBunch& Bunch, FReplicationFlags RepFlags, const TStaticBitArray<COND_Max>& ConditionMap);
469
472
473 ENGINE_API bool ValidateReplicatedSubObjects();
474
475 ENGINE_API void TestLegacyReplicateSubObjects(UActorComponent* ReplicatedComponent, FOutBunch& Bunch, FReplicationFlags RepFlags);
476 ENGINE_API void TestLegacyReplicateSubObjects(FOutBunch& Bunch, FReplicationFlags RepFlags);
477
478 ENGINE_API bool UpdateDeletedSubObjects(FOutBunch& Bunch);
479
480 inline TArray< TObjectPtr<UObject> >& GetCreatedSubObjects()
481 {
483 return CreateSubObjects;
485 }
486
488 bool ShouldProcessAllQueuedBunches(float CurrentTimeSeconds);
489
491 void ProcessBunchInternal(FInBunch& Bunch);
492
493private:
494
495 // TODO: It would be nice to merge the tracking of these with PendingGuidResolves, to not duplicate memory,
496 // especially since both of these sets should be empty most of the time for most channels.
497 TSet<TSharedRef<struct FQueuedBunchObjectReference>> QueuedBunchObjectReferences;
498
499 static ENGINE_API const FString ClassNetCacheSuffix;
500};
OODEFFUNC typedef const int const char * function
Definition oodle2.h:710
ESetChannelActorFlags
Definition ActorChannel.h:38
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
ELifetimeCondition
Definition CoreNetTypes.h:20
EChannelCloseReason
Definition CoreNetTypes.h:49
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EChannelCreateFlags
Definition Channel.h:41
EChannelGetAdditionalRequiredBunchesFlags
Definition Channel.h:48
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
JsonWriter Close()
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
void Init()
Definition LockFreeList.h:4
const bool
Definition NetworkReplayStreaming.h:178
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UCLASS(...)
Definition ObjectMacros.h:776
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Actor.h:257
Definition PlayerController.h:261
Definition Archive.h:1208
Definition CoreNet.h:91
Definition CoreNet.h:73
Definition DataBunch.h:127
Definition CoreNet.h:415
Definition CoreNet.h:384
Definition PackageMapClient.h:92
Definition NetworkGuid.h:12
Definition UObjectGlobals.h:1292
static COREUOBJECT_API FObjectInitializer & Get()
Definition UObjectGlobals.cpp:5001
Definition DataReplication.h:74
ENGINE_API bool ReceivedBunch(FNetBitReader &Bunch, const FReplicationFlags &RepFlags, const bool bHasRepLayout, bool &bOutHasUnmapped)
Definition DataReplication.cpp:980
ENGINE_API void ReceivedNak(int32 NakPacketId)
Definition DataReplication.cpp:884
ENGINE_API void QueueRemoteFunctionBunch(UFunction *Func, FOutBunch &Bunch)
Definition DataReplication.cpp:2288
ENGINE_API void StartBecomingDormant()
Definition DataReplication.cpp:2415
ENGINE_API bool ReadyForDormancy(bool bDebug=false)
Definition DataReplication.cpp:2362
TObjectPtr< UClass > ObjectClass
Definition DataReplication.h:337
Definition DataBunch.h:24
Definition UObjectGlobals.h:2492
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition StaticBitArray.h:75
Definition ActorChannel.h:78
TMap< int32, int32 > SubobjectRepKeyMap
Definition ActorChannel.h:361
AActor * GetActor() const
Definition ActorChannel.h:187
TArray< class FOutBunch * > QueuedExportBunches
Definition ActorChannel.h:152
TMap< UObject *, TSharedRef< FObjectReplicator > > ReplicationMap
Definition ActorChannel.h:132
EChannelCloseReason QueuedCloseReason
Definition ActorChannel.h:155
ESubObjectDeleteFlag
Definition ActorChannel.h:448
TSet< FNetworkGUID > PendingGuidResolves
Definition ActorChannel.h:138
ENGINE_API bool KeyNeedsToReplicate(int32 ObjID, int32 RepKey)
uint32 bBlockChannelFailure
Definition ActorChannel.h:106
TArray< int32 > PendingObjKeys
Definition ActorChannel.h:367
double QueuedBunchStartTime
Definition ActorChannel.h:136
static ENGINE_API bool CanIgnoreDeprecatedReplicateSubObjects()
void SetSkipRoleSwap(const bool bShouldSkip)
Definition ActorChannel.h:128
TArray< FNetworkGUID > QueuedMustBeMappedGuidsInLastBunch
Definition ActorChannel.h:151
void SetForcedSerializeFromRPC(bool bInFromRPC)
Definition ActorChannel.h:346
virtual bool ReplicateSubobjectCustom(UObject *Obj, FOutBunch &Bunch, const FReplicationFlags &RepFlags)
Definition ActorChannel.h:329
bool GetSkipRoleSwap() const
Definition ActorChannel.h:127
TArray< class FInBunch * > QueuedBunches
Definition ActorChannel.h:135
TSharedPtr< FObjectReplicator > ActorReplicator
Definition ActorChannel.h:130
TMap< int32, FPacketRepKeyInfo > SubobjectNakMap
Definition ActorChannel.h:364
Definition ActorComponent.h:152
Definition Channel.h:63
Definition Class.h:3793
Definition NetConditionGroupManager.h:50
Definition Class.h:2476
Definition NetConnection.h:284
Definition Object.h:95
Definition NetworkVersion.cpp:28
@ false
Definition radaudio_common.h:23
Definition ObjectKey.h:19
Definition EngineTypes.h:3601
Definition ObjectPtr.h:488
Definition WeakObjectPtrTemplates.h:25
Definition ActorChannel.h:353
FPacketRepKeyInfo()
Definition ActorChannel.h:354
int32 PacketID
Definition ActorChannel.h:356
TArray< int32 > ObjKeys
Definition ActorChannel.h:357
Definition NetSubObjectRegistry.h:29