UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ScriptInterface.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 ScriptInterface.h: Script interface definitions.
5=============================================================================*/
6
7#pragma once
8
10#include "Templates/Casts.h"
12
13#include <type_traits>
14
22{
23private:
27 TObjectPtr<UObject> ObjectPointer = nullptr;
28
32 void* InterfacePointer = nullptr;
33
34protected:
38 COREUOBJECT_API FArchive& Serialize(FArchive& Ar, class UClass* InterfaceType);
39
40public:
44 FScriptInterface() = default;
45
50 : ObjectPointer(InObjectPointer), InterfacePointer(InInterfacePointer)
51 {}
52
58
63 {
64 return ObjectPointer;
65 }
66
71 {
72 return ObjectPointer;
73 }
74
79 {
80 // Only access the InterfacePointer if we have a valid ObjectPointer. This is necessary because garbage collection may only clear the ObjectPointer.
81 // This will also return null for objects that only implement the interface in a blueprint class because there is no native representation.
82 return ObjectPointer ? InterfacePointer : nullptr;
83 }
84
89 {
90 ObjectPointer = InObjectPointer;
91 if ( ObjectPointer == nullptr )
92 {
93 SetInterface(nullptr);
94 }
95 }
96
101 {
102 InterfacePointer = InInterfacePointer;
103 }
104
109 {
110 return GetInterface() == Other.GetInterface() && ObjectPointer == Other.GetObject();
111 }
113 {
114 return GetInterface() != Other.GetInterface() || ObjectPointer != Other.GetObject();
115 }
116
118 {
119 Collector.AddReferencedObject(ObjectPointer);
120 }
121
123 {
124 return GetTypeHash(Instance.InterfacePointer);
125 }
126};
127
128
129
130template<> struct TIsPODType<class FScriptInterface> { enum { Value = true }; };
131template<> struct TIsZeroConstructType<class FScriptInterface> { enum { Value = true }; };
132
137template <typename InInterfaceType>
139{
140public:
143
147 TScriptInterface() = default;
148
153
157 template <
158 typename U
159 UE_REQUIRES(std::is_convertible_v<U, UObjectType*>)
160 >
161 inline TScriptInterface(U&& Source)
162 {
163 // Always set the object
164 UObjectType* SourceObject = ImplicitConv<UObjectType*>(Source);
165 SetObject(SourceObject);
166
167 if constexpr (std::is_base_of<InInterfaceType, std::remove_pointer_t<std::remove_reference_t<U>>>::value)
168 {
169 // If we know at compile time that we got passed some subclass of InInterfaceType, set it
170 // without a cast (avoiding the cast also allows us to not require linking to its module)
171 SetInterface(Source);
172 }
173 else
174 {
175 // Tries to set the native interface instance, this will set it to null for BP-implemented interfaces
178 }
179 }
180
184 template <
185 typename OtherInterfaceType
186 UE_REQUIRES(std::is_convertible_v<OtherInterfaceType*, InInterfaceType*>)
187 >
195
199 template <typename ObjectType>
201 {
202 // Always set the object
203 SetObject(SourceObject);
204
205 if constexpr (std::is_base_of<InInterfaceType, ObjectType>::value)
206 {
207 // If we know at compile time that we got passed some subclass of InInterfaceType, set it
208 // without a cast (avoiding the cast also allows us to not require linking to its module)
209 SetInterface(SourceObject.Get());
210 }
211 else
212 {
213 // Tries to set the native interface instance, this will set it to null for BP-implemented interfaces
216 }
217 }
218
224
229 {
230 *this = TScriptInterface();
231 return *this;
232 }
233
237 template <
238 typename U
239 UE_REQUIRES(std::is_convertible_v<U, UObjectType*>)
240 >
242 {
243 *this = TScriptInterface(Source);
244 return *this;
245 }
246
250 template <
251 typename OtherInterfaceType
252 UE_REQUIRES(std::is_convertible_v<OtherInterfaceType*, InInterfaceType*>)
253 >
259
263 template <typename ObjectType>
265 {
266 *this = TScriptInterface(SourceObject);
267 return *this;
268 }
269
273 template <
274 typename OtherInterface
275 UE_REQUIRES(std::is_convertible_v<OtherInterface*, InInterfaceType*>)
276 >
278 {
279 return GetInterface() == Other;
280 }
281 template <
282 typename OtherInterface
283 UE_REQUIRES(std::is_convertible_v<OtherInterface*, InInterfaceType*>)
284 >
286 {
287 return GetInterface() != Other;
288 }
289
294 {
295 return GetInterface() == Other.GetInterface() && GetObject() == Other.GetObject();
296 }
298 {
299 return GetInterface() != Other.GetInterface() || GetObject() != Other.GetObject();
300 }
301
306 {
307 return GetInterface() == nullptr;
308 }
310 {
311 return GetInterface() != nullptr;
312 }
313
318 {
319 return GetInterface();
320 }
321
328 {
329 return *GetInterface();
330 }
331
339
347
355
363
371
376 UE_FORCEINLINE_HINT explicit operator bool() const
377 {
378 return GetInterface() != nullptr;
379 }
380
381 friend FArchive& operator<<( FArchive& Ar, TScriptInterface& Interface )
382 {
383 return Interface.Serialize(Ar, InInterfaceType::UClassType::StaticClass());
384 }
385};
386
387template <typename InterfaceType>
388struct TCallTraits<TScriptInterface<InterfaceType>> : public TCallTraitsBase<TScriptInterface<InterfaceType>>
389{
391};
typename TCopyQualifiersFromTo< From, To >::Type TCopyQualifiersFromTo_T
Definition CopyQualifiersFromTo.h:17
FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR
The type of the C++ nullptr keyword.
Definition Platform.h:1157
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
T * ToRawPtr(const TObjectPtr< T > &Ptr)
Definition ObjectPtr.h:1000
const bool
Definition NetworkReplayStreaming.h:178
#define UE_REQUIRES(...)
Definition Requires.h:86
UEBinkAudioDecodeInterface * Interface
Definition binka_ue_decode_test.cpp:24
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition UObjectGlobals.h:2492
Definition ScriptInterface.h:22
UE_FORCEINLINE_HINT void SetInterface(void *InInterfacePointer)
Definition ScriptInterface.h:100
UE_FORCEINLINE_HINT void * GetInterface() const
Definition ScriptInterface.h:78
void SetObject(UObject *InObjectPointer)
Definition ScriptInterface.h:88
UE_FORCEINLINE_HINT bool operator!=(const FScriptInterface &Other) const
Definition ScriptInterface.h:112
UE_FORCEINLINE_HINT bool operator==(const FScriptInterface &Other) const
Definition ScriptInterface.h:108
friend uint32 GetTypeHash(const FScriptInterface &Instance)
Definition ScriptInterface.h:122
FScriptInterface()=default
FScriptInterface(const FScriptInterface &)=default
UE_FORCEINLINE_HINT UObject * GetObject() const
Definition ScriptInterface.h:62
FScriptInterface(UObject *InObjectPointer, void *InInterfacePointer)
Definition ScriptInterface.h:49
FScriptInterface & operator=(const FScriptInterface &)=default
void AddReferencedObjects(FReferenceCollector &Collector)
Definition ScriptInterface.h:117
UE_FORCEINLINE_HINT TObjectPtr< UObject > & GetObjectRef()
Definition ScriptInterface.h:70
Definition ScriptInterface.h:139
UE_FORCEINLINE_HINT void SetInterface(InInterfaceType *InInterfacePointer)
Definition ScriptInterface.h:343
UE_FORCEINLINE_HINT InInterfaceType & operator*() const
Definition ScriptInterface.h:327
TScriptInterface & operator=(U &&Source)
Definition ScriptInterface.h:241
UE_FORCEINLINE_HINT bool operator==(TYPE_OF_NULLPTR) const
Definition ScriptInterface.h:305
UE_FORCEINLINE_HINT bool operator!=(TYPE_OF_NULLPTR) const
Definition ScriptInterface.h:309
TScriptInterface & operator=(const TScriptInterface< OtherInterfaceType > &Other)
Definition ScriptInterface.h:254
UE_FORCEINLINE_HINT InInterfaceType * GetInterface() const
Definition ScriptInterface.h:335
TScriptInterface(TObjectPtr< ObjectType > SourceObject)
Definition ScriptInterface.h:200
TScriptInterface(const TScriptInterface &)=default
InInterfaceType InterfaceType
Definition ScriptInterface.h:141
TScriptInterface(const TScriptInterface< OtherInterfaceType > &Other)
Definition ScriptInterface.h:188
UE_FORCEINLINE_HINT InInterfaceType * operator->() const
Definition ScriptInterface.h:317
UE_FORCEINLINE_HINT UObjectType * GetObject() const
Definition ScriptInterface.h:351
TCopyQualifiersFromTo_T< InterfaceType, UObject > UObjectType
Definition ScriptInterface.h:142
UE_FORCEINLINE_HINT bool operator!=(const OtherInterface *Other) const
Definition ScriptInterface.h:285
TScriptInterface & operator=(TObjectPtr< ObjectType > SourceObject)
Definition ScriptInterface.h:264
UE_FORCEINLINE_HINT bool operator!=(const TScriptInterface &Other) const
Definition ScriptInterface.h:297
TScriptInterface & operator=(const TScriptInterface &)=default
TScriptInterface(TYPE_OF_NULLPTR)
Definition ScriptInterface.h:152
TScriptInterface(U &&Source)
Definition ScriptInterface.h:161
UE_FORCEINLINE_HINT bool operator==(const OtherInterface *Other) const
Definition ScriptInterface.h:277
UE_FORCEINLINE_HINT void SetObject(UObjectType *InObjectPointer)
Definition ScriptInterface.h:367
friend FArchive & operator<<(FArchive &Ar, TScriptInterface &Interface)
Definition ScriptInterface.h:381
TScriptInterface()=default
UE_FORCEINLINE_HINT bool operator==(const TScriptInterface &Other) const
Definition ScriptInterface.h:293
TScriptInterface & operator=(TYPE_OF_NULLPTR)
Definition ScriptInterface.h:228
UE_FORCEINLINE_HINT TObjectPtr< UObjectType > & GetObjectRef()
Definition ScriptInterface.h:359
Definition Class.h:3793
Definition Object.h:95
Definition UnrealTypeTraits.h:267
Definition UnrealTypeTraits.h:283
Definition IsPODType.h:12
@ Value
Definition IsPODType.h:13
Definition UnrealTypeTraits.h:172
Definition ObjectPtr.h:488
FORCEINLINE T * Get() const
Definition ObjectPtr.h:664