How to check which actor overlap with player

I have 2 actor that might overlap with player, this can be either treasure chest or merchant. I would like to interact with them using “E” key. How to differentiate and have different interaction between them? I have attached the problematic part below

You can do it like this, just connect the second Cast to the first Cast Failed. Basically it will go like this: Is it a merchant? if yes, interact with merchant; if no → Is it a chest? Etc.

However, it’s fine with two types of objects, but the more object types you’ll have, the more difficult it will be to setup. I strongly suggest looking into blueprint interfaces. With them, you can make a function call to any actor without casting, and each actor will do its own thing.

1 Like

I have never used blueprint interface before. Will it be possible if you can show some example on how to do that?

It’s a really common thing to do, you can find tons of videos or tutorials online. E.g. Blueprint Interfaces | More Efficient Interactions | Interact Interface - Unreal Engine 4 Tutorial - YouTube

1 Like

Thank you, I manage to make it work following the video. But I do have some questions, when you want to use interface do you always have to call “Does Implement Interface” function just like the image I attached ?

and you mentioned casting is expensive, so what is the good rule of thumb when to use interface and when to just use cast? I learn Unreal Engine by following tutorials so this kind of topic I usually miss.

Thank you

do you always have to call “Does Implement Interface”

In blueprints you don’t have to do that. You can send any interface call to any actor, and if it doesn’t implement an interface, nothing will happen.

what is the good rule of thumb when to use interface and when to just use cast

I can only say from my experience, but: interfaces are really handy when you need to work with various actors that do different things, but can be controlled in the same way. Interaction is one of such cases.

If you’re working with a few classes with various different unique functions, you don’t really want to use interfaces, because you’ll end up with a lot of interface functions that only do one thing.

My general rule of thumb is if you need to have the same function in more than two different classes, consider using an interface instead. It’s really easy to use in blueprints too.

2 Likes

I mean is it necessary to use “Does Implement Interface” to call an interface or is there other function that does the same as that?

You can just make the interface call without checking anything.

1 Like