Introduction to Actor Communication#
Actors are similar to GameObjects in Unity, which are game objects that can be placed freely in the scene. UE provides various methods for communication between different Actors. This article will demonstrate the requirements and common usage of the four communication methods listed in the table below through a demo.
Actor Communication Methods Table:
Communication Method | Usage Scenario | Prerequisites | Example |
---|---|---|---|
Direct Communication | Need to call a method of a specific Actor in the level | Need to reference the Actor in the level | Trigger an event on a specific Actor in the level. |
Type Casting | Want to verify if an Actor belongs to a specific class to access its properties. | Need to reference the Actor in the level to cast to the desired Actor class. | Access specific functionality of a child Actor that belongs to the same parent class. |
Event Dispatcher | Trigger events to multiple Actors through one Actor. | Other Actors need to subscribe to the event to respond. | Notify different types of Actors that a certain event has been triggered. |
Interface | When you need to add the same functionality to different Actors. | Need to reference the Actor in the level, and that Actor needs to implement the interface. | Add interactive behavior to different types of Actors. |
Communication Method Demonstration#
Preparation#
Open Unreal Engine -- Create a new project -- Select Game -- Third Person Game -- Check the Beginner Content package.
This demo is built using the latest version of Unreal Engine 5.4.
Direct Communication#
This example will implement the visibility control of a Cube by the Third Person Character Actor blueprint to demonstrate the use of the direct communication method.
-
Create BP_Cube Blueprint
Open the content sidebar menu -- Create a new MyBlueprints directory under the content menu -- Right-click menu to select Blueprint Class -- Choose Actor class under Common -- Name it BP_Cube.
-
Add Static Mesh
Double-click to open BP_Cube -- Click Add in the left component window -- Search and select Cube.
-
Write BP_Cube Blueprint
In the left side under our blueprint, find the function list -- Click the + sign to add a function -- Name it OnCubeVisible -- Hold the Ctrl key and drag the Cube variable into the blueprint -- Drag out a line from the Cube pin and release -- Search and select the Set Visibility function.
-
Add Parameters to the Function
Select the OnCubeVisible function -- Click the + sign in the details panel input area -- Add a Boolean variable IsVisible -- Connect the function parameter Is Visible pin to the new Visibility of Set Visibility -- Click Compile to save the blueprint.
-
Add Key Event to Third Person Blueprint BP_ThirdPersonCharacter
Search for BP_ThirdPersonCharacter in the content menu and double-click to open -- Right-click in the blank area of the blueprint to search for the number key 0 event (Keyboard Events node).
Drag out a line from the Pressed pin and search for Get Actor Of Class node -- In the Actor Class pin, click the dropdown and search for and select BP_Cube -- Connect to the Flip Flop node -- Drag out from the return value pin to connect to the On Cube Visible function. The connection diagram is as follows:
-
Drag the Actor BP_Cube from the content menu into the scene and run the test. Continuously press the number pad 0 key.
Type Casting Communication#
This example will implement the rotation control of a Cube by the Third Person Character Actor blueprint to demonstrate the use of the type casting communication method. Continue using the PracticeProject to complete this part.
-
Write Rotation Logic in BP_Cube Blueprint
Double-click to open BP_Cube blueprint -- Add IsRotate variable in the variable bar -- Find the Event Tick node in the event graph -- Drag out the execution pin to connect to the Branch node -- Connect the True pin to the Add Actor Local Rotation node -- Input a random value for the Z of the Delta Rotation pin -- Drag out the IsRotate variable to connect to the Condition pin of the Branch.
Finally, set IsRotate to be a public variable (to provide settings for other blueprints). -
Write Collision Function in BP_ThirdPersonCharacter Blueprint
Right-click on the Capsule Component and select Add Event -- Add On Component Begin Overlay and On Component End Overlap functions to the event graph.
-
Write Type Casting Call Logic
In the Begin node, drag out from the Other Actor pin to connect to the Cast To BP_Cube node -- Connect from the As BP Cube node to the Set Is Rotate node -- Check the Is Rotate pin.
Similarly, set the End node, but uncheck the last Is Rotate pin. As shown in the figure below:
-
Set Collision Preset for Character Pawn
To enable overlapping collisions for the character, the collision preset settings for the component need to be checked as WorldDynamic.
-
Run the test, rotate when close to the Cube, and stop rotating when moving away.
Event Dispatcher Communication#
Similar to delegate events in Unity, a globally or locally public event dispatcher can be freely subscribed to by the required scripts. Once subscribed to the event dispatcher, when the event is triggered, these subscribers can receive messages and handle their respective logic.
-
Write an Actor that Holds an Event Dispatcher
Create a new Actor type blueprint in the content menu -- Name it BP_EventActor -- Add a Box component -- In my blueprint, add an event dispatcher by clicking the + sign -- Name the event dispatcher OnBoxEvent -- Add the On Component Begin Overlay event to the event graph -- Drag the event dispatcher into the graph and select Call, the connection diagram is as follows:
Compile and save, and place it anywhere in the scene.
-
Create the First Event Receiver Actor -- BP_CircularReceive
Write blueprint logic to hide the four QuarterCylinders. Add a variable named EventActor and set its variable type to BP_EventActor in the details panel, and set it as a public variable -- Add a variable named CylinderList and set its variable type to Static Mesh Actor (array type) in the details panel, and set it as public. -- In the details panel of the EventActor variable, add the + sign next to On Box Event to start writing logic:
Drag BP_CircularReceive into the scene and assign variable references in the details panel:
-
Write the Second Event Receiver Actor -- BP_ExplosionActor
Search for Blueprint_Effect_Explosion in the content menu, copy it to your defined folder, and rename it to BP_ExplosionActor -- Drag it to the position of the cylinder in the scene, slightly adjust its position to center it in the cylinder. Similarly, add the Eventor variable and complete its event logic to enable the explosion effect.
Also, uncheck the Auto Activate property for the two subcomponents P_Explosion and ExplosionAudio. Finally, place it in the scene and set the variable reference:
-
Test the Event Dispatcher Effect
Interface Communication#
In UE5, Actor interface communication is a very effective design pattern. The interface is responsible for defining a series of common behaviors or functionalities, which can have different implementations in different Actors. This communication method is suitable when you have implemented the same type of functionality for different Actors.
In this example, a simple interaction system will be implemented by communicating between two different Actors to learn the usage of interfaces.
-
Create an Interface
Right-click in the empty directory to create a blueprint interface class.
Double-click to open and add a function named Interaction.
-
Create an Interactive Switch Light
Search for Blueprint_CeilingLight in the content menu, copy it to your custom directory, and rename it to BP_Light -- Place it in the scene.
Double-click to open the blueprint, click Class Settings to add the interface BPI_Interaction, compile and save, then right-click the Interaction interface in my blueprint interface panel to select Implement Event, and you will see the Event Interaction node automatically added to the blueprint.
Write the logic for alternating the switch light:
-
Create an Interactive Sphere
Create a new Actor blueprint class named BP_Sphere, add a sphere component, and similarly add the Interaction interface and implement the logic for switching materials:
-
Modify the Player Blueprint and Test the Interface Event
Find the BP_ThirdPersonCharacter blueprint, and add the execution node Interaction(Message) in its OnComponent Begin Overlap event.
Compile, save, and run:
Summary#
-
Direct Communication:
- Suitable for simple scenarios, very effective when you need to interact directly with a specific Actor. Ensure you have the correct reference.
-
Type Casting:
- A good choice when you need to access functionalities or properties of a specific class. It allows you to verify and access specific functionalities of an Actor at runtime.
-
Event Dispatcher:
- Suitable for scenarios that require broadcasting events, allowing multiple Actors to respond to the same event, enhancing the flexibility and scalability of the system.
-
Interface:
- Provides a flexible way to add common functionalities to different types of Actors. Implementing through interfaces can reduce code duplication and improve maintainability.
Through these four communication methods, developers can efficiently manage interactions between Actors in Unreal Engine, building complex game logic. Choosing the appropriate communication method can enhance code readability and maintainability, making the project more modular.