Decoupling blueprint functionality

In my characterBP I have a line trace (channel) -this works great and it outputs the actor it hits without problem. This is used for aim activation of objects.

I added the line trace in the character blueprint because that’s where it belongs in this case. The character is doing a scan. Now normally you’d simply just get the actor output from the line scan, cast it to the appropriate object and call a function.

For example:
Character walks in front of a door. The character line scan clips the door and outputs the actor (door). Then you’d cast that as a DoorBP and run the function connected to it called DoorOpenFunction.

The thing is that if you add the casting to the character blue print you’re going to have a lot of behavior not directly related to the character in there.

I’d like to do something like this:

Line trace → actor → get actor BP → check if function/event calls “BeingLookedAt” exists in the BP → run it (or something like that).

Then I don’t have to do couplings to specific blueprints inside characterBP where, in my head, they don’t belong. It’d also create a really large amount of castings really fast :S

Does it make sense? I’ve been banging my head against this problem for quite a while now :S

What you’re saying makes a lot of sense, and I believe Blueprint Interfaces were made for just this situation. Essentially, you could define a blueprint interface with a function “BeingLookedAt”; the interface has no implementation. Instead, on every actor you want to do something with that function, you add the interface and implement the function, and in your character blueprint, you just need to check if the actor implements the interface. If it does, you can call the function on the actor without knowing whether the actor is a door or anything else!

I hope that makes some sense (I think the documentation will be better than my explanation)!

Exactly, except you don’t even need to check if the actor implements the interface - Unreal handles that automatically and does nothing if the actor doesn’t implement the method. See this section on calling blueprint functions for details.

@MkII Aw man that sounds sweet - I’ll check it out and see if it fixes my issue. If it does I’ll get back asap and mark as solved! :slight_smile: Boggles my mind why it’s not available just on events/functions without need for an interface, but I’m sure there’s an engine reason for this.

@ thank you for the extra info!