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"

How do i expose a C++ Interface to Blueprints with a BlueprintCallable function?

Hello i have an interface that is implemented by some of my c++ classes, and each one overrides a function that returns it's own value for each class. The problem is that i want to expose the interface to blueprints (by using UINTERFACE(Blueprintable)) and i just want to call that function in blueprints, but the compiler doesn't allow me to have BlueprintCallable modifiers in Blueprintable interfaces, only BlueprintImplementableEvent is allowed (which i don't want). What else can i do to make this work? Thank you.

Product Version: UE 4.10
Tags:
more ▼

asked Dec 21 '15 at 04:11 PM in C++ Programming

avatar image

xlar8or
6k 161 129 304

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

1 answer: sort voted first

I've faced this issue before. What works is to use both BlueprintNativeEvent and BlueprintCallable together while declaring your interface function. This optionally allows you to override it in BP as well, but in your case you don't need to as you're just interested in calling the function. Like this:

 UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "SomeCategory")
 void SomeInterfaceFunction();


Be sure to use this specific format to declare the function in whichever C++ class is implementing it:

 // format:
 //virtual <RetType> <YourFunctionName>_Implementation() override;
 
 // from the example above:
 virtual void SomeInterfaceFunction_Implementation() override;


This is necessary because the generated code behind the scenes is declared in this manner. Implement this function in your CPP with the this name too (i.e. _Implementation needs to be appended to the function name).

This should work fine - I just setup an interface like this now for my own work and I'm able to call it from Blueprints.

PS: Some additional information that may help down the line - if you do decide to implement your interface function in a Blueprint you might need to use the syntax Execute_SomeInterfaceFunction(UObject* YourObject) to call those interface functions from C++.

For a more detailed explanation about why all this is necessary check out this writeup and this wiki


[UPDATE - 12th Jan 2016]

I just stumbled upon some of my old project code that does exactly what you ask for even without needing BlueprintNativeEvent. Looks like marking the interface as CannotImplementInterfaceInBlueprint is key.

Here's an example (dummy names):

 UINTERFACE(meta = (CannotImplementInterfaceInBlueprint))
 class UMyInterface : public UInterface
 {
     GENERATED_UINTERFACE_BODY()
 };
 
 class IMyInterface
 {
     GENERATED_IINTERFACE_BODY()
 
     UFUNCTION(BlueprintCallable, Category = SomeCategory)
     virtual void SomeInterfaceFunction();
 };


more ▼

answered Dec 27 '15 at 04:47 PM

avatar image

VSZ
939 24 28 55

avatar image Blind Gazer Jan 28 '19 at 05:46 PM

Thanks, CannotImplementInterfaceInBlueprint works

avatar image vle07 Feb 15 '19 at 10:26 PM

When using "CannotImplementInterfaceInBlueprint", how did you override the interface functions in your c++ classes? I keep running into compilation error LNK2001, now matter how I declare the functions in my c++ class. Thanks.

avatar image Blind Gazer Feb 16 '19 at 02:02 PM

Create SomeInterface.h and empty SomeInterface.cpp (only include "SomeInterface.h"):

 #include "UObject/Interface.h"
 #include "SomeInterface.generated.h"
 
  UINTERFACE(Blueprintable, MinimalAPI, meta = (CannotImplementInterfaceInBlueprint))
     class USomeInterface: public UInterface
     {
         GENERATED_BODY()
     };
     
     
     class ISomeInterface
     {
         GENERATED_BODY()
     
     public:
     
         UFUNCTION(BlueprintCallable)
         virtual void SomeAction() = 0;
     };

Than create implementation:

 UCLASS()
 class ASomeImplementation : public ISomeInterface
 {
     GENERATED_BODY()
 
 public:
 
     void SomeAction() override;
 };
 

   

Note: This work in UE 4.21.1

avatar image vle07 Feb 16 '19 at 07:18 PM

Thanks! Turns out I needed to make the function declaration in the interface pure virtual by adding "= 0" to the end of the function. I need to brush up on my C++ knowledge. Thanks again for the help :)

avatar image cuwaki Apr 18 '19 at 03:35 AM

thank you!

(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