UE4 使用c++创建简单的背包系统

本文详细介绍了在Unreal Engine 4中实现背包系统的步骤,包括创建物品数据表、设计交互和自动拾取物品的Actor、在GameState中加载数据表、在PlayerController中处理物品重量和容量,以及在Character中检测可拾取物品。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参照教程  https://wiki.unrealengine.com/index.php?title=C%2B%2B_Inventory#Player_Controller

最后ui提示部分没有做 只是在代码中简单的进行了一下输出  

整体结构

CppInventory inventoryClassDiagram.png

1.首先创建物品的DataTable类型

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

#pragma once

#include "CoreMinimal.h"
#include "Engine/UserDefinedStruct.h"
#include "Engine/DataTable.h"
#include "InventoryItem.generated.h"


/**
 * 
 */
USTRUCT()
struct INVENTORY_API FInventoryItem : public FTableRowBase
{
	GENERATED_BODY()
public:
	FInventoryItem()
	{
		this->Name = FText::FromString("No Name");
		this->Weight = 1;
		this->Value = 1;
		this->Description = FText::FromString("No Description");
	}

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FName ItemID;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FText Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 Weight;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 Value;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UTexture2D* Thumbnail;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FText Description;

	bool operator==(const FInventoryItem& OtherItem) const
	{
		if (ItemID == OtherItem.ItemID)
			return true;
		return false;
	}
	
};

2.在工程中创建DataTable资源 后面会在GameState中进行加载

3.创建可以拾取的物体 一共分为两种类型 一种是通过按键拾取的Interactable 一种是根据角色碰撞盒进行检测的AutoPickup

就不具体展开了 可以看后面的工程源码

4.创建PlayerController GameState 在GameState中加载我们刚才创建的Datatable资源

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

#include "InventoryGameState.h"

AInventoryGameState::AInventoryGameState()
{
	static ConstructorHelpers::FObjectFinder<UDataTable> BP_ItemDB(TEXT("DataTable'/Game/Item/ItemListDT.ItemListDT'"));
	ItemDB = BP_ItemDB.Object;
}

UDataTable* AInventoryGameState::GetItemDB() const
{
	return ItemDB;
}

PlayerController中对拾取到的物品进行重量或者容量的判断 然后进行保存

5.在Character中 对两种不同类型的物品进行检测 是否可拾取 然后调用Controller保存

void AInventoryCharacter::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	CollectAutoPickups();
	CheckForInteractables();
}

void AInventoryCharacter::CollectAutoPickups()
{
	TArray<AActor*> CollectedActors;
	CollectionSphere->GetOverlappingActors(CollectedActors);
	AInventoryController* IController = Cast<AInventoryController>(GetController());
	for (int32 i = 0; i < CollectedActors.Num(); i++)
	{
		AAutoPickup* Pick = Cast<AAutoPickup>(CollectedActors[i]);
		if (Pick && !Pick->IsPendingKill())
		{
			Pick->Collect(IController);
		}
	}
}

void AInventoryCharacter::CheckForInteractables()
{
	FHitResult HitResult;

	int32 Range = 500;
	FVector StartTrace = FollowCamera->GetComponentLocation();
	FVector EndTrace = (FollowCamera->GetForwardVector() * Range) + StartTrace;

	FCollisionQueryParams QueryParams;
	QueryParams.AddIgnoredActor(this);

	AInventoryController* IController = Cast<AInventoryController>(GetController());
	if (IController)
	{
		if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_Visibility, QueryParams))
		{
			AInteractable* Interactable = Cast<AInteractable>(HitResult.GetActor());
			if (Interactable)
			{
				IController->CurrentInteractable = Interactable;
				return;
			}
		}

		IController->CurrentInteractable = nullptr;
	}
}

6.最后在GameMode中使用我们自定义的Controller和GameState 然后在工程中对拾取的按键进行配置

AInventoryGameMode::AInventoryGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
		PlayerControllerClass = AInventoryController::StaticClass();
		GameStateClass = AInventoryGameState::StaticClass();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值