x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Make an interface that is only implementable by C++ classes

Is it possible to make an interface that is only implementable by C++ classes, and not blueprints? I couldn't find any documentation about the UINTERFACE macro, so I have no idea what I can do with it.

Product Version: UE 4.14
Tags:
more ▼

asked Nov 25 '16 at 09:13 PM in C++ Programming

avatar image

Or Aviram
1 2 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Just declare a regular C++ interface class without the UINTERFACE attribute.

 class IMyInterface
 {
 };
 
 UCLASS()
 class UMyObject
     : public UObject
     , public IMyInterface
 {
 };
more ▼

answered Nov 26 '16 at 03:58 PM

avatar image Or Aviram Dec 03 '16 at 03:07 PM

Sorry for the delay... I saw your answer pretty long ago, and I tried to implement it...I didn't really work.

First of all, yes, I could make a normal C++ interface and it wasn't exposed to blueprints. But it still isn't perfect.

So the interface is there because I wanted to have a generic AI controller that will be able to control many pawns. So that interface is called IAICharacter, and it is declared like that: class REAL3DPLATFORMER_API IAICharacter {

 public:
     FORCEINLINE virtual class UBehaviorTree* GetBehaviorTree() const = 0;
 
 };

This is an interface all characters that are expected to have an AI controller should have. So inside of the Possess function in the controller, I wanted to get access to that method I put in the interface. So I wanted to cast the possessed pawn into the interface, and call the method. Here are the kind of casts I used:

     Character = Cast<IAICharacter>(PossessedPawn);
     Character = dynamic_cast<IAICharacter*>(PossessedPawn);
     Character = static_cast<IAICharacter*>(PossessedPawn);    

(Keep in mind that PossessedPawn is the parameter the Possess function takes. And that Character is an IAICharacter pointer.) So the first one didn't work because IAICharacter is not a UObject. The second one didn't work because Unreal doesn't allow RTTI...which I personally find kinda stupid, to be honest. But I guess they have their reasons. The third one didn't work because...it says it can't convert from APawn* to IAICharacter*, which actually makes sense.

But if those three casts didn't work... What can I use?

avatar image gmpreussner STAFF Dec 03 '16 at 08:50 PM

What you're trying to do is called down-casting, and it's bad design. You shouldn't do it. Either way, a C-style cast should work:

 IAICharacter* Character = (IAICharacter*)PossessedPawn;
avatar image Or Aviram Dec 04 '16 at 07:45 AM

I forgot to say...I already tried a C-style cast. But it crashes. Probably because C-style casts work in a way where if they succeed, they return the object as the type it is casted to, else they thrown an exception (and Unreal crashes whenever an exception is thrown).

This is not what I want. I want to return a null pointer if it fails, so I can use an if statement to check if it's safe to use it. This is basically what dynamic_cast does, but since Unreal doesn't allow RTTI, I am not able to use.

Coming from C#, it was as easy as adding the "as" operator after the object, and then putting the interface name. It returned null if it failed, and the object as the new type if it succeed. Sounds like dynamic_cast to me, so I don't see what's the problem with that (especially that I know Unity supports it without any problem, so it kinda confuses me why Epic decided not to allow using it...).

What do you recommend me do if it's bad to use downcasting? And if in this case I need to use downcasting, which cast can I use?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question