Casting Actor to Interface Problem & Work Around

I am confused about how to cast an actor to an interface using blueprint

I have an interface called UseableItem. I use get collision event to find out when player touches an item to pick it up. Now EventActorBeginOverlap macro returns other actor we are overlapping. I can check if other actor implements my UseableItem interface. But once I find out Actor implements interface, I don’t see how to call interface function Pickup because I can’t cast Actor to my UseableItem interface.

Clearly I seem to misunderstand something about interfaces. It seems to me that casting an Actor as returned by EventActorBeginOverlap to an interface is pretty important. How can I do this or what should I be doing instead?

work around I have found is as follows. Instead of creating an interface, I create a class called GenericUseableItem which inherits from Actor. Then I can cast to that. problem is since multiple inheritance is not allowed I can only use this workaround for one kind of interface.

Please advise.

Thanks,
-X

After further attempts to work around this I think I can clarify problem and work around better:

  1. Interfaces look useful and like good programming practice so I tried to design an interface for UseableItem and ItemUser. Naturally these interfaces would like to call each other so function signatures for both were initially written to take in ItemUser or UseableItem interface references as input.
  2. Some UE4 features such as EventActorBeginOverlap return an actor.
  3. It does not seem possible to cast such an actor to an interface although you can call interface functions via messages. It seems like UE4 designers thought that being able to call interface functions via messages meant that casting actor to an interface was unnecessary.
  4. Because I can’t cast actor to an interface, initial function signatures for ItemUser and UseableItem won’t work because they except interface references as input but actor I have cannot be cast to appropriate interface.

So work around is: NEVER use an interface reference as an input to a function because you may not be able to cast an actor into that interface.

Obviously this is somewhat clunky for lots of reasons. It would be great if Epic could make it so that you could cast actor to interface. If someone from epic reads this and needs me to list reasons its clunky, let me know. One simple answer is that without that ability I have to pass actor around instead of an interface which means that functions should really be checking if their input actors actually implement desired interface.

Further comments welcome.

Hi Xarol,

As you mentioned, you can call an interface function via a message on an Actor reference. I’m not certain I see where this limits your options. Can you create a clear instance of when this does not do what you need and show me some images of your setup? If you’d prefer, you can create a test project and upload that somewhere for me. Thanks!

You can call an interface message on an actor no worries, that simply looks like this:

Overlap event > check overlap actor does implement interface: ItemUser > If true > InterfaceMessage: Pickup

I feel like what you’re saying then is that your interface message Pickup wants to take a parameter of type: UseableItem, and you want to feed self into that parameter and it won’t let you.

If this is case, problem is your actor isn’t implementing interface.

If an example class ‘HealthPotion’ Implements interface ‘UseableItem’, then you’ll be able to feed anything of type ‘HealthPotion’ into a parameter of type ‘UseableItem’.

Thank you for comment but I I think you are missing point. You can call an interface message an any actor but you can’t pass an actor into a function which takes an interface.

Imagine your team member creates a blueprint function library with a function called HideUseableItem which takes a UseableItem interface as input. Furthermore, imagine that you call one of many built-in UE4 functions which return an actor (e.g., to find overlapping actors or line trace or find nearby actors or whatever). You get an actor from UE4 function, you can check if it implements interface, but even if it does you can’t pass actor into HideUseableItem function because you have a generic Actor object and you can’t cast to UseableItem interface.

fundamental problem is this:

  1. You can create functions which take in a UseableItem interface (or in general any interface).
  2. Many UE4 built-in functions give you an actor.
  3. Although you can verify actor implements interface you cannot cast actor to interface to pass into function.

To say it in one sentence: creating functions with an input type you cannot cast to is going to cause problems.

Please let me know if that wasn’t clear or if you think I misunderstood something.

Thanks,
-X

Thanks for quick response. See my comment on answer from .

Just to try and answer again, I don’t think a test project or image is required. Imagine my team member creates a blueprint function library with a function foo that requires an instance of UseableItem interface as input. If I get an actor as a result of some UE4 built-in function I have no way to pass my actor into function foo without a cast.

Your answer could be “Don’t use blueprint function libraries or blueprint macro libraries.” I guess that might solve problem but at cost of generating a lot of extra work.

Let me ask question in a different way: what is purpose of having a type such as interface which you cannot cast to?

Thanks,
-X

Ahhh I understand what you’re better now.

This isn’t so much a problem with system as just a nuance of context sensitive menu.
reason you can’t cast an actor to an interface type is that you can generally only downcast. An interface is not a type of actor, therefore you can’t cast an actor to an interface.

Interfaces are types of objects, but UE4 avoids using objects in favor of actors pretty much entirely, but in this case it’s what you want.

If you click context sensitive off, then simply search ‘Cast To UseableItem’ node will come up, then you can just feed your actor output into node and you’re good to go.

Holy mackerel, do I feel stupid!

You are completely correct. If I turned off context sensitive then this would work. I’m embarrassed that I wrote up a question and went on and on about this issue without realizing that.

Thanks for clearing that up for me: your solution works. Very helpful.

How do we go about casting to an interface in C++? I’ve tried many of things suggested elsewhere and nothing worked.
EDIT: Nevermind, found something:(care of )

auto t = Cast<ITargetInterface>(f.GetActor());
if (t != nullptr){
TScriptInterface<ITargetInterface> s = TScriptInterface<ITargetInterface>();
s.SetInterface(t);
s.SetObject(f.GetActor());
}

Is code not showing up? I did post something. Variable s is Actor cast to an interface.

Could you please post “something”?