체력바를 표시해보자
전역으로 사용할 변수를 지정할 수 있는 GameInstance C++ 클래스를 생성
USTRUCT()
struct FMyCharacterData : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Level;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Attack;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 MaxHp;
};
위에서 만든 FMyCharacterData
는 아래와 같이 실제로 볼 수 있다
/**
*
*/
UCLASS()
class TESTUNREALENGINE_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UMyGameInstance();
virtual void Init() override;
FMyCharacterData* GetStatData(int32 Level);
private:
UPROPERTY()
class UDataTable* MyStats;
};
UMyGameInstance::UMyGameInstance()
{
static ConstructorHelpers::FObjectFinder<UDataTable> DATA(TEXT("DataTable'/Game/Data/StatTable.StatTable'"));
MyStats = DATA.Object;
}
void UMyGameInstance::Init()
{
Super::Init();
UE_LOG(LogTemp, Warning, TEXT("MyGameInstance %d"), GetStatData(1)->Attack);
}
FMyCharacterData* UMyGameInstance::GetStatData(int32 Level)
{
return MyStats->FindRow<FMyCharacterData>(*FString::FromInt(Level), TEXT(""));
}
게임의 인스턴스도 변경해야 줘야 동작한다
데미지 처리는 이렇게 하자
void AMyCharacter::AttackCheck()
{
// ...
if (bResult && HitResult.Actor.IsValid())
{
UE_LOG(LogTemp, Log, TEXT("Hit Actor : %s"), *HitResult.Actor->GetName());
FDamageEvent DamageEvent;
HitResult.Actor->TakeDamage(Stat->GetAttack(), DamageEvent, GetController(), this);
}
}