您现在的位置是:首页 >技术杂谈 >UE5.1.1C++从0开始(4.虚幻的接口以及交互功能)网站首页技术杂谈
UE5.1.1C++从0开始(4.虚幻的接口以及交互功能)
这一个章节对于第一次接触虚幻的人来说可以说是最绕的一个点,因为老师突然给你塞了很多的概念,对于这一块的学习,我个人的推荐是:先把蓝图搞明白了,再对应到C++的代码中,不然一定会被整的晕头转向。还是,有不懂的点就来问我。
主要有几个大点:
- 接口
- 自定义组件
- 如何在角色身上使用自定义组件
这些东西在蓝图里面真的非常好实现,但是到了C++里面,由于语法,做的工作会比蓝图里面拉节点多很多,所以这一块我的建议就最上面的:先把蓝图整明白。
这一节课的第一个重点:接口Interface
说白了就是,一个地方定义,多个地方实现,假设我们在A的情况如果要调用接口的函数,直接调用A这个类实现的接口函数。接口方便我们用一个按键去完成不同的功能(比如你用E键对物品的时候是拾起物品,对人的时候就是进行对话)
具体功能听老师讲解,还是老样子,上代码以及注意事项:
SGamePlayInterface.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "SGameplayInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class USGameplayInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class ACTIONROGUELIKE_API ISGameplayInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent)
void Interact(APawn* InstigatorPawn);
};
注:我们不需要在SGamePlayInterface.cpp文件中对我们的函数进行实现,实现是别的继承接口的类的工作,而不是我们接口类的工作,接口类的工作就是定义函数,说白了就是.cpp文件你不用动,在.h文件里面写了东西,就行了。
接口定义完成之后,我们需要一个能交互功能。首先定义一个可以交互的物体,按照斯坦福教程老师的代码走:
SItemChest.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SGameplayInterface.h"
#include "SItemChest.generated.h"
UCLASS()
class ACTIONROGUELIKE_API ASItemChest : public AActor, public ISGameplayInterface//这里的父类除了AActor,还有ISGameplayInterface,也就是说,这个类继承了我们的接口类,所以理所当然的,我们需要实现这个接口的函数
{
GENERATED_BODY()
//对接口类定义的函数进行实现
void Interact_Implementation(APawn* InstigatorPawn) override;//看到这个override没,就是重写这个函数,也就是实现这个函数。
public:
//一个float类型数据,可在蓝图内编辑
UPROPERTY(EditAnywhere)
float TargetPitch;
//两个静态网格体组件
UPROPERTY(VisibleAnywhere)
class UStaticMeshComponent* BaseMeshComp;
UPROPERTY(VisibleAnywhere)
class UStaticMeshComponent* LidMeshComp;
public:
// Sets default values for this actor's properties
ASItemChest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
SItemChest.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SItemChest.h"
#include "Components/StaticMeshComponent.h"
//实现接口类的函数的函数体
void ASItemChest::Interact_Implementation(APawn* InstigatorPawn)
{
//这个函数的作用就是,将lidmesh按照pitch的方向进行旋转,旋转角度就是我们初始化那里定义的float数据,这个数据可以在蓝图里面变换
LidMeshComp->SetRelativeRotation(FRotator(TargetPitch, 0, 0));
}
// Sets default values
ASItemChest::ASItemChest()
{
// 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;
//组件初始化,前面几章的内容,忘记的就回到前几个视频重新看,这是最基本的东西
BaseMeshComp=CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseMesh"));
RootComponent = BaseMeshComp;
LidMeshComp=CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LidMesh"));
LidMeshComp->SetupAttachment(BaseMeshComp);
//数据的初始化
TargetPitch = 110.0f;
}
// Called when the game starts or when spawned
void ASItemChest::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASItemChest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
我想这里的问题应该都不是很大,因为这里除了接口类,其它的都是前几章的内容,感觉有问题的就往回看,还是不明白就问我。
有了接口,有了实现接口的方法,有了对应我们要交互的类,那么接下来就需要写连接玩家操作和物体的那一个链接了,这个链接我们通过自定义组件来解决。自定义组件在ue编辑器里头长这样:
其实你也可以直接写到SCharacter的文件内,但是这么做是为了将玩家基本信息和功能实现分开,更好的进行代码管理,不然一个游戏,你什么都往SCharacter里头写,也许写到最后你发现你SCharacter文件里面有几万行代码,你怎么维护?你怎么debug?很困难的,所以把这些功能分开,有助于我们后期维护代码。
SInteractionComponent.h文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "SInteractionComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ACTIONROGUELIKE_API USInteractionComponent : public UActorComponent
{
GENERATED_BODY()
public:
//此处只定义了这一个函数,就如上面所说,你可以把这个函数丢到SCharacter里头,但是为了方便后期维护,最好还是写一个组件来将两者分开
void PrimaryInteract();
public:
// Sets default values for this component's properties
USInteractionComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
SInteractionComponent.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SInteractionComponent.h"
#include "SGameplayInterface.h"
#include "Engine/World.h"
#include "Engine/HitResult.h"
//后面的很长一串就是我们的实现交互的函数
void USInteractionComponent::PrimaryInteract()
{
//碰撞的结果,这是一个结构体
//FHitResult HitResult;
FVector StartLocation;
FVector EndLocation;
FRotator EyeRotation;
//被检测物体的属性
FCollisionObjectQueryParams objectQueryParams;
objectQueryParams.AddObjectTypesToQuery(ECC_WorldDynamic);
//碰撞体形状
FCollisionShape Sphere;
//球形碰撞体半径
float Radius = 30.0f;
Sphere.SetSphere(Radius);
//一个碰撞结果的数组
TArray<FHitResult>Hits;
//GetOwner()函数对于组件来说就是获得它的父组件,也就是我们的SCharacter对象,也就是玩家
AActor* Owner = GetOwner();
//StartLocation = Owner->GetActorLocation();
//找到玩家的其实位置和停止位置
Owner->GetActorEyesViewPoint(StartLocation, EyeRotation);
//我没有用Gideon的模型,我的模型是一个正方体,所以我直接从正方体中心出发
StartLocation = Owner->GetActorLocation();
EndLocation = StartLocation + (EyeRotation.Vector()*100.0f);
//LineTraceSingleByObjectType函数返回一个bool值,表示是否检测到了一个我们指定的物体
//bool bBlockingHit = GetWorld()->LineTraceSingleByObjectType(HitResult, StartLocation, EndLocation, objectQueryParams);
//SweepMultiByObjectType函数返回一个bool值,表示是否检测到了一个我们指定的物体,这个指定的属性就是objectQueryParams
bool bBlockingHit = GetWorld()->SweepMultiByObjectType(Hits, StartLocation, EndLocation, FQuat::Identity, objectQueryParams, Sphere);
//设置颜色,如果有碰撞就是绿色,没有碰撞就是红色
FColor LineColor = bBlockingHit ? FColor::Green : FColor::Red;
//for循环进行遍历
for (FHitResult HitResult : Hits)
{
//检查是否有碰撞
if (AActor* HitActor = HitResult.GetActor())
{
//检查我们碰撞到的对象是否实现了这个接口
if (HitActor->Implements<USGameplayInterface>())
{
APawn* MyPawn = Cast<APawn>(Owner);
ISGameplayInterface::Execute_Interact(HitActor,MyPawn);
//遍历到的第一个实现了这个接口的类,就停止遍历了
break;
}
}
//画debg用的球
DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, Radius, 32, LineColor, false, 10.0f);
}
//画视线线条
DrawDebugLine(GetWorld(), StartLocation, EndLocation, LineColor , false, 10.0f, 0U, 1.0f);
}
// Sets default values for this component's properties
USInteractionComponent::USInteractionComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void USInteractionComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void USInteractionComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
有没有发现,就一个函数,这个组件内的代码长度能那么长,以后还不知道有多少个这一类的功能要实现,你想想看,是不是用组件的方式独立出来比较好?
好了,我们的组件也写好了,那么最后一步就是:将我们的组件绑定到我们的SCharacter身上,就如同我们前面几章一样的方法
SCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "SCharacter.generated.h"
UCLASS(config = Game)
class ACTIONROGUELIKE_API ASCharacter : public ACharacter
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere)
class USpringArmComponent* SpringArmComp;
UPROPERTY(VisibleAnywhere)
class UCameraComponent* CameraComp;
UPROPERTY(EditAnywhere, Category = Input)
class UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* MoveAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* JumpAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* LookAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* PrimaryAttackAction;
//新增的一个输入事件
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* InteractionAction;
//新增的我们的自定义组件
UPROPERTY(VisibleAnywhere)
class USInteractionComponent* InteractionComp;
protected:
UPROPERTY(EditAnywhere)
class TSubclassOf<AActor> ProjectileClass;
public:
// Sets default values for this character's properties
ASCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
protected:
void Move(const FInputActionValue& Value);
void Look(const FInputActionValue& Value);
void PrimaryAttack();
//新增的交互函数
void PrimaryInteract();
//新增的delay之后调用的函数
void PrimaryAttack_TimeElasped();
protected:
FTimerHandle TimerHandle_PrimaryAttack;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
SCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "SInteractionComponent.h"
#include "TimerManager.h"
//#include "../../../../../UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputSubsystems.h"
//#include "../../../../../UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputComponent.h"
// Sets default values
ASCharacter::ASCharacter()
{
// 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;
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
SpringArmComp->SetupAttachment(RootComponent);
SpringArmComp->bUsePawnControlRotation = true;
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraComp->SetupAttachment(SpringArmComp);
CameraComp->bUsePawnControlRotation = false;
//我们的自定义组件
InteractionComp = CreateDefaultSubobject<USInteractionComponent>(TEXT("InteractionComp"));
}
// Called when the game starts or when spawned
void ASCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASCharacter::Move(const FInputActionValue& Value)
{
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ASCharacter::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void ASCharacter::PrimaryAttack()
{
GetWorldTimerManager().SetTimer(TimerHandle_PrimaryAttack,this,&ASCharacter::PrimaryAttack_TimeElasped,0.2f);
}
//新增的组件触发事件
void ASCharacter::PrimaryInteract()
{
//只要组件存在就会触发组件内部的那个函数
if (InteractionComp)
{
InteractionComp->PrimaryInteract();
}
}
void ASCharacter::PrimaryAttack_TimeElasped()
{
FTransform SpawnTM = FTransform(GetControlRotation(), GetActorLocation());
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
GetWorld()->SpawnActor < AActor >(ProjectileClass, SpawnTM, SpawnParams);
}
// Called every frame
void ASCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ASCharacter::Move);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASCharacter::Look);
EnhancedInputComponent->BindAction(PrimaryAttackAction, ETriggerEvent::Triggered, this, &ASCharacter::PrimaryAttack);
//Interaction
//新增的输入事件
EnhancedInputComponent->BindAction(InteractionAction, ETriggerEvent::Triggered, this, &ASCharacter::PrimaryInteract);
}
}
发现没有,我们只需要绑定组件,然后SCharacter的文件内部就啥都不用加了,加一个输入事件,就是E键嘛,交互键,这样一来,SCharacter的文件看着就很简洁,不会很庞杂。