基于C++代码的UE4学习(十七)

您所在的位置:网站首页 ue4接口怎么用 基于C++代码的UE4学习(十七)

基于C++代码的UE4学习(十七)

2024-07-14 08:09| 来源: 网络整理| 查看: 265

 

因为C++并没有所谓的接口概念,在UE4中的接口其实是虚基类的单继承或多继承的概念。

先创建一个接口(虚基类),只写一个KILL方法。

 

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 "Kill.generated.h" 8 9 // This class does not need to be modified. 10 UINTERFACE(meta=(CannotImplementInterfaceInBlueprint)) 11 class UKill : public UInterface 12 { 13 GENERATED_BODY() 14 }; 15 16 /** 17 * 18 */ 19 class INHERITINTERFACE_API IKill 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 virtual void Kill(); 27 }; 28 29 // Fill out your copyright notice in the Description page of Project Settings. 30 31 32 #include "Kill.h" 33 #include "Engine.h" 34 35 // Add default functionality here for any IKill functions that are not pure virtual. 36 void IKill::Kill() { 37 GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("KILL技能正在释放!")); 38 39 AActor* me = Cast(this); 40 41 if (me) { 42 me->Destroy(); 43 } 44 }

 

 

再创建一个接口(虚基类)继承自IKill类,只写一个reborn方法。

这里有一点要注意,UReborn主体与IReborn主体部分都需要继承自UKill与IKill,这里是用的公有继承。

 

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 "Kill.h" 8 #include "Reborn.generated.h" 9 10 // This class does not need to be modified. 11 UINTERFACE(MinimalAPI) 12 class UReborn : public UKill 13 { 14 GENERATED_BODY() 15 }; 16 17 /** 18 * 19 */ 20 class INHERITINTERFACE_API IReborn:public IKill 21 { 22 GENERATED_BODY() 23 24 // Add interface functions to this class. This is the class that will be inherited to implement this interface. 25 public: 26 virtual void Kill() override; 27 virtual void reborn(); 28 }; 29 30 31 // Fill out your copyright notice in the Description page of Project Settings. 32 33 34 #include "Reborn.h" 35 #include "Engine.h" 36 37 // Add default functionality here for any IReborn functions that are not pure virtual. 38 39 void IReborn::reborn() 40 { 41 GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("正在重生!")); 42 43 44 } 45 46 47 void IReborn::Kill() { 48 GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("在Reborn类中的KILL技能正在释放!")); 49 50 AActor* me = Cast(this); 51 52 if (me) { 53 me->Destroy(); 54 } 55 }

 

 

在完成了两个接口的之后,创建一个ACTOR类,实现IReborn类接口。

 

 

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 "Reborn.h" 8 #include "MyActor.generated.h" 9 10 UCLASS() 11 class INHERITINTERFACE_API AMyActor : public AActor,public IReborn 12 { 13 GENERATED_BODY() 14 15 public: 16 // Sets default values for this actor's properties 17 AMyActor(); 18 19 protected: 20 // Called when the game starts or when spawned 21 virtual void BeginPlay() override; 22 23 public: 24 // Called every frame 25 virtual void Tick(float DeltaTime) override; 26 27 public: 28 UFUNCTION(BlueprintCallable) 29 virtual void Kill() override; 30 UFUNCTION(BlueprintCallable) 31 virtual void reborn() override; 32 }; 33 34 35 // Fill out your copyright notice in the Description page of Project Settings. 36 37 38 #include "MyActor.h" 39 #include "Engine.h" 40 41 // Sets default values 42 AMyActor::AMyActor() 43 { 44 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 45 PrimaryActorTick.bCanEverTick = true; 46 47 } 48 49 // Called when the game starts or when spawned 50 void AMyActor::BeginPlay() 51 { 52 Super::BeginPlay(); 53 54 } 55 56 // Called every frame 57 void AMyActor::Tick(float DeltaTime) 58 { 59 Super::Tick(DeltaTime); 60 61 } 62 63 64 65 void AMyActor::Kill() { 66 GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("Kill is releasing in ACOTR")); 67 } 68 69 void AMyActor::reborn() 70 { 71 GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("ACTOR is reborning")); 72 73 74 }

 

 

由于KILL方法和REBORN方法是从(接口)虚基类获得,所以需要重写一下。

重写、编译后再关卡蓝图中调用。

 

 

最终效果如下:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3