Hello, I found that when i using the Interface as a memeber, ‘TScriptInterface’ is the right way for it.
But TScriptInterface’s InterfacePointer is always null.
I’ve seen several questions like this, but none of them has been answered.
TScriptInterface<IHighlightInterface> CurHighlightActor;
I tried two ways :
CurHighlightActor.SetInterface(Cast<IHighlightInterface>(CursorHit.GetActor()));
CurHighlightActor.SetObject(CursorHit.GetActor());
and
CurHighlightActor = CursorHit.GetActor();
but both of them not worked.
No check for ‘CursorHit.GetActor()’ is implementing (can be casted) IHighlightInterface,
and also it always has NULL InterfacePointer.
And I can’t find any specific documentation of TScriptInterface.
Does anybody know how to use this well?
Just answer myself.
TScriptInterface is just Wrapper for Interface.
If the Interface Cast succeeded, the TScripInterface->GetInerface() is Casted Interface class.
If not, TScriptInterface->GetInterface() would return nullptr.
So if you need to check the cast was successful,
CurHighlightActor = CursorHit.GetActor();
if (CurHighlightActor.GetInterface())
{
// do Something
}
And according to TScriptInterface :
template <typename ObjectType>
TScriptInterface& operator=(TObjectPtr<ObjectType> SourceObject)
{
*this = TScriptInterface(SourceObject);
return *this;
}
template <typename ObjectType>
TScriptInterface(TObjectPtr<ObjectType> SourceObject)
{
SetObject(SourceObject);
InterfaceType* SourceInterface = Cast<InterfaceType>(ToRawPtr(SourceObject));
SetInterface(SourceInterface);
}
The operator = set the Object to SourceObject and automatically Cast it to specific InterfaceType, and Set Interface for it.
So we need to check its SourceInterface, and it could be done with GetInterface() Method.
1 Like