Ivan3z
(Ivan3z)
June 10, 2023, 1:59am
1
have an interface that does not contain UFUNCTIONS
UINTERFACE()
class UMyInteface : public UInterface
{
GENERATED_BODY()
};
class IMyInteface
{
GENERATED_BODY()
public:
virtual MyFunction();
};
1-If I use Cast< UClass >() I get a pointer to UMyInteface
UMyInteface* UPTR = Cast(UObj)
2-If I use Cast< Class >() get a compile error.
UMyInteface* IPTR = Cast< IMyInteface >(UObj)
3- If I use dinamic_cast< Class >() I get a compile error.
dynamic_cast< IMyInteface >(UObj)
4- If I use cast C style i not sure what will happend.
if (Owner->Implements< UMyInteface >()) return;
IMyInteface * MyIntefacePtr = (IMyInteface *)UObj;
if (MyIntefacePtr == nullptr) return;
In this case:
Which is the right way to do it? or the safest way?
Because I suppose that in this case the function must be called like this:
MyIntefacePtr->MyFunction();
and not in this other way.
IMyInteface::Execute_MyFunction( UPTR );
Thank you so much!!
Posts 5 and 7 might help you
Curious, what did you benchmark there? In the editor with all other things running at the same time it’s hard to be accurate… maybe when testing a million runs I don’t know XD.
There’s something else going on with interfaces, in c++ you have to do multiple checks to get a proper interface cast depending on if the interface is blueprint only or not. Extremely frustrating.
bool bCanBeGrabbed = false;
if (OtherOwner->Implements<UGrabInteractionInterface>()) {
bCanBeGrabbed = IGrabInteractionInt…
Can’t tell you how this behaves with no UFUNCTION macro since it might not be able to reflect without one. Normally you declare your functions as UFUNCTION in the IMyInterface.
I believe that the only time you use
Implements<UGrabInteractionInterface>()
is when you need to test a blueprint implementation, but I might be wrong. If a method is not UFUNCTION it can’t be implemented by a blueprint.
Another way is to cast to it
else if (const IGrabInteractionInterface* OtherInterface = Cast<IGrabInteractionInterface>(OtherOwner)) {
bCanBeGrabbed = OtherInterface->CanBeGrabbed(InPrimitiveComponent);
}
My post 5 on that link gives some info on when and why, but Chatouille in post 7 says things might be different in current versions of the engine.
2 Likes
Ivan3z
(Ivan3z)
June 10, 2023, 3:18am
3
Thank you very much for your help Roy!!
Very appreciated!!
2 Likes
Ivan3z
(Ivan3z)
June 10, 2023, 3:27am
4
It is not for blueprint… that’s why I don’t use UFUCTION…
But it wouldn’t let me derive from a pure class in C++… it had to be derived from UObject… However, being derived from UObject also has very useful benefits… so it’s fine… but I don’t need UFUCTIONS…
Thank you so much!!
system
(system)
Closed
July 10, 2023, 3:27am
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.