banner
YZ

周周的Wiki

种一棵树最好的时间是十年前,其次是现在。
zhihu
github
csdn

Unreal Engine Gameplay: Exploring Efficient Communication and Interaction Techniques Between Actors

Introduction to Actor Communication#

Actor is similar to the empty GameObject in Unity, a type of game object that can be freely placed 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 MethodUsage ScenarioPrerequisitesExample
Direct CommunicationWhen you need to call a method of a specific Actor in the levelNeed to reference the Actor in the levelTrigger an event on a specific Actor in the level.
Type CastingWhen you 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 functions of a child Actor that belongs to the same parent class.
Event DispatcherTo trigger events to multiple Actors through one Actor.Other Actors need to subscribe to the event to respond to it.Notify different types of Actors: a certain event has been triggered.
InterfaceWhen 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.

image.png
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.

  1. Create BP_Cube Blueprint
    Open the content side menu -- Create a new MyBlueprints directory under the content menu -- Right-click menu select Blueprint Class -- Choose Actor class under Common -- Name it BP_Cube.
    image.png

  2. Add Static Mesh
    Double-click to open BP_Cube -- In the left component window click Add -- Search and select Cube.
    image.png

  3. Write BP_Cube Blueprint
    In the left side of our blueprint, find the function list -- Click the + sign to add a function -- Name it OnCubeVisible -- Hold down the Ctrl key on the keyboard + drag the Cube variable into the blueprint -- Drag out a line from the Cube pin and release -- Search and select the Set Visibility function.
    image.png

  4. Add Parameters to the Function
    Select the OnCubeVisible function -- In the right details panel input field click the + sign -- 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.
    image.png

  5. 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).
    image.png
    Drag out a line from the Pressed pin and release to search for the Get Actor Of Class node -- In the Actor Class pin click the dropdown to search 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:
    image.png

  6. Drag the Actor BP_Cube from the content menu into the scene and run the test. Continuously press the number key 0.
    cube.gif

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 type casting communication method.
Continue using the PracticeProject to complete this part of the work.

  1. 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 the execution pin out to connect to the Branch node -- Connect the True pin to the Add Actor Local Rotation node -- Input a random value for the Z pin of Delta Rotation -- Drag the IsRotate variable out to connect to the Condition pin of Branch.
    image.png
    Finally, set IsRotate as a public variable (to provide settings for other blueprints).

  2. Write Collision Function in BP_ThirdPersonCharacter Blueprint
    Right-click on the Capsule Component to add an event -- Add the On Component Begin Overlay and On Component End Overlap functions to the event graph.
    image.png

  3. 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 you need to uncheck the last Is Rotate pin. As shown in the figure below:
    image.png

  4. Set Collision Preset for Character Pawn
    To enable the character to overlap collisions, you also need to check the WorldDynamic option in the component's collision preset settings.
    image.png

  5. Run the test, rotate when close to the Cube, and stop rotating when moving away.
    rotate.gif

Event Dispatcher Communication#

Similar to delegates in Unity, a globally or locally public event dispatcher that can be freely subscribed to by needed scripts. As long as they subscribe to the event dispatcher, when the event is triggered, these subscribers will receive messages and handle their respective logic.

  1. Write an Actor that Holds an Event Dispatcher
    Create a new Actor type blueprint class in the content menu -- Name it BP_EventActor -- Add a Box component -- In my blueprint under the event dispatcher section, click the + sign -- Add an event dispatcher and name it OnBoxEvent -- In the event graph, add the On Component Begin Overlay event -- Drag the event dispatcher into the graph and select Call, the connection diagram is as follows:
    image.png
    Compile and save, and place it anywhere in the scene.
    image.png
  2. 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, set its variable type to Static Mesh Actor (array type) in the details panel, and set it as public. -- In the EventActor variable details panel, add the On Box Event to the right of the event section by clicking the + sign, and start writing the logic:
    image.png
    Drag BP_CircularReceive into the scene and assign variable references in the details panel:
    image.png
  3. 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, which here is to enable the explosion effect.
    image.png
    You also need to uncheck the Auto Activate property for the two subcomponents P_Explosion and ExplosionAudio. Finally, place it in the scene and set the variable reference:
    image.png
  4. Test the Event Dispatcher Effect
    event.gif

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 functions, 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.

  1. Create an Interface
    Right-click in the blank area of the directory to create a blueprint interface class.
    image.png
    Double-click to open and add a function named Interaction.
    image.png
  2. 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 on the Interaction interface in my blueprint interface section to select Implement Event, and you will see the Event Interaction node automatically added to the blueprint.
    image.png
    Write the logic for toggling the light switch:
    image.png
  3. 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:
    image.png
  4. Modify Player Blueprint and Test Interface Event
    Find the blueprint BP_ThirdPersonCharacter, and in its On Component Begin Overlap event, add the execution node Interaction(Message).
    image.png
    Compile, save, and run:
    interface.gif

Summary#

  1. Direct Communication:

    • Suitable for simple scenarios, very effective when you need to interact directly with a specific Actor. Ensure you have the correct reference.
  2. Type Casting:

    • Type casting is a good choice when you need to access the functionality or properties of a specific class. It allows you to verify and access specific functionalities of an Actor at runtime.
  3. 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.
  4. Interface:

    • Provides a flexible way to add common functionality to different types of Actors. By implementing interfaces, you 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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.