📜 ⬆️ ⬇️

Interaction between Blue Print (Interface, Cast To)

The article is intended for newcomers to the Unreal Engine 4, and it understands how to interact between Blue Print (BP) in the scene.

Some theory


BP in Unreal Engine 4 is a class in the concept of programming, that is, an abstract description of algorithms and variables contained in a container. As long as BP is not placed in the scene (that is, an object is not created), it is impossible to perform any operations with it, other than creating an object (instance) based on it.

For example , you have a BP playing music called BP_playMusic. To make it work and start playing music, you must either place this BP on the scene (create an object / instance of class BP_playMusic) or create an instance from another BP located on the scene. This object will have its own personal state of variables, and if you place several such objects on the stage, they will be independent actors-objects, although they are created from the same BP, and they have a common class BP_playMusic. If one of these objects changes the music track, then it will not affect other objects of the BP_playMusic class in any way.

Interfaces


The main tool for BP interaction is the interface. An interface is an announcement that a given BP has a handler for the functions described in the interface.
')
For example , there are BP lamps and bp_lamp and bp_alarm alarms, and we want the turnOn and turnOff functions in these BPs. We create the interface, we name, for example, I_turnAbleItem.

image

In the interface, we will add the turnOn and turnOff functions. We also want to be able to adjust the brightness or volume of the corresponding actors. Add the setValue function, and in it the (float) value parameter. Functions in the interface look inactive, and nothing can be done in the workspace. This is because here only the name of the functions and their parameters are specified, and the implementation of the logic will already be in the BP itself.

image

Now you can compile, save and close the interface.

Open the BP bp_lamp and bp_alarm, go to the configuration and add the I_turnAbleItem interface and compile the BP.

image

Thus, we indicated that in these BPs there are event handlers for turnOn, turnOff, setValue, and we can set the functionality for these events.

Now, if in the Event Graph call the context menu and enter turnOn, three lines in different categories will be highlighted:

Add Event - add an event handler. This is what is used to execute logic when activated from another BP:


image

Here we need Add Event, we add all 3 events described in the interface, and we describe the logic that should be executed when they are called.

image

Our BP is ready. As an activator, create a simple BP_Button with a trigger. If the character entered the trigger area - turn on the lamps and alarms, set them to value = 10. When it comes out, turn it off. If specific lamps and alerts are already installed in the scene, then you can set them in the BP_Button actors object as variables or an array of objects, but what if there are several hundred of such objects or they are dynamically generated and they are not on stage before the game starts? To do this, you can use the GetActorWithInterface function. It will return an array of all objects on the scene that have the specified interface. We use this function, we select I_turnAbleItem as the interface. In the cycle of the resulting array of objects, each of them will call the event turnOn.

image

The turnOn node will be shown with an envelope sign (message) indicating that this is a function call of the interface. Also add the call to setValue. It will be possible to put the value value, which we specified in the interface. By analogy, set the turnOff event to the endOverlap event. The result will look something like this:

image

Now, no matter how much we put the lamps and alerts on the stage, in all of them these events will be triggered when the character interacts with the trigger.

The advantage of function calls through the message interface is that they can be called by any BP, even if it does not have an interface with the called function, in this case nothing will happen. For example, we received a link to some actor-object as a result of Trace and do not know what kind of actor-object it is, but we want it to show some text if it has a text display function. For such cases, you can use the call interface (message). If the actor object has a called handler, it will work.

Let's complicate the task. We need to make sure that the lamp turns on, and if not, then write a message. To do this, in the interface, in the turnOn function, add the Output value (boolean) result. Compile, save.

image

After that, an error appeared in BP_lamp and BP_alert, that the turnOn event conflicts.

image

Now the event handler is not just an event, but a function with its own graph for logic. It is opened by double clicking on the function name in the Interfaces block on the right. Add logic to it.

image

Also in the BP trigger, the turnOn function call node now has a return parameter, result, which will contain the value set in the actor object. Add a warning print if the lamp does not light up or the alarm does not turn on. Since we have a link to the object itself, we can take any information from it, for example, its coordinates.

image

Now we have control over all objects with the I_turnAbleItem interface, and we can localize all of them that have gone wrong.

Cast to


Another important function for inter-BP communication is “Cast To {class name}”. This is a function of casting a BP (class) to a type, which we specify as a parameter. Using “Cast To”, you can call custom functions of actor-objects, if we do not know in advance what type of actor-object.

For example, add the regular setColor function with the color parameter to BP_lamp

image

In the character, add the Trace handler, which is returned by the actor-object of type Actor. It does not have the setColor function that we did in the lamp, so using “Cast To” we try to cast this actor object to type BP_lamp.

image

And if this actor-object is really of type BP_lamp, “Cast To” returns an actor-object of type BP_lamp, and we can call the setColor function in it. If this is some other BP, then we simply do nothing. Casting to type through Cas To also extends to Child classes. If there are actor-objects of the BP_spotLamp, BP_pointLight classes on the scene, then Cast To BP_lamp will successfully lead them to the BP_lamp type and return an object of this type.

image

Thanks for attention!

Source: https://habr.com/ru/post/320252/


All Articles