Pass a blueprint as parameter

Hello

I would like to know if I can pass blueprint as parameter in order to make similar object do different things.

Here is an exemple :

Imagine I have 100 buttons, each button as the exact same script because every button work the same way.
But each button do a different action when they are pushed, some open door, others turn on the lights …

What I would like to do is have 100 exact same buttons but with a blueprint script parameter Action. So that, when they are pushed, each button call their Action script passed as parameter.

This way I would have 100 exact identical button, and just custom script for action (like open door, activate light, play dialogue…).

In this way, I would have generic items and custom action that I could reuse everywhere I need.

The problem I have actually is that I can’t find a way to pass a blueprint as parameter WITHOUT adding it to the scene. I’m forced to putt all the action on the scene and get a reference of the useless action in order to make everything works. I’m not talking about communication between 2 blueprint, i really mean passing a blueprint script as parameter.

For exemple in Unity, I can pass a c# script as parameter and use it within the script. How do I do that in unreal? I must use c++?

I don’t think you can do this, not in a blueprint only project, and it’s also very complex and inefficient to do in C++ project. Blueprint (as an asset, a.k.a UBlueprint) purpose is to generate a class (UClass to be precise). It’s an asset that will not be included in the actual game, the one that is included is the class generated by the blueprint, if you create a function in a blueprint, the blueprint itself doesn’t actually has the function. The point is, you should never save a reference to UBlueprint asset in the game, the only safe place to reference UBlueprint is in editor-only codes. If you’re using Blueprint only project then you don’t have to worry about this because the editor prevents you to do that.

There are lots of alternative and simpler solutions for your case, but the one I would use is the Event Dispatcher: Just create an event dispatcher in the button’s blueprint and then call it when the button is pressed. Somebody else (a door maybe) can listen to this event and change its own state (from closed to opened or vice versa).

That way you only need to create 1 Button Blueprint and place this button as many as you like in the level, as for the Door (or any object that listens to the button), just create a property that reference to the button you’ve made, and set it to listen to the Button’s event in its BeginPlay event script. The only thing that you need to do after setting this up is to click the door in your level, open/find the button reference in its details panel, and set it to the correct button it should listen to. (just like setting which directional light to use for the SkyBox)

What you’re looking for is polymorphism.

You make a base blueprint that implements the basics of the button. Then you make a function in the button like OnClick.

Then you can make derived versions of it that perform different things by overriding the OnClick function.

Or you can make an interface that actors can implement (Like IButtonTarget), and then make an array in the button of actors to activate. Then you can add the actors the button should activate, and then iterate that list and execute a function in the interface on each of the actors to activate on the button press.