요약
1.
용어 설명
1. LoadObject :
여러개를 로드 했더라도 경로가 같다면. 여러번 선언 하더라도 메모리는 1개만 사용한다.
Tip
1.
#include "../GameInfo.h"
#include <Components/ListView.h>
#include "Blueprint/UserWidget.h"
#include "InventoryBase.generated.h"
UCLASS()
class UE11_API UInventoryBase : public UUserWidget
{
GENERATED_BODY()
private:
UListView* m_Listview;
public:
virtual void NativeConstruct() override;
virtual void NativeTick(const FGeometry& _geo, float _DeltaTime) override;
};
void UInventoryBase::NativeConstruct()
{
Super::NativeConstruct();
m_Listview = Cast<UListView>(GetWidgetFromName(FName("ListView")));
m_Listview->SetVisibility(ESlateVisibility::Visible);
}
void UInventoryBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
Super::NativeTick(_geo, _DeltaTime);
}
UCLASS()
class UE11_API UItemDataBase : public UObject
{
GENERATED_BODY()
private:
FString m_IconPath; // 아이콘 이미지 경로
FString m_Description; // 아이템 설명
int32 m_ItemCount; // 아이템 수량
public:
void SetIconPath(const FString& _IconPath) { m_IconPath = _IconPath; }
const FString& GetIconPath() { return m_IconPath; }
void SetItemDesc(const FString& _Desc) { m_Description = _Desc; }
const FString& GetItemDesc() { return m_Description; }
void SetItemCount(const int32 _ItemCount) { m_ItemCount = _ItemCount; }
const int32 GetItemCount() { return m_ItemCount; }
public:
UItemDataBase();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "InventoryBase.h"
#include "ItemDataBase.h"
void UInventoryBase::NativeConstruct()
{
Super::NativeConstruct();
m_Listview = Cast<UListView>(GetWidgetFromName(FName("ListView")));
m_Listview->SetVisibility(ESlateVisibility::Visible);
UItemDataBase* pNewData = NewObject<UItemDataBase>();
pNewData->SetIconPath(TEXT("/Script/Engine.Texture2D'/Game/Viking_RPG_UI_5_0/Buttons/Standart_buttons/Flat_Icon_13_a.Flat_Icon_13_a'"));
pNewData->SetItemDesc(TEXT("그냥 칼임"));
pNewData->SetItemCount(5);
m_Listview->AddItem(pNewData);
}
void UInventoryBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
Super::NativeTick(_geo, _DeltaTime);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include <Components/Image.h>
#include <Components/TextBlock.h>
#include "../GameInfo.h"
#include "Blueprint/UserWidget.h"
#include "InventoryItemBase.generated.h"
/**
*
*/
UCLASS()
class UE11_API UInventoryItemBase : public UUserWidget
{
GENERATED_BODY()
private:
UImage* m_IconImg;
UTextBlock* m_ItemNameText;
UTextBlock* m_CountText;
public:
virtual void NativeConstruct() override;
virtual void NativeTick(const FGeometry& _geo, float _DeltaTime) override;
public:
UFUNCTION(BlueprintCallable)
void InitFromData(UObject* _Data);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "InventoryItemBase.h"
void UInventoryItemBase::NativeConstruct()
{
Super::NativeConstruct();
m_IconImg = Cast<UImage>(GetWidgetFromName(FName("Icon")));
m_ItemNameText = Cast<UTextBlock>(GetWidgetFromName(FName("ItemName")));
m_CountText = Cast<UTextBlock>(GetWidgetFromName(FName("Count")));
}
void UInventoryItemBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
Super::NativeTick(_geo, _DeltaTime);
}
void UInventoryItemBase::InitFromData(UObject* _Data)
{
UItemDataBase* pData = Cast<UItemDataBase>(_Data);
if (IsValid(pData))
{
const FString& IconPath = pData->GetIconPath();
const FString& ItemName = pData->GetItemDesc();
int32 ItemCount = pData->GetItemCount();
UTexture2D* pTex2D = LoadObject<UTexture2D>(nullptr, *IconPath);
if (IsValid(pTex2D))
{
m_IconImg->SetBrushFromTexture(pTex2D);
}
m_ItemNameText->SetText(FText::FromString(ItemName));
m_CountText->SetText(FText::FromString(FString::Printf(TEXT("%d"), ItemCount)));
}
}
앞서 만든 데이터를 코드가 아닌 테이블로 관리 방법
//Item 정보
UENUM(BlueprintType)
enum class EITEM_TYPE : uint8
{
EQUIP_WEAPON,
EQUIP_ARMOR,
EQUIP_AACCESARY,
CONSUMABLE,
QUEST
};
UENUM(BlueprintType)
enum class EITEM_ID : uint8
{
CI_POTION,
CI_POTION_MID,
CI_POTION_MEGA,
EW_GREATEWORD,
EW_LOWGBOW,
EW_POISONDAGGER,
EA_LEATHERARMOR,
EA_KNIGHTARMOR,
AC_RING,
AC_KECKLESS,
};
USTRUCT(Atomic, BlueprintType)
struct FItemDataInfo : public FTableRowBase
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
EITEM_ID ID;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
EITEM_TYPE ItemType;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
FString ItemName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
FString Description;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
FString IconPath;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
float HPHeal;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
float MPHeal;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
float Att;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
float Def;
};