基于C++代码的UE4学习(二十二)——在关卡蓝图中实现C++接口的重写

 

上一节,我们将C++的接口在蓝图中进行了实现。

这一节我们将C++的接口在蓝图中进行重写。

准备一下接口文件:

 

 

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 MYPROJECT9_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     bool showinfo();
28 };

 

将UFUNCTION宏定义中改成BlueprintNativeEvent,让其在蓝图和C++中都可以编辑,为了让其变成方法,方法返回值类型不定义为VOID类型。

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 "GameFramework/Actor.h"
 7 #include "MyInterface.h"
 8 #include "Engine.h"
 9 #include "MyActor.generated.h"
10 
11 UCLASS()
12 class MYPROJECT9_API AMyActor : public AActor,public IMyInterface
13 {
14     GENERATED_BODY()
15     
16 public:    
17     // Sets default values for this actor's properties
18     AMyActor();
19 
20 protected:
21     // Called when the game starts or when spawned
22     virtual void BeginPlay() override;
23 
24 public:    
25     // Called every frame
26     virtual void Tick(float DeltaTime) override;
27 
28     UFUNCTION()
29     virtual bool showinfo_Implementation() override;
30 };

 

 

在CPP文件中对接口方法进行重写。

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "MyActor.h"
 5 
 6 // Sets default values
 7 AMyActor::AMyActor()
 8 {
 9      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
10     PrimaryActorTick.bCanEverTick = true;
11
12 } 
13 
14 // Called when the game starts or when spawned 
15 void AMyActor::BeginPlay() 
16 { 
17  Super::BeginPlay(); 
18 
19 } 
20 
21 // Called every frame 
22 void AMyActor::Tick(float DeltaTime) 
23 { 
24  Super::Tick(DeltaTime); 
25 
26 } 
27 
28 bool AMyActor::showinfo_Implementation() 
29 { 
30     GEngine->AddOnScreenDebugMessage(-1, 15, FColor::Blue, TEXT("Can be Override")); 
31     return true; 
32 }

 

 

将ACTOR类蓝图化

 

 

在蓝图中重写接口、调用

 

 

 

结果如下,成功重写

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值