UE4 C++ 用C++接口实现,让C++和蓝图调用接口消息

UE4 C++ 用C++接口实现,让C++和蓝图调用接口消息

1. 一个character类实现一个C++的接口
2. 一个character的动画蓝图获取character类发送接口消息

新建接口:PlayerActorInterface
在这里插入图片描述
声明方法:

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

#pragma once

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

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UPlayerActorInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class CHADISI_API IPlayerActorInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	//BlueprintCallable只有写了这个宏蓝图才可以调用接口消息
	UFUNCTION(BlueprintNativeEvent,BlueprintCallable,Category="My")
	void F_GetPlayerForwardAndRight(float& forward,float& right);
};

一个character类:
继承ACharacter,和接口
实现接口方法

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Interface/PlayerActorInterface.h"
#include "HdsPlayer.generated.h"

UCLASS()
class CHADISI_API AHdsPlayer : public ACharacter,public IPlayerActorInterface
{
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere)
	TArray<UAnimMontage*> AnimMontages;
	
public:
	// Sets default values for this character's properties
	AHdsPlayer();

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

public:
	void MoveForward(float Value);
	void MoveRight(float Value);

	void PlayAnim(int index);

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

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	//接口实现的方法
	virtual void F_GetPlayerForwardAndRight_Implementation(float& forward, float& right) override;
};


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


#include "HdsPlayer.h"

// Sets default values
AHdsPlayer::AHdsPlayer()
{
 	// Set this character 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 AHdsPlayer::BeginPlay()
{
	Super::BeginPlay(); 
	
}

void AHdsPlayer::MoveForward(float Value)
{
	if (Value!=0.0f&&Controller!=nullptr)
    	{
    		FRotator Rotator=GetController()->GetControlRotation();
    		FRotator NowRotaor(0.0f,Rotator.Yaw,0.0f);
    		FVector Vector=FRotationMatrix(NowRotaor).GetUnitAxis(EAxis::Y);
    		AddMovementInput(Vector,Value);
    	}
}

void AHdsPlayer::MoveRight(float Value)
{
	if (Value!=0.0f&&Controller!=nullptr)
	{
		FRotator Rotator=GetController()->GetControlRotation();
		FRotator NowRotator(0.0f,Rotator.Yaw,0.0f);
		FVector Vector=FRotationMatrix(NowRotator).GetUnitAxis(EAxis::X);
		AddMovementInput(Vector,Value);
	}
}

void AHdsPlayer::PlayAnim(int index)
{
	if (AnimMontages[index]!=nullptr)
	{
		//UE_LOG(LogTemp,Warning,TEXT("%d,"),*index);
		this->PlayAnimMontage(AnimMontages[index],1);
	}
}

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

}

// Called to bind functionality to input
void AHdsPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis(TEXT("MoveForward"),this,&AHdsPlayer::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"),this,&AHdsPlayer::MoveRight);
	
}

void AHdsPlayer::F_GetPlayerForwardAndRight_Implementation(float& forward, float& right)
{
	UE_LOG(LogTemp,Warning,TEXT("sfd"));
}

蓝图可以看到我们继承接口的方法
在这里插入图片描述

在这里插入图片描述
这样就可以拿到节点了

在这里插入图片描述
动画蓝图里随便调用一下就可以成功拿到控制台了
在这里插入图片描述

C++的接口消息:

我这里的是建了一个玩家状态类,一个C++接口接到玩家状态类,然后在玩家控制器里拿到玩家状态类,再判断是否继承了这个接口,再调用接口方法。

接口:

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

#pragma once

#include "CoreMinimal.h"
#include "enum/PlayerEnum.h"
#include "UObject/Interface.h"
#include "PlayerStateInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UPlayerStateInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class CHADISI_API IPlayerStateInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	UFUNCTION(BlueprintNativeEvent,BlueprintCallable,Category="My")
	void SetPlayerState();
	UFUNCTION(BlueprintNativeEvent,BlueprintCallable,Category="My")
	FMyPlayerState GetPlayerState();
};

玩家状态类:
.h

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

#pragma once

#include "CoreMinimal.h"
#include "enum/PlayerEnum.h"
#include "GameFramework/PlayerState.h"
#include "Interface/PlayerStateInterface.h"
#include "OnePlayerState.generated.h"

/**
 * 
 */
UCLASS()
class CHADISI_API AOnePlayerState : public APlayerState,public IPlayerStateInterface
{

private:
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="My")
	FMyPlayerState myplayerState; //玩家的状态
	
public:
	virtual void BeginPlay() override;
	void SetPlayerState();
	virtual void SetPlayerState_Implementation() override;
	FMyPlayerState GetPlayerState();
	virtual FMyPlayerState GetPlayerState_Implementation() override;
	
};

.cpp

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


#include "OnePlayerState.h"

#include <string>

void AOnePlayerState::BeginPlay()
{
	Super::BeginPlay();
	this->myplayerState.HP=100.0f;
}

void AOnePlayerState::SetPlayerState_Implementation()
{
	
}

FMyPlayerState AOnePlayerState::GetPlayerState()
{
	return IPlayerStateInterface::GetPlayerState();
}

FMyPlayerState AOnePlayerState::GetPlayerState_Implementation()
{
	this->myplayerState.HP=100.0f;
	return this->myplayerState;
}



玩家控制器里这样就可以调用玩家状态里实现的方法了:

AOnePlayerState* OnePlayerState = Cast<AOnePlayerState>(PlayerState);
	if (OnePlayerState->GetClass()->ImplementsInterface(UPlayerStateInterface::StaticClass()))
	{
		/*IPlayerStateInterface* PlayerStateInterface = Cast<IPlayerStateInterface>(OnePlayerState);
		myplayerState=PlayerStateInterface->Execute_GetPlayerState(OnePlayerState);*/
		UE_LOG(LogTemp,Warning,TEXT("%f"),(OnePlayerState->Execute_GetPlayerState(OnePlayerState).HP));
	}

在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Li~蒙一

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

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

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

打赏作者

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

抵扣说明:

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

余额充值