当前位置: 首页 > news >正文

UE求职Demo开发日志#32 优化#1 交互逻辑实现接口、提取Bag和Warehouse的父类

1 定义并实现交互接口

接口定义:

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyInterActInterface.generated.h"// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterActInterface : public UInterface
{GENERATED_BODY()
};/*** */
class ARPG_CPLUS_API IMyInterActInterface
{GENERATED_BODY()// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:UFUNCTION(BlueprintCallable, BlueprintNativeEvent)void OnInterAct(APawn* InstigatorPawn);
};

 实现接口:

class ARPG_CPLUS_API AInterActTrigger : public AActor,public IMyInterActInterface
{GENERATED_BODY()public:	// Sets default values for this actor's propertiesAInterActTrigger();virtual void OnInterAct_Implementation(APawn* InstigatorPawn)override;.......}

实现里绑定碰撞函数,重叠时设置指针:

// Fill out your copyright notice in the Description page of Project Settings.#include "InterAct/InterActTrigger.h"
#include "Components/BoxComponent.h"
#include "Player/MyPlayer.h"
// Sets default values
AInterActTrigger::AInterActTrigger()
{// 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;// 创建 BoxCollision 组件BoxCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollision"));BoxCollision->SetupAttachment(RootComponent); // 绑定到根组件BoxCollision->SetBoxExtent(FVector(50.f, 50.f, 50.f)); // 设置碰撞盒的大小BoxCollision->SetCollisionProfileName(TEXT("Trigger"));}// Called when the game starts or when spawned
void AInterActTrigger::BeginPlay()
{Super::BeginPlay();// 绑定重叠事件BoxCollision->OnComponentBeginOverlap.AddDynamic(this, &AInterActTrigger::OnBeginOverlap);BoxCollision->OnComponentEndOverlap.AddDynamic(this, &AInterActTrigger::OnEndOverlap);}// Called every frame
void AInterActTrigger::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void AInterActTrigger::OnInterAct_Implementation(APawn* InstigatorPawn)
{UE_LOG(LogTemp,Warning,TEXT("OnInterActInC++"));
}// 开始重叠事件
void AInterActTrigger::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{//UE_LOG(LogTemp, Warning, TEXT("Begin Overlap with: %s"), *OtherActor->GetName());if (OtherActor && OtherActor != this){//UE_LOG(LogTemp, Warning, TEXT("Begin Overlap with: %s"), *OtherActor->GetName());if(AMyPlayer* MyPlayer=Cast<AMyPlayer>(OtherActor)){MyPlayer->TriggerActorRef=this;}else{//UE_LOG(LogTemp, Warning, TEXT("AInterActTrigger-->MyPlayer is Not Valid"));}}
}// 结束重叠事件
void AInterActTrigger::OnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{if (OtherActor && OtherActor != this){//UE_LOG(LogTemp, Warning, TEXT("End Overlap with: %s"), *OtherActor->GetName());if(AMyPlayer* MyPlayer=Cast<AMyPlayer>(OtherActor)){MyPlayer->TriggerActorRef=nullptr;}else{//UE_LOG(LogTemp, Warning, TEXT("AInterActTrigger-->MyPlayer is Not Valid"));}}
}

这时就能把那一坨东西改为这简洁的一行:

优雅多了() ,然后就是恢复功能了。

2 把实现搬到各接口中

例如这个:

3 提取Bag和Warehouse父类 

这里只贴提取完的父类声明,不得不说比之前舒服多了

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Enum/My_Enum.h"
#include "ItemManageBaseComponent.generated.h"class UGameplayAbility;USTRUCT(BlueprintType)
struct ARPG_CPLUS_API FMyItemInfo
{GENERATED_USTRUCT_BODY()UPROPERTY(EditAnywhere, BlueprintReadOnly)int32 ItemId;UPROPERTY(EditAnywhere, BlueprintReadOnly)int64 CurrentOwnedCnt;UPROPERTY(EditAnywhere, BlueprintReadOnly)FString DisplayName;UPROPERTY(EditAnywhere, BlueprintReadOnly)EMyItemType ItemType{EMyItemType::Item};UPROPERTY(EditAnywhere, BlueprintReadOnly)EMyArmType ArmType{EMyArmType::None};UPROPERTY(EditAnywhere, BlueprintReadWrite)EMyItemLocation ItemLocation{EMyItemLocation::None};FMyItemInfo(int32 ItemId,int64 CurrentOwnedCnt,FString DisplayName) : ItemId(ItemId), CurrentOwnedCnt(CurrentOwnedCnt), DisplayName(DisplayName){}FMyItemInfo(){ItemId = 0;CurrentOwnedCnt=0;DisplayName=FString("Default");}
};USTRUCT(BlueprintType)
struct ARPG_CPLUS_API FMyItemData:public FTableRowBase
{GENERATED_USTRUCT_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite)int ItemId;UPROPERTY(EditAnywhere, BlueprintReadWrite)int MaxOwnedCnt;UPROPERTY(EditAnywhere, BlueprintReadWrite)FString ItemBaseName;UPROPERTY(EditAnywhere, BlueprintReadWrite)UTexture2D* Texture;
};USTRUCT(BlueprintType)
struct ARPG_CPLUS_API FAttributeModifier
{GENERATED_USTRUCT_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite)FString AttributeName;UPROPERTY(EditAnywhere, BlueprintReadWrite)bool bIsPercent;UPROPERTY(EditAnywhere, BlueprintReadWrite)float PercentValue;UPROPERTY(EditAnywhere, BlueprintReadWrite)float AddedValue;
};USTRUCT(BlueprintType)
struct ARPG_CPLUS_API FAttrModItemData:public FMyItemData
{GENERATED_USTRUCT_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite)TArray<FAttributeModifier> AttributeMods;UPROPERTY(EditAnywhere, BlueprintReadWrite)TArray<TSubclassOf<UGameplayAbility>> GAsToAdd;
};UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ARPG_CPLUS_API UItemManageBaseComponent : public UActorComponent
{GENERATED_BODY()public:	// Sets default values for this component's propertiesUItemManageBaseComponent();UItemManageBaseComponent(int MaxCellCntLimit,EMyItemLocation ItemLocation):MaxCellCntLimit(MaxCellCntLimit),ItemLocation(ItemLocation){PrimaryComponentTick.bCanEverTick = true;static ConstructorHelpers::FObjectFinder<UDataTable> DataTableAsset(TEXT("DataTable'/Game/Data/DataTable/ItemsData.ItemsData'"));if (DataTableAsset.Succeeded()){ItemDataTable = DataTableAsset.Object;}}UFUNCTION(BlueprintCallable)virtual void SaveData();UFUNCTION(BlueprintCallable)virtual void LoadData();UFUNCTION(BlueprintCallable)virtual bool AddItemByArrayWithSave(const TArray<FMyItemInfo> ItemsToAdd);UFUNCTION(BlueprintCallable)virtual bool AddItemWithSave(FMyItemInfo& ItemToAdd);UFUNCTION(BlueprintCallable)virtual bool RemoveItemWithSave(const int ItemId,const int SubCnt);UFUNCTION(BlueprintCallable)virtual bool AddItemByArray(TArray<FMyItemInfo> ItemsToAdd);UFUNCTION(BlueprintCallable)virtual bool AddItem(FMyItemInfo& ItemToAdd);UFUNCTION(BlueprintCallable)virtual int GetAvailableSpace()const;UFUNCTION(BlueprintCallable)virtual bool RemoveItem(const int ItemId,const int SubCnt);UFUNCTION(BlueprintCallable)virtual void LogMes()const;UFUNCTION(BlueprintCallable)virtual FMyItemInfo GetItemInfoByItemId(int& ItemId);static UDataTable* ItemDataTable;UFUNCTION(BlueprintCallable)static FMyItemData GetItemDataByItemId(const int ItemId);UFUNCTION(BlueprintCallable)virtual bool CheckIsEnough(const int ItemId,const int Cnt)const;protected:UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "ItemData")TArray<FMyItemInfo> Items;UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "ItemData")int MaxCellCntLimit{25};UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "ItemData")EMyItemLocation ItemLocation{EMyItemLocation::None};// Called when the game startsvirtual void BeginPlay() override;public:	// Called every framevirtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;};

http://www.lryc.cn/news/538050.html

相关文章:

  • Visonpro 检测是否有缺齿
  • 第1章大型互联网公司的基础架构——1.6 RPC服务
  • 今日AI和商界事件(2025-02-15)
  • 算法题(69):搜索插入位置
  • 在 Linux 系统中,tc(Traffic Control) QoS 常用命令简介
  • 如何画产品功能图、结构图
  • 4090单卡挑战DeepSeek r1 671b:尝试量化后的心得的分享
  • SpringBoot速成(12)文章分类P15-P19
  • C++17中的clamp函数
  • 配置Open-R1,评测第三方蒸馏模型的性能1
  • Chrome插件开发流程
  • 物联网行业通识:从入门到深度解析
  • 【做一个微信小程序】校园事件页面实现
  • C++基础系列【14】继承与多态
  • DeepSeek-R1 大模型本地部署指南
  • 在conda环境下,安装Pytorch和CUDA
  • Java里int和Integer的区别?
  • 【第13章:自监督学习与少样本学习—13.4 自监督学习与少样本学习的未来研究方向与挑战】
  • 【NLP】文本预处理
  • deepseek r1从零搭建本地知识库10:嵌入模型和知识库建设
  • Linux-文件IO
  • 3d pose 学习笔记2025
  • LC-随机链表的复制、排序链表、合并K个升序链表、LRU缓存
  • 静态页面在安卓端可以正常显示,但是在ios打开这个页面就需要刷新才能显示全图片
  • 四元数如何用于 3D 旋转(代替欧拉角和旋转矩阵)【ESP32指向鼠标】
  • JavaScript 内置对象-日期对象
  • 本地大模型编程实战(19)RAG(Retrieval Augmented Generation,检索增强生成)(3)
  • DeepSeek与ChatGPT:AI语言模型的全面对决
  • 2024年年终总结
  • 利用 Valgrind 检测 C++ 内存泄露