基于C++代码的UE4学习(二十三)——利用接口来完成应答Cube

目的是PAWN触碰到一个ACTOR时候,触发的接口重写的函数。实现由两种方法。

第一种方法:以应答机触碰为主

先定义一个接口类:

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "UObject/Interface.h"
 7 #include "MyInterface.generated.h"
 8 
 9 // This class does not need to be modified.
10 UINTERFACE(MinimalAPI)
11 class UMyInterface : public UInterface
12 {
13     GENERATED_BODY()
14 };
15 
16 /**
17  * 
18  */
19 class MYPROJECT10_API IMyInterface
20 {
21     GENERATED_BODY()
22 
23     // Add interface functions to this class. This is the class that will be inherited to implement this interface.
24 public:
25 
26     UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interface")
27     void talking();
28 
29 };

创建一个STATICMESHACTOR类,作为应答机。

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "Engine/StaticMeshActor.h"
 7 #include "MyInterface.h"
 8 #include "UObject\ConstructorHelpers.h"
 9 #include "Components\BoxComponent.h"
10 #include "TalkCube.generated.h"
11 
12 /**
13  * 
14  */
15 UCLASS()
16 class MYPROJECT10_API ATalkCube : public AStaticMeshActor,public IMyInterface
17 {
18     GENERATED_BODY()
19     
20 public:
21 
22     ATalkCube();
23     UFUNCTION()
24     virtual void talking_Implementation() override;
25 
26     UBoxComponent* box;
27 
28 public:
29 
30     FScriptDelegate myDelegate;
31 
32     UFUNCTION()
33     void BeginOverlap();
34 };
 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "TalkCube.h"
 5 #include "Engine.h"
 6 
 7 ATalkCube::ATalkCube() {
 8     UStaticMeshComponent* myMesh = GetStaticMeshComponent();
 9     auto asset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'"));
10     if (asset.Succeeded()) {
11         myMesh->SetStaticMesh(asset.Object);
12     }
13     myMesh->SetGenerateOverlapEvents(true);
14     SetActorEnableCollision(true);
15     myMesh->SetMobility(EComponentMobility::Movable);
16 
17     box = CreateDefaultSubobject<UBoxComponent>(TEXT("box"));
18     box->InitBoxExtent(FVector(100.0f));
19     box->SetupAttachment(myMesh);
20 
21     myDelegate.BindUFunction(this, FName("BeginOverlap"));
22     box->OnComponentBeginOverlap.Add(myDelegate);
23 }
24 
25 void ATalkCube::talking_Implementation() {
26     GEngine->AddOnScreenDebugMessage(-1, 8, FColor::Red,TEXT("Hello,What's your name?"));
27 }
28 
29 
30 void ATalkCube::BeginOverlap()
31 {
32     //不可以直接绑定接口重写的函数,需要放置在另一个函数中。
33     talking_Implementation();
34 }

自定义PAWN类,并调整好按键输入。

  1 // Fill out your copyright notice in the Description page of Project Settings.
  2 
  3 #pragma once
  4 
  5 #include "CoreMinimal.h"
  6 #include "GameFramework/Pawn.h"
  7 #include "Components\BoxComponent.h"
  8 #include "MyInterface.h"
  9 #include "TalkCube.h"
 10 #include "MyPawn.generated.h"
 11 
 12 UCLASS()
 13 class MYPROJECT10_API AMyPawn : public APawn
 14 {
 15     GENERATED_BODY()
 16 
 17 public:
 18     // Sets default values for this pawn's properties
 19     AMyPawn();
 20 
 21 protected:
 22     // Called when the game starts or when spawned
 23     virtual void BeginPlay() override;
 24 
 25 public:    
 26     // Called every frame
 27     virtual void Tick(float DeltaTime) override;
 28 
 29     // Called to bind functionality to input
 30     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
 31 
 32 public:
 33     FVector keyInput;
 34     FRotator mouseInput;
 35     void forward(float value);
 36     void yaw(float value);
 37     class UBoxComponent* box;
 38 }
 39 
 40 
 41 
 42 // Fill out your copyright notice in the Description page of Project Settings.
 43 
 44 
 45 #include "MyPawn.h"
 46 #include "Components\InputComponent.h"
 47 #include "Engine.h"
 48 
 49 // Sets default values
 50 AMyPawn::AMyPawn()
 51 {
 52      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
 53     PrimaryActorTick.bCanEverTick = true;
 54     bUseControllerRotationYaw = true;
 55     AutoPossessPlayer = EAutoReceiveInput::Player0;
 56     box = CreateDefaultSubobject<UBoxComponent>(TEXT("box"));
 57     RootComponent = box;
 58     box->InitBoxExtent(FVector(120));
 59     SetActorEnableCollision(true);
 60     box->SetGenerateOverlapEvents(true);
 61     box->SetCollisionObjectType(ECC_Pawn);
 62 
 63     
 64 }
 65 
 66 
 67 
 68 
 69 // Called when the game starts or when spawned
 70 void AMyPawn::BeginPlay()
 71 {
 72     Super::BeginPlay();
 73 }
 74 
 75 // Called every frame
 76 void AMyPawn::Tick(float DeltaTime)
 77 {
 78     Super::Tick(DeltaTime);
 79     AddActorLocalOffset(keyInput);
 80     AddControllerYawInput(mouseInput.Yaw);
 81     
 82 }
 83 
 84 // Called to bind functionality to input
 85 void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
 86 {
 87     Super::SetupPlayerInputComponent(PlayerInputComponent);
 88     PlayerInputComponent->BindAxis("forward", this, &AMyPawn::forward);
 89     PlayerInputComponent->BindAxis("yaw", this, &AMyPawn::yaw);
 90 }
 91 
 92 void AMyPawn::forward(float value)
 93 {
 94     keyInput.X = value * 5;
 95 }
 96 
 97 void AMyPawn::yaw(float value)
 98 {
 99     mouseInput.Yaw = value;
100 }

第二种方法是以PAWN类为碰撞主体,增加新的方法(加粗)。

头文件:

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/Pawn.h"
 7 #include "Components\BoxComponent.h"
 8 #include "MyInterface.h"
 9 #include "TalkCube.h"
10 #include "MyPawn.generated.h"
11 
12 UCLASS()
13 class MYPROJECT10_API AMyPawn : public APawn
14 {
15     GENERATED_BODY()
16 
17 public:
18     // Sets default values for this pawn's properties
19     AMyPawn();
20 
21 protected:
22     // Called when the game starts or when spawned
23     virtual void BeginPlay() override;
24 
25 public:    
26     // Called every frame
27     virtual void Tick(float DeltaTime) override;
28 
29     // Called to bind functionality to input
30     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
31 
32 public:
33     FVector keyInput;
34     FRotator mouseInput;
35     void forward(float value);
36     void yaw(float value);
37     class UBoxComponent* box;
38 
39 
40 public:
41     virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
42 
43 
44 };

源文件:

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "MyPawn.h"
 5 #include "Components\InputComponent.h"
 6 #include "Engine.h"
 7 
 8 // Sets default values
 9 AMyPawn::AMyPawn()
10 {
11      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
12     PrimaryActorTick.bCanEverTick = true;
13     bUseControllerRotationYaw = true;
14     AutoPossessPlayer = EAutoReceiveInput::Player0;
15     box = CreateDefaultSubobject<UBoxComponent>(TEXT("box"));
16     RootComponent = box;
17     box->InitBoxExtent(FVector(120));
18     SetActorEnableCollision(true);
19     box->SetGenerateOverlapEvents(true);
20     box->SetCollisionObjectType(ECC_Pawn);
21 
22     
23 }
24 
25 
26 
27 
28 // Called when the game starts or when spawned
29 void AMyPawn::BeginPlay()
30 {
31     Super::BeginPlay();
32 }
33 
34 // Called every frame
35 void AMyPawn::Tick(float DeltaTime)
36 {
37     Super::Tick(DeltaTime);
38     AddActorLocalOffset(keyInput);
39     AddControllerYawInput(mouseInput.Yaw);
40     
41 }
42 
43 // Called to bind functionality to input
44 void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
45 {
46     Super::SetupPlayerInputComponent(PlayerInputComponent);
47     PlayerInputComponent->BindAxis("forward", this, &AMyPawn::forward);
48     PlayerInputComponent->BindAxis("yaw", this, &AMyPawn::yaw);
49 }
50 
51 void AMyPawn::forward(float value)
52 {
53     keyInput.X = value * 5;
54 }
55 
56 void AMyPawn::yaw(float value)
57 {
58     mouseInput.Yaw = value;
59 }
60 
61 
62 
63 
64 void AMyPawn::NotifyActorBeginOverlap(AActor* OtherActor)
65 {
66     if(OtherActor->GetClass()->ImplementsInterface(UMyInterface::StaticClass())){
67     
68         IMyInterface::Execute_talking(OtherActor);
69         
70     }
71 
72 }

效果如下:

猜你喜欢

转载自www.cnblogs.com/dlak/p/13372104.html
今日推荐