UE5.1.1C++从0开始(4.虚幻的接口以及交互功能)

这篇具有很好参考价值的文章主要介绍了UE5.1.1C++从0开始(4.虚幻的接口以及交互功能)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这一个章节对于第一次接触虚幻的人来说可以说是最绕的一个点,因为老师突然给你塞了很多的概念,对于这一块的学习,我个人的推荐是:先把蓝图搞明白了,再对应到C++的代码中,不然一定会被整的晕头转向。还是,有不懂的点就来问我。

主要有几个大点:

  1. 接口
  2. 自定义组件
  3. 如何在角色身上使用自定义组件

这些东西在蓝图里面真的非常好实现,但是到了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编辑器里头长这样:

UE5.1.1C++从0开始(4.虚幻的接口以及交互功能),UE5.1.1_C++,c++,ue5,虚幻

其实你也可以直接写到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的文件看着就很简洁,不会很庞杂。文章来源地址https://www.toymoban.com/news/detail-756119.html

到了这里,关于UE5.1.1C++从0开始(4.虚幻的接口以及交互功能)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • UE5.1.1 c++从0开始(14.用C++写UMG类)

    先在这里放一个链接防止第一次看的朋友们不知道我在讲什么:https://www.bilibili.com/video/BV1nU4y1X7iQ/ 这一段的教程不难,唯一新建的C++类是UMG的一个类。这个类用来写绑定在ai身上的血条。 总结一下一共做了什么事情: 给ai写了一个血条ui 重新整理了我们之前写的所有ui,放到

    2024年02月11日
    浏览(32)
  • 【虚幻引擎UE】UE4/UE5 功能性插件推荐及使用介绍 2

    (基于UE5 的Python支持插件) 支持Python语言基于UE5进行开发 GIT地址:https://github.com/mamoniem/UnrealEditorPythonScripts (基于UE5 的Haxe支持插件) Haxe是一门新兴的开源编程语言,是一种开源的编程语言。支持Haxe语言基于UE5进行开发。 GIT地址:https://github.com/RobertBorghese/Haxe-UnrealEngine

    2024年02月06日
    浏览(70)
  • 【虚幻引擎UE】UE4/UE5 功能性插件推荐及使用介绍 1

    实现POST/GET基本方法,并支持HEAD带信息。 使用案例:【虚幻引擎UE】UE5 三种模式调用API详解(案例基于免费Varest插件) 能够快速实现打开对话框获取数据的导入、导出路径。 某月限免插件,比较方便的地图插件,可以改改样式直接用。 http下载数据到指定路径 按空格可以切

    2023年04月11日
    浏览(63)
  • UE5功能-与网页交互

            首先下载WebUI插件,Releases · tracerinteractive/UnrealEngine (github.com). 插件页面         选择相应引擎版本下载,我这里选择的5.1.(ps:如果无法打开相应界面,需要先把Epic账号与Git账号关联,首先登录Epic官网,然后点击头像,点击个人信息,选择左边的连接按钮,在连接页面选择Git

    2024年02月12日
    浏览(35)
  • 【虚幻引擎】UE4/UE5插件

    Blank:空白插件,可以从头开始自己定义想要的插件风格和内容,用此模板创建的插件不会有注册或者菜单输入。 BlueprintLibrary:创建一个含有蓝图函数库的插件,此模板函数都是静态全局函数,可以在蓝图中直接调用。 ContentOnly:创建一个只包含内容的空白文件 Editor Toolba

    2024年02月05日
    浏览(46)
  • 【虚幻引擎】UE4/UE5 材质

      基础颜色(BaseColor) :材质本身的颜色,例如绿色蓝色等 金属度(Metallic) :金属度,材质是否含有金属光泽 粗糙度(Roughness) :粗糙或者平滑度,物体表面的粗糙程度 自发光(EmissiveColor) :物体本身是否发光 透明度(Opactity) :物体表面是否透明,适用于 半透明(Translucent)、

    2024年02月02日
    浏览(49)
  • ue5,ue4(虚幻5)安卓触摸

    这个是单方向的,如果要用双方向的,就是 x和y同时运用的话建议用向量2d(变量),把它分割成x和y就可以了,我门平时在网上见到的触摸都是:按下到连续,我这个方法是按下-连续-停止,记住,y的变量要是负数,还有就是,灵敏度调大的时候,屏幕会跳动,由于x和y的屏

    2024年02月11日
    浏览(59)
  • unrealengine(UE5)虚幻引擎下载安装

    早就听说功能强大的UE4游戏引擎,今天终于可以开始学习UE4了,还是有点点小激动哈,兄弟萌撸起来。。。。。。 UE4官网 进入ue5官网,现在是ue5版本了,首先需要先注册,才能下载ue4安装器 首先安装上一步的ue5下载器,安装完成后会自动弹出登录页面 点击“虚幻引擎”来安

    2024年02月12日
    浏览(58)
  • 【虚幻引擎】UE4/UE5 pak挂载

     找到:D:UEUE_4.27EngineBinariesWin64,  WindowS+R打开CMD命令 运行UnrealPak,运行结果如下      注意如果想要加载Pak内资源,那么这些资源必须是经过Cook的。如果打包的是未Cook的资源,那么即使Pak挂载成功,也不可能会成功加载Pak内资源。  Cook好之后,存储的路径在你的I:DBJ

    2024年02月10日
    浏览(48)
  • 【虚幻引擎UE】UE4/UE5 新人科普向

    Unreal Engine是当前最为流行的游戏引擎之一,具有丰富的游戏开发功能和强大的游戏引擎渲染能力。 UE5官方文档:UE5官方文档非常详细,介绍了UE5的各个功能和应用,适合入门学习和深入探究。链接:https://docs.unrealengine.com/5.1/zh-CN/ UE5中文社区:该社区聚集了大量的UE5开发者,

    2024年02月09日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包