Download

BP variable as interface type

I understand basic purpose of using interfaces in blueprints. An object implements a defined interface, then when another object has a reference to it, it can test if it implements the interface, and optionally call some interface functions, which the first object can then handle.

I’m looking to do something slightly different, and more in line with how interfaces are used in C++ I want to create a variable on an Actor BP, and give that variable an interface type that I have defined. Then I want to assign that variable to an object that has implemented the interface. I can do the first two steps, but I cannot seem to be able to assign an object that implements the interface.

For example, I’ve made an “Animal” interface:

https://forums.unrealengine.com/core/image/gif;base64

And I’ve defined a single method “Noise” which returns a string:

https://forums.unrealengine.com/core/image/gif;base64

Now I have a new BP called “Animal Owner” that has a variable of type Animal Interface

https://forums.unrealengine.com/core/image/gif;base64

And on its BeginPlay, it simply calls the Animal variable’s “Noise” function

https://forums.unrealengine.com/core/image/gif;base64

Lastly, I have a “Cat” BP that implements the Animal Interface

https://forums.unrealengine.com/core/image/gif;base64

https://forums.unrealengine.com/core/image/gif;base64

All of this seems to be set up so that it would work. But I can’t assign the Cat to the Animal Owner’s Animal variable. It doesn’t list the Cat BP from the dropdown, and drag and drop doesn’t work either, even though Cat implements the Animal interface:

https://forums.unrealengine.com/core/image/gif;base64

Anybody have an idea what I’m doing wrong? The only thing I can imagine is that “you can’t do that.” But then I would ask why can you even make a variable to have an interface type if you can’t assign anything to it? Thanks

Bump. I’ve been searching for a few days around this subject. It doesn’t seem like anyone has tried to do anything similar. I did find a workaround though, in case anyone else stumbles across this.

The solution is to create a “base” object that implements the Animal Interface. I called mine AnimalBase. On the AnimalOwner object, instead of having a variable of type Animal Interface, have it be of type AnimalBase. For the Cat object, instead of it implementing AnimalInterface directly, have its parent class be AnimalBase (instead of Actor). You will see AnimalInterface listed under the “inherited interfaces”.

You can now assign Cat to AnimalOwner’s Animal variable, and you don’t need to cast it in order to call the “Noise” function.

Is this still true? Why does UE4 even allow you to set Interface as a property type if you can’t assign to it? It’s either stupid engine design or I’m missing something… Hopefully it’s the second one, as doing the workaround @amoffat suggests defeats the purpose of the interface existing in the first place

Agrees, came to the same conclusion and hope this is fixed in some way.
I want to create a interface and use it for e.g. variables, but unfortunately this is not possible