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));
}
赞