UE4 实现C++蓝图接口

蓝图接口和C++、Java中的接口类似,都是用来被继承的,并且可以实现相应的功能。

  1. 使用UE新建一个第一人称游戏来实现接口实例:TestInterface
    在这里插入图片描述
    在这里插入图片描述2. 新建一个C++的接口类:HittedInterface
    在这里插入图片描述
    在这里插入图片描述
  2. 打开VS2019编辑C++代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "HittedInterface.generated.h"

// This class does not need to be modified.
/**
class UHittedInterface 这个类是为了在UE中使用反射系统,不需要处理
*/
UINTERFACE(MinimalAPI)
class UHittedInterface : public UInterface {
	GENERATED_BODY()
};

/**
 *
 */
class TESTINTERFACE_API IHittedInterface {
	GENERATED_BODY()

		// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	/**
	BlueprintNativeEvent除了实现C++调用蓝图外,同样会调用一个本地方法,本地方法的实现为 声明的函数名+_Implementation
	在实现中应该是:
	void hitByProjectile_Implementation()override;
	这样就可以实现一次调用,两个实现(c++实现,蓝图实现)
	*/
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
	void hitByProjectile();
};

// Fill out your copyright notice in the Description page of Project Settings.
#include "HittedInterface.h"
// Add default functionality here for any IHittedInterface functions that are not pure virtual.

  1. 新建一个Actord类HittedActor继承自接口IHittedInterface
    在这里插入图片描述
    在这里插入图片描述
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "HittedInterface.h"
#include "GameFramework/Actor.h"
#include "HittedActor.generated.h"

UCLASS()
class TESTINTERFACE_API AHittedActor : public AActor, public IHittedInterface {
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AHittedActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

public:
	void hitByProjectile_Implementation()override;

};

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


#include "HittedActor.h"
#include "Kismet/KismetSystemLibrary.h"

// Sets default values
AHittedActor::AHittedActor() {
	// 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;

}

// Called when the game starts or when spawned
void AHittedActor::BeginPlay() {
	Super::BeginPlay();

}

// Called every frame
void AHittedActor::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

}

void AHittedActor::hitByProjectile_Implementation() {
	//在HUD界面显示内容
	GEngine->AddOnScreenDebugMessage(-1, 2.0, FColor::Red, UKismetSystemLibrary::GetDisplayName(this) + TEXT(" hitting..."));
}

  1. 在子弹击中后调用这个函数,在类ATestInterfaceProjectile中实现
void ATestInterfaceProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) {
	
	//Actor被子弹击中后,调用接口
	IHittedInterface* hitedActor = Cast<IHittedInterface>(OtherActor);
	if (hitedActor) {
		hitedActor->Execute_hitByProjectile(OtherActor);
	}
	
	// Only add impulse and destroy projectile if we hit a physics
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics()) {
		OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());

		Destroy();
	}
}
  1. 在UEEditor 中创建AHittedActor的蓝图类BP_HittedActor
    在这里插入图片描述

在蓝图中添加一个立方体
在这里插入图片描述
保存以后,把BP_HittedActor拖动到场景中

可以在跑【类设置】中查看接口继承情况
在这里插入图片描述

  1. 运行程序
    在这里插入图片描述
    aaa
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值