直接Actor通信快速入门

Programming Language

×C++

Select an option from the dropdown to see content relevant to it.

Overview

Direct Actor communication is a common method of sharing information between Actors in your Level. This method requires a reference to the target Actor so you can access it from your working Actor. This communication method uses a one-to-one relationship between your working Actor and your target Actor. In this Quick Start guide, you will learn how to use direct Actor communication to access information from a target Actor in order to use its functions.

1 - Required Setup

  1. In the New Project Categories section of the menu, select Games and click Next. 直接Actor通信快速入门 - 图1

  2. Select the Third Person template and click Next. 直接Actor通信快速入门 - 图2

  3. Select C++ and With Starter Content options and click Create Project. 直接Actor通信快速入门 - 图3

Section Results

You have created a new Third Person project and are now ready to learn about direct Blueprint communication.

2 - Creating the Ceiling Light Actor

  1. From the C++ Class Wizard, create a new Actor class named CeilingLight. 直接Actor通信快速入门 - 图4

  2. In the class defaults of CeilingLight.h implement the following code. C++ protected: UPROPERTY(EditInstanceOnly, BlueprintReadWrite) USceneComponent* SceneComp; UPROPERTY(EditInstanceOnly, BlueprintReadWrite) class UPointLightComponent* PointLightComp; UPROPERTY(EditInstanceOnly, BlueprintReadWrite) UStaticMeshComponent* StaticMeshComp; Expand code Copy full snippet(21 lines long)

  3. Next navigate to CeilingLight.Cpp and declare the following Include library. C++ #include "Components/PointLightComponent.h" #include "Components/PointLightComponent.h" Copy full snippet(1 line long)
  4. From the constructor ACeilingLight::CeilingLight declare the following. C++ ACeilingLight::ACeilingLight() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; SceneComp = CreateDefaultSubobject(TEXT("SceneComp")); PointLightComp = CreateDefaultSubobject(TEXT("PointLightComp")); StaticMeshComp = CreateDefaultSubobject(TEXT("StaticMeshComp")); SceneComp = RootComponent; PointLightComp->AttachToComponent(SceneComp,FAttachmentTransformRules::KeepRelativeTransform); StaticMeshComp->AttachToComponent(SceneComp, FAttachmentTransformRules::KeepRelativeTransform); Expand code Copy full snippet(19 lines long)
  5. Compile your code.

Finished Code

CeilingLight.h

C++

// Fill out your copyright notice in the Description page of Project Settings.
    #pragma once

    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "CeilingLight.generated.h"

    UCLASS()
    class BPCOMMUNICATION_API ACeilingLight : public AActor
    {

Expand code Copy full snippet(47 lines long)

CeilingLight.cpp

C++

//Copyright Epic Games, Inc. All Rights Reserved.
    #include "CeilingLight.h"
    #include "Components/PointLightComponent.h"

    // Sets default values
    ACeilingLight::ACeilingLight()
    {
        // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
        SceneComp = CreateDefaultSubobject(TEXT("SceneComp"));

Expand code Copy full snippet(51 lines long)

  1. From the C++ Classes folder, right-click your CeilingLight Actor, then from the C++ Class Actions dropdown menu, select Create Blueprint class based on CeilingLight. Name your Blueprint BP_CeilingLight. 直接Actor通信快速入门 - 图5

  2. From the BP_CeilingLight class defaults, navigate to the Components panel, then select the StaticMeshComp. 直接Actor通信快速入门 - 图6

  3. From the Details panel, navigate to the Static Mesh category, select the dropdown arrow next to the Static Mesh variable, then search and select for SM_Lamp_Ceiling. 直接Actor通信快速入门 - 图7

  4. Compile and save your Blueprint.

  5. From the Content Browser, drag an instance of your BP_CeilingLight Actor into your Level. 直接Actor通信快速入门 - 图8

Modifying the ThirdPersonCharacter Class

  1. Navigate to your C++ Classes folder, and double-click the BPCommunicationCharacter class to open its BPCommunicationCharacter.h, then declare the following code in the class defaults. C++ protected: UPROPERTY(EditInstanceOnly, BlueprintReadWrite) class ACeilingLight* CeilingLightToToggle; void ToggleCeilingLight(); protected: UPROPERTY(EditInstanceOnly, BlueprintReadWrite) class ACeilingLight* CeilingLightToToggle; void ToggleCeilingLight(); Copy full snippet(4 lines long)
  2. Navigate to your BPCommunicationCharacter.cpp, and declare the following include. C++ #include "CeilingLight.h" #include "CeilingLight.h" Copy full snippet(1 line long)
  3. Implement your ABPCommunicationCharacter::ToggleCeilingLight method. C++ void ABPCommunicationCharacter::ToggleCeilingLight() { if (CeilingLightToToggle) { CeilingLightToToggle->TurnOffLight(); } } void ABPCommunicationCharacter::ToggleCeilingLight() { if (CeilingLightToToggle) { CeilingLightToToggle->TurnOffLight(); } } Copy full snippet(7 lines long)
  4. Navigate to the ABPCommunicationCharacter::SetupPlayerInputComponent method and declare the following. C++ PlayerInputComponent->BindAction("Use", IE_Pressed, this, &ABPCommunicationCharacter::ToggleCeilingLight); PlayerInputComponent->BindAction("Use", IE_Pressed, this, &ABPCommunicationCharacter::ToggleCeilingLight); Copy full snippet(1 line long)
  5. In the Editor, navigate to Edit > Project Settings > Input. From the Bindings category, navigate to the Action Mappings then click the Add (+) button to create a new Action mapping named Use, and select the E key for the key value. 直接Actor通信快速入门 - 图9

  6. Compile your code.

Finished Code

BPCommunicationCharacter.h

C++

// Copyright Epic Games, Inc. All Rights Reserved.
    #pragma once

    #include "CoreMinimal.h"
    #include "GameFramework/Character.h"
    #include "BPCommunicationCharacter.generated.h"

    UCLASS(config=Game)
    class ABPCommunicationCharacter : public ACharacter
    {

Expand code Copy full snippet(77 lines long)

BPCommunicationCharacter.cpp

C++

// Copyright Epic Games, Inc. All Rights Reserved.
    #include "BPCommunicationCharacter.h"
    #include "HeadMountedDisplayFunctionLibrary.h"
    #include "Camera/CameraComponent.h"
    #include "Components/CapsuleComponent.h"
    #include "Components/InputComponent.h"
    #include "GameFramework/CharacterMovementComponent.h"
    #include "GameFramework/Controller.h"
    #include "GameFramework/SpringArmComponent.h"
    #include "CeilingLight.h"

Expand code Copy full snippet(142 lines long)

3 - Interacting with the Lamp Blueprint

  1. Select your ThirdPersonCharacter Blueprint in the Level and position it closer to the lamp. 直接Actor通信快速入门 - 图10

  2. With your ThirdPersonCharacter selected, navigate to the Details panel, then from the BPCommunication Character category, find the Ceiling Light To Toggle variable, and select the arrow adjacent to it. From the dropdown menu, search for and select the BP_CeilingLight Actor. 直接Actor通信快速入门 - 图11

  3. Press Play to go into PlE (Play-in Editor) mode and press the E key to turn the lamp on and off. 直接Actor通信快速入门 - 图12

Section Results

In this section you added the Ceiling Light Actor reference to the ThirdPersonCharacter Blueprint and you turned the light on and off by pressing the E key.

Next Steps

Now that you know how to use direct Blueprint communication type, take a look at the other communication types referenced in the documentation page.