Announcement

Collapse
No announcement yet.

How to check if actor implements blueprint interface from C++?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to check if actor implements blueprint interface from C++?

    Hey all.

    I've got a use-case where I want to check if an actor has implemented a blueprint interface and then call an interface method if they have from my C++ code. Anyone got any leads on how to do such a thing?

    I specifically don't want to have to cast actors to base types in this process, so I really want to do what in blueprint would be a "does implement interface" node and then a "call function" node.

    Thanks for any help.

    #2
    Turns out I can just create a blueprintimplementableevent and call it and it will fail silently if it hasn't been implemented, which is what I wanted:

    UFUNCTION(BlueprintImplementableEvent)
    virtual void HandleNonBlockingHit(const FHitResult &hit, const FVector & start, const FVector & end) const;

    Comment


      #3
      You can actually just use Cast<IMyInterface> to see if it's an interface, if you're still interested in knowing.

      Comment


        #4
        Originally posted by cancel View Post
        You can actually just use Cast<IMyInterface> to see if it's an interface, if you're still interested in knowing.
        That would only work for C++ interfaces though. I'm talking about blueprint interfaces. I need the equivalent of "Does implement interface" and some method of calling the interface function if the object does implement the interface. The point here is to not have to know the class of the thing implementing the functionality.

        Comment


          #5
          Originally posted by cancel View Post
          You can actually just use Cast<IMyInterface> to see if it's an interface, if you're still interested in knowing.
          Is this definitely the case?
          When I was experimenting with interfaces not long ago, it seemed there were some limitations, in that Cast<> would only work if the object was of a blueprint derived from a C++ class which itself implemented the interface. If the interface was only implemented at the blueprint level (ie. the parent C++ class of the blueprint didn't implement the interface), Cast<> would return a null pointer.

          There was a workaround for calling interface methods but it was very nasty, and did not actually allow getting an interface pointer to the object.

          ---
          Found the article: https://wiki.unrealengine.com/Interfaces_And_Blueprints
          It's a bit outdated, but as far as I could tell, this was still an issue in 4.6 at least.

          Comment


            #6
            Originally posted by zoombapup View Post
            I need the equivalent of "Does implement interface"
            What about... calling DoesImplementInterface?

            Code:
            UKismetSystemLibrary::DoesImplementInterface( MyObject, UMyInterface::StaticClass() );
            -Camille

            Comment


              #7
              Originally posted by cmartel View Post

              What about... calling DoesImplementInterface?

              Code:
              UKismetSystemLibrary::DoesImplementInterface( MyObject, UMyInterface::StaticClass() );
              -Camille
              This is exactly what I needed, thanks! My goal was to find out if a Blueprint (AnimBlueprint in my case) implemented a C++ Interface.
              If you don't want to include the UKismetSystemLibrary, what the function does is, it checks whether the given Interface StaticClass is actually an Interface and then it calls:

              Code:
              MyObject->GetClass()->ImplementsInterface(UMyInterface::StaticClass());
              So you could just call this line if you are sure UMyInterface is an Interface.

              Comment

              Working...
              X