UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
NetTestHelpers.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "HAL/Platform.h"
6#include "Engine/NetDriver.h"
7
8#include "Containers/Array.h"
11
13class UGameInstance;
16
18
19#if WITH_EDITOR
20
21namespace UE::Net
22{
23
24template<class T>
26{
28 UGameplayStatics::GetAllActorsOfClass(Cast<UObject>(World), T::StaticClass(), Actors);
29 return Actors.Num();
30}
31
32/*
33 * Used to set a cvar and restore it to it's original value when destroyed
34 */
36{
37public:
40
44 FScopedCVarOverrideInt& operator=(const FScopedCVarOverrideInt&) = delete;
45
46private:
47 IConsoleVariable* Variable = nullptr;
48 int32 SavedValue = 0;
49};
50
52{
53public:
54 ENGINE_API FScopedCVarOverrideFloat(const TCHAR* VariableName, float Value);
56
60 FScopedCVarOverrideFloat& operator=(const FScopedCVarOverrideFloat&) = delete;
61
62private:
63 IConsoleVariable* Variable = nullptr;
64 float SavedValue = 0;
65};
66
72{
73public:
76
79 FScopedTestSettings& operator=(FScopedTestSettings&&) = delete;
80 FScopedTestSettings& operator=(const FScopedTestSettings&) = delete;
81
82private:
88};
89
94{
95public:
96
99
104
105private:
106
107 // Restores PIE world context
108 UWorld* OldGWorld = nullptr;
109 int32 OldPIEID = 0;
110 bool OldGIsPlayInEditorWorld = false;
111};
112
118{
119 struct FContext;
120
121public:
122 ENGINE_API static FTestWorldInstance CreateServer(const TCHAR* InURL, const EReplicationSystem ReplicationSystem = EReplicationSystem::Default);
123 ENGINE_API static FTestWorldInstance CreateClient(int32 ServerPort, int32 NumLocalPlayers = 1);
125
127
128 FTestWorldInstance(const FTestWorldInstance&) = delete;
129 FTestWorldInstance& operator=(const FTestWorldInstance&) = delete;
130
131 explicit FTestWorldInstance(bool bDelayedInit) : GameInstance(nullptr) {}
132
135
137
138 ENGINE_API FContext GetTestContext() const;
139
140 ENGINE_API UWorld* GetWorld() const;
141 ENGINE_API UNetDriver* GetNetDriver() const;
142
143 ENGINE_API int32 GetPort();
144
145 ENGINE_API void Tick(float DeltaSeconds = 0.0166f);
146
147 ENGINE_API void LoadStreamingLevel(FName LevelName);
148 ENGINE_API void UnloadStreamingLevel(FName LevelName);
149
150public:
151
152 struct FContext
153 {
154 UWorld* World = nullptr;
155 UNetDriver* NetDriver = nullptr;
158
159 bool IsValid() const
160 {
161 return World && NetDriver && IrisRepSystem && IrisRepBridge;
162 }
163 };
164
165public:
166
167 UGameInstance* GameInstance = nullptr;
168
169private:
171
172 void Shutdown();
173
175
177};
178
183struct FTestWorlds
184{
187
193 ENGINE_API FTestWorlds(const FString& MapName, const FString& GameModeName, const EReplicationSystem ReplicationSystem = EReplicationSystem::Default);
194
196 ENGINE_API explicit FTestWorlds(const TCHAR* ServerURL, const EReplicationSystem ReplicationSystem = EReplicationSystem::Default);
197
199
201 float GetTickDeltaSeconds() const { return TickDeltaSeconds; }
202
205
208
211 ENGINE_API void TickServer();
212 ENGINE_API void TickClients();
213
217
221
226 template<class PredicateT>
227 bool TickAllUntil(const PredicateT& Predicate, float DeltaSeconds = 0.0166f, int32 MaxTicks = 60);
228
231
234
242
243 template<typename T>
245 {
247 }
248
255
261
262public:
263
267
270 FOnClientConnected OnClientConnected;
271
272public:
273
277
278private:
279
280 void InitDelegates();
281
282 void OnNetDriverCreated(UWorld* InWorld, UNetDriver* InNetDriver);
284
285private:
286
287 float TickDeltaSeconds = 0.0166f;
288
289 // Sets up important settings for the networking system to run optimally
291
292 // Restore GWorld and other PIE settings
294};
295
296//------------------------------------------------------------------------
297// Inline functions
298//------------------------------------------------------------------------
299
300template<class PredicateT>
301inline bool FTestWorlds::TickAllUntil(const PredicateT& Predicate, float DeltaSeconds, int32 MaxTicks)
302{
303 PreTickAllDelegate.Broadcast();
304
305 int32 TickCount = 0;
306 bool bPredicateResult = Predicate();
307
308 while (!bPredicateResult && TickCount < MaxTicks)
309 {
310 Server.Tick(DeltaSeconds);
312 {
313 Client.Tick(DeltaSeconds);
314 }
315 TickCount++;
317 bPredicateResult = Predicate();
318 }
319
320 return bPredicateResult;
321}
322
325
326} // end namespace UE::Net
327
328#endif //WITH_EDITOR
uint64 GFrameCounter
Definition CoreGlobals.cpp:418
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
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
#define DECLARE_MULTICAST_DELEGATE(DelegateName)
Definition DelegateCombinations.h:23
#define DECLARE_MULTICAST_DELEGATE_OneParam(DelegateName, Param1Type)
Definition DelegateCombinations.h:49
EReplicationSystem
Definition NetEnums.h:85
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition PlayerController.h:261
Definition IDelegateInstance.h:14
Definition NameTypes.h:617
Definition IConsoleManager.h:558
Definition Array.h:670
Definition GameInstance.h:152
Definition NetDriver.h:799
Definition ObjectReplicationBridge.h:83
Definition Object.h:95
Definition ReplicationSystem.h:70
Definition World.h:918
FString GetWorldContext(UWorld *InWorld)
Definition AutomationCommon.cpp:745
const FName NumActors("NumActors")
Definition NetworkMetricsDefs.h:67
Definition NetworkVersion.cpp:28
Definition Engine.h:334