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"

Safely Casting An AActor* to IMyInterface* ?

I have the following interface declarations and some function that tries to cast an actor to my interface. Is this an acceptable way to cast to an interface with the Unreal framework?

 UINTERFACE(MinimalAPI)
 class UMyInterface: public UInterface
 {
     GENERATED_BODY()
 };
 
 class MYSTUFF_API IMyInterface
 {
     GENERATED_BODY()
 public:
 }

 
 void USomeClass::someFunction()
 {
         AActor* actor = somehowGetActor();
         if( actor->GetClass()->ImplementsInterface(UMyInterface::StaticCalss()) )
         {
                 void* castAddress = actor->GetInterfaceAddress(UMyInterface::StaticClass());
                 IMyInterface* interface = Cast<IMyInterface>( castAddress );
         }
 }
Product Version: UE 4.21
Tags:
more ▼

asked Jan 17 '19 at 01:37 AM in C++ Programming

avatar image

GarettHunter
35 2 4 8

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Good news! It's much easier than that. You just have to try casting your object to the interface class. If the cast succeeds, that object implements the interface, and you can call your interface functions as desired. If the cast fails, the object does not implement the interface.

So your code might look something like:

 void USomeClass::SomeFunction()
 {
     AActor* actor = SomehowGetActor();
     IMyInterface* TheInterface = Cast<IMyInterface>(actor);
     if(TheInterface)
     {
         TheInterface->Execute_SomeInterfaceFunction();
     }
 }

If you're not familiar with it, check out this wiki page about Interfaces in C++, which explains and shows how to properly use them. Note that the correct way to call interface functions is to use the Execute_ prefix (this is explained further in the wiki page).

more ▼

answered Jan 17 '19 at 09:23 PM

avatar image

the_batch
1k 4 5 8

avatar image GarettHunter Jan 17 '19 at 11:47 PM

Thanks. I will give that a shot. I originally thought I could just cast to IMyInterface like this, but I was not sure. I did not realize that I needed the Execute_ prefix though. Give me some time to test this out and if all goes well I will accept this answer.

avatar image GarettHunter Jan 24 '19 at 07:58 AM

You are correct, except it appears for this one case which is the case that I am in right now: https://answers.unrealengine.com/questions/315990/c-interface-cast-returns-null-when-implemented-on.html

I haven't tested their "solution" since it appears that one person commented the workaround would not work.

(comments are locked)
10|2000 characters needed characters left
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