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"

Calling Interface Functions In C++

I am trying to implement an interaction system via an interface but am having trouble figuring out how to call functions in my interface via C++. I am able to call these interface functions in Blueprints with the message nodes.

Some answers online talk about using Execute_* functions but I can't find any clear description on how to do this. I have tried casting with Cast<>() and InterfaceCast but these cause my project to crash.

Specifically, what I am trying to do is run a trace to see if there are any actors in front of the player pawn that implement my InteractableInterface. And then I want to be able to call the Interact(...) function on that actor. My InteractableInterface code is shown below.

Any help would be appreciated. Thanks very much

    UINTERFACE(meta = (CannotImplementInterfaceInBlueprint))
     class UInteractableInterface : public UInterface
     {
         GENERATED_UINTERFACE_BODY()
     
     };
     
     class IInteractableInterface
     {
         GENERATED_IINTERFACE_BODY()
     
     public:
         UFUNCTION(BlueprintCallable, Category = Interact)
         virtual bool IsInteractable()=0;
     
         UFUNCTION(BlueprintCallable, Category = Interact)
         virtual void Interact(AKillablePawn* Instigator)=0;
     };







Product Version: UE 4.8
Tags:
more ▼

asked Jun 30 '15 at 05:25 AM in C++ Programming

avatar image

Havoc47c
21 1 3 8

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

3 answers: sort voted first

Try this:

 if (YourActor->GetClass()->ImplementsInterface(UInteractableInterface::StaticClass()))
 {
   IInteractableInterface::Execute_Interact(YourActor); // This is the Execute_* function. The asterisk means your function name. :)
 }
more ▼

answered Jun 30 '15 at 06:30 AM

avatar image

ryanjon2040
1.6k 56 43 93

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

Upon compilation I get Execute_Interact is not a member of IInteractableInterface. I found another error in my code and I was able to cast to IInteractable Interface successfully with Cast < InteractableInterface > (ActorRef);

Thanks for the input

more ▼

answered Jun 30 '15 at 07:19 AM

avatar image

Havoc47c
21 1 3 8

avatar image jgm Feb 07 '16 at 07:04 PM

I have this same issue, any updates on the problem?

avatar image ryanjon2040 Feb 08 '16 at 05:11 PM

Execute_Interact was just an example i posted. You need to replace Interact with your function name. For example, if your function name is MyFunction then it should be Execute_MyFunction.

avatar image Paul "TBBle" Hampson May 23 '19 at 07:17 AM

The Execute_* static function only exists on BlueprintImplementable/BlueprintNativeEvent UFUNCTIONs. By using CannotImplementInterfaceInBlueprint, Cast<InteractableInterface> is the correct approach.

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

I use them extensively, here's an example:

.h (blueprint callable interface, but implemented in c++)

 #pragma once
 //this is important for the auto-generated executed functions.
 #include "EntityInterface.generated.h"
 
 /** Interface for actors which can be associated with teams */
 /** Class needed to support InterfaceCast<IToStringInterface>(Object) */
 UINTERFACE(meta = (CannotImplementInterfaceInBlueprint = true))
 class UEntityInterface : public UInterface
 {
     GENERATED_UINTERFACE_BODY()
 };
 
 class IEntityInterface
 {
     GENERATED_IINTERFACE_BODY()
 
     /** returns the owner number of the actor (0=world, otherwise it's a player) */
     UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
     virtual int32 GetOwnerNum() const = 0;
 
     /** sets the owner number of the actor */
     UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
     virtual void SetOwnerNum(const int32 OwnerNum) = 0;
 
     /** returns the id number of the actor */
     UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
     virtual int32 GetEntityId() const = 0;
 
     /** sets the id number of the actor */
     UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
     virtual void SetEntityId(const int32 NewEntityId) = 0;
 
     /** Remove the given amount from entity's healt (net unsafe) */
     UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
     virtual void ApplyDamage(int32 Amount) = 0;
 };

another .h (Blueprint implementable events)

 #pragma once
 
 #include "SelectableInterface.generated.h"
 
 /** Interface for actors which can be associated with teams */
 /** Class needed to support InterfaceCast<IToStringInterface>(Object) */
 UINTERFACE()
 class USelectableInterface : public UInterface
 {
     GENERATED_UINTERFACE_BODY()
 };
 
 class ISelectableInterface
 {
     GENERATED_IINTERFACE_BODY()
 
     /** tries to select actor */
     UFUNCTION(BlueprintImplementableEvent, Category = Selection)
     void OnSelectionGained();
 
     /** tries to deselect actor */
     UFUNCTION(BlueprintImplementableEvent, Category = Selection)
     void OnSelectionLost();
 
     UFUNCTION(BlueprintImplementableEvent, Category = Commands)
     bool DoOnTargetEvent(const TScriptInterface<IEntityInterface>& Target, const TEnumAsByte<EJobType::Type>& JobType);
 };
 

.cpp (tip: you can implement more than one interface in only 1 cpp, as u can see)

 #include "MyProject.h"
 //#include "Interfaces/SelectableInterface.h"
 //#include "Interfaces/MultipleSelectableInterface.h"
 
 USelectableInterface::USelectableInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
 {
 }
 
 UMultipleSelectableInterface::UMultipleSelectableInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
 {
 }
 
 UEntityInterface::UEntityInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
 {
 }

and here's how i call the functions:

 //i have multiple targets at once
 TArray<IEntityInterface*> Subjects;
 //fill the array somehow
 
 //and then call the function on the interface
 //note that this function is only declared in the interface,
 //the implementation is on Blueprints side
 
 for (IEntityInterface* Subject : Subjects)
 {
     ISelectableInterface* s = Cast<ISelectableInterface>(Subject);
     //note that actually i'm using 2 different interfaces
     if (s)
     {
         s->Execute_DoOnTargetEvent(Cast<UObject>(s), Target, Job);
     }
 }



Hope this helps you :)

more ▼

answered Jun 30 '15 at 08:33 AM

avatar image

zamy
396 17 13 34

avatar image zamy Jun 30 '15 at 08:43 AM

I forgot sorry, note that for the execute function you have to provide the UObject context (the Object on which you're calling the function, as a child of UObject: i used a cast to a generic UObject, but if you already have a pointer of your class type, you can use that directly) as first parameter.

(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