Is is possible to cast to whichever Actor is the result of a Line Trace hit?

I’m setting up a function which will allow me to interact with a number of different blueprints with the same line trace setup.

At the moment, I have a Line Trace by Channel working which is only blocked by select BoxComponents in each blueprint I want the player to have some interaction with. My plan here is to fire off a ‘Can Interact’ event within the interactive blueprints whenever the player is looking at them correctly (from different sides, in this case).

With this plan, however, comes a bit of an issue. I’m looking to break the hit results of the line trace and cast to the blueprint actor that the trace hit. When dragging out from the Break Hit node (Hit Actor), it only gives me the option to cast to a specific blueprint. How would I cast to whichever blueprint it was the trace hit?

Thanks in advance!

This is a job for BP Interfaces. Create a BPI with the interactive function names you need, then make sure that all actors that should be interactive implement those functions. Your trace hit can then call the appropriate function on the hit actor without casting. And if the hit actor doesn’t implement the interface, nothing bad happens, it just ignores the call silently. It’s win-win.

Looks like you get it.

The implementation (like in your second screenie) should be made in every class that is set to implement your BI_Interactive. So, if you have an interactive cheese in Cheese_BP, and an interactive salami in Salami_BP, both those BP classes should have a function called Interact with that exact signature (Hit Component and Has Hit input parameters), and they should be set to implement BI_Interactive in their respective Blueprint Props settings.

More on implementation details here:
https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/UsingInterfaces/index.html

Edit: I’m nowhere near my dev box at the moment, so I can’t supply any screenshots unfortunately.

I’ve played around with this a bit more. I think I understand the way the BPIs work. However, if I wanted to carry the Hit Component information through the function too, how would I do this?

I’ve tried adding inputs and outputs to the BPI function, but you can’t seem to edit them, so I can’t connect the input/output together to carry that Hit Component with it.

Thanks again, !

The BPI merely specifies the names and parameters of the functions in the interface. Each individual blueprint that is set to implement the interface then defines what the functions should do in detail. The key thing is that the function signatures are agreed upon, then they can be called by some other process which doesn’t need to know the exact implementation. Each implementing class handles this by itself.

So it’s a two-stage process. First create the BPI with names and parameters only. Next implement said BPI in all the classes that you need to (in this case) interact with using the interface functions. See https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/UsingInterfaces/index.html

Thanks for the help ! I think I understand now how BPIs work (deleted my last comment as it was a little hasty).

If I want to call the Interact function inside the BPI, but also carry the Hit Component information with it as well (I’m actually tracking if they’ve hit a certain component within said actor). How would I achieve this?

I can add inputs/outputs to the BPI function, but I can’t connect them together. I was hoping It worked like custom events do (where the input/output can carry other variable info as well).

If I understand you correctly, you can just add an input parameter on your BPI::Interact(…) function that accepts the applicable hit component information data type. The trace would pass it in when calling, and the respective implementing classes then decide what to do with it.

I guess an image might help to explain what I’m trying to do. I feel like I might be using the function / BPI incorrectly

The idea is that while I want to ‘run’ the function inside the ‘Hit Actor’, I want to also carry with it (through the function?) the ‘Hit Component’ as well as a boolean for the hit result. So that I can then use these variables in the Hit Actor blueprint to decide what to do with the Hit itself.

I imagine the function would replace this event in some way? (With much the same outputs (brought from the trace)

I could be missing the point of BPIs entirely. So forgive me if I’m totally wrong here.

Ahh! I guess I just missed the fact having a BPI function created it’s own event. I’ve got it working now. I should probably spend some time reading into BPIs.

Thanks so much! Love the support this community offers on such a frequent and reliable basis.