요약
1.
용어 설명
1.
Tip
1. AddToRoot :
최상위에 배치함으로써 가비지 컬렉터가 삭제하지 못하도록 설정.
GameInstance는 게임에 1개만 동작된다.
거기에 Inventory Manager를 추가해 관리하도록한다.
이름 : InventoryManager
경로 : 클래스\Manager
class UE11_API UInventoryManager : public UObject
{
GENERATED_BODY()
private:
static UWorld* m_CurWorld;
public:
UInventoryManager();
public:
static UInventoryManager* GetInst(UWorld* _World);
static UInventoryManager* GetInst(UGameInstance* _GameInst);
public:
UDataTable* m_ItemTable;
TMap<EITEM_ID, FName> m_id;
public:
void ShowInventory(bool _Show);
bool IsInventoryOpen();
void SetItemTable(UDataTable* _Table);
const FItemDataInfo* GetItemInfo(EITEM_ID _ItemID);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "InventoryManager.h"
#include "../UE11GameInstance.h"
#include "../UE11GameModeBase.h"
#include "../UMG/MainHUDBase.h"
UWorld* UInventoryManager::m_CurWorld = nullptr;
UInventoryManager::UInventoryManager()
{
}
UInventoryManager* UInventoryManager::GetInst(UWorld* _World)
{
m_CurWorld = _World;
UUE11GameInstance* GameInst = Cast<UUE11GameInstance>(UGameplayStatics::GetGameInstance(_World));
//인벤토리 요청이 처음이라면 안만들어진 상태니 생성 구현
if (!IsValid(GameInst->m_Inventoty))
{
GameInst->m_Inventoty = NewObject<UInventoryManager>();
// 가비지 컬렉터가 지우지 못하도록 최상위 우선순위로 지정.
GameInst->m_Inventoty->AddToRoot();
}
return GameInst->m_Inventoty;
}
UInventoryManager* UInventoryManager::GetInst(UGameInstance* _GameInst)
{
UUE11GameInstance* GameInst = Cast<UUE11GameInstance>(_GameInst);
//인벤토리 요청이 처음이라면 안만들어진 상태니 생성 구현
if (!IsValid(GameInst->m_Inventoty))
{
GameInst->m_Inventoty = NewObject<UInventoryManager>();
// 가비지 컬렉터가 지우지 못하도록 최상위 우선순위로 지정.
GameInst->m_Inventoty->AddToRoot();
}
return GameInst->m_Inventoty;
}
bool UInventoryManager::IsInventoryOpen()
{
AUE11GameModeBase* GameMode = Cast<AUE11GameModeBase>(UGameplayStatics::GetGameMode(m_CurWorld));
//Main Level인지 확인 Main이 아니라면 false.
if (!IsValid(GameMode))
{
return false;
}
// Main Level이라면 GameMode의 HUD를 가져오기
UMainHUDBase* MainHUD = GameMode->GetMainHUD();
UInventoryBase* InventoryWidget = MainHUD->GetInventoryWidget();
return InventoryWidget->IsVisible();
}
void UInventoryManager::ShowInventory(bool _Show)
{
AUE11GameModeBase* GameMode = Cast<AUE11GameModeBase>(UGameplayStatics::GetGameMode(m_CurWorld));
if (!IsValid(GameMode))
return;
UMainHUDBase* MainHUD = GameMode->GetMainHUD();
UInventoryBase* InventoryWidget = MainHUD->GetInventoryWidget();
if (_Show)
InventoryWidget->SetVisibility(ESlateVisibility::Visible);
else
InventoryWidget->SetVisibility(ESlateVisibility::Hidden);
}
void UInventoryManager::SetItemTable(UDataTable* _Table)
{
m_ItemTable = _Table;
//데이터 테이블에 있는 모든 데이터 정보.
FString str;
TArray<FItemDataInfo*> AllItemInfo;
m_ItemTable->GetAllRows<FItemDataInfo>(str, AllItemInfo);
//데이터 테이블에 있는 모든 행이름
TArray<FName> AllRowname;
AllRowname = m_ItemTable->GetRowNames();
for (int32 i = 0; i < AllItemInfo.Num(); ++i)
{
m_id.Add(AllItemInfo[i]->ID, AllRowname[i]);
}
}
const FItemDataInfo* UInventoryManager::GetItemInfo(EITEM_ID _ItemID)
{
FName RowName = m_id.FindRef(_ItemID);
FItemDataInfo* ItemInfo = m_ItemTable->FindRow<FItemDataInfo>(RowName, nullptr);
return ItemInfo;
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
class UInventoryManager; <<<추가!
#include "GameInfo.h"
#include "Engine/GameInstance.h"
#include "UE11GameInstance.generated.h"
UCLASS()
class UE11_API UUE11GameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UUE11GameInstance();
~UUE11GameInstance();
private:
UDataTable* m_MonsterTable;
UDataTable* m_ItemTable;
UInventoryManager* m_Inventoty; <<<추가!
public:
virtual void Init();
public:
const FMonsterTableInfo* FindMonsterTable(const FName& Name);
friend class UInventoryManager; <<<추가!
};
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction<APlayerCharacter>(TEXT("Inventory"), EInputEvent::IE_Pressed, this,&APlayerCharacter::InventoryOnOff);
}
void APlayerCharacter::InventoryOnOff()
{
// Inventory Manager에 접근
// Inventroy가 켜져있으면 끄고 꺼져있으면 켜기
if (IsValid(UInventoryManager::GetInst(GetWorld())))
{
if (UInventoryManager::GetInst(GetWorld())->IsInventoryOpen())
{
UInventoryManager::GetInst(GetWorld())->ShowInventory(false);
}
else
{
UInventoryManager::GetInst(GetWorld())->ShowInventory(true);
}
}
const FItemDataInfo* ItemInfo = UInventoryManager::GetInst(GetWorld())->GetItemInfo(EITEM_ID::CI_POTION);
}