Switch Incrementing Level over to datatable

This commit is contained in:
baz 2025-07-15 18:31:03 +01:00
parent bd54f30507
commit 5fac9c7f73
7 changed files with 98 additions and 8 deletions

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/DT_PlayerLevels.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Player/DT_PlayerLevelsTest.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -3,6 +3,8 @@
#include "EXPComponent.h"
#include "TableRows/ExpTableRow.h"
// Sets default values for this component's properties
UEXPComponent::UEXPComponent()
{
@ -19,8 +21,30 @@ void UEXPComponent::IncrementEXP(int value)
int oldLevel = CurrentLevel;
CurrentEXP += value;
OnEXPGained.Broadcast(CurrentEXP, GetCurrentLevelPercent());
if (NextLevelRow.Level >= 0)
{
if (CurrentEXP >= NextLevelRow.CumulativeExpForNextLevel)
{
CurrentLevel = NextLevelRow.Level;
if (FExpTableRow* newRow = LevelsTable->FindRow<FExpTableRow>(FName(*FString::FromInt(NextLevelRow.Level + 1)),"", true))
{
NextLevelRow = *newRow;
}
else
{
NextLevelRow.Level++;
NextLevelRow.CumulativeExpForPreviousLevel = NextLevelRow.CumulativeExpForNextLevel;
NextLevelRow.ExpRequiredForNextLevel += 16;
NextLevelRow.CumulativeExpForNextLevel += NextLevelRow.ExpRequiredForNextLevel;
}
OnEXPLevelUp.Broadcast(CurrentLevel);
}
}
else
{
CurrentLevel = FMath::Floor(CurrentEXP / 100.0f);
if (CurrentLevel != oldLevel)
@ -29,6 +53,9 @@ void UEXPComponent::IncrementEXP(int value)
}
}
OnEXPGained.Broadcast(CurrentEXP, GetCurrentLevelPercent());
}
void UEXPComponent::SetCurrentEXP(int value)
{
int oldEXP = CurrentEXP;
@ -58,6 +85,14 @@ int UEXPComponent::GetCurrentLevel()
void UEXPComponent::Reset()
{
if (LevelsTable)
{
if (FExpTableRow* newRow = LevelsTable->FindRow<FExpTableRow>(FName("1"), "", true))
{
NextLevelRow = *newRow;
}
}
CurrentEXP = 0;
CurrentLevel = 0;
OnEXPGained.Broadcast(CurrentEXP, GetCurrentLevelPercent());
@ -66,7 +101,15 @@ void UEXPComponent::Reset()
float UEXPComponent::GetCurrentLevelPercent()
{
return (CurrentEXP % 100) / 100.0f;
int adjustedCurrentExp = CurrentEXP - NextLevelRow.CumulativeExpForPreviousLevel;
float res = static_cast<float>(adjustedCurrentExp) / static_cast<float>(NextLevelRow.ExpRequiredForNextLevel);
if (FMath::IsNaN(res))
{
return 0.0f;
}
return res;
}
// Called when the game starts

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "TableRows/ExpTableRow.h"
#include "EXPComponent.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnEXPGainedDelegate, int, exp, float, currentLevelPercent);
@ -21,11 +22,16 @@ public:
UPROPERTY(BlueprintAssignable, Category="EXP")
FOnEXPLevelUpDelegate OnEXPLevelUp;
UPROPERTY(EditDefaultsOnly, Category="EXP")
TObjectPtr<UDataTable> LevelsTable;
protected:
int CurrentEXP = 0;
int CurrentLevel = 0;
FExpTableRow NextLevelRow;
public:
// Sets default values for this component's properties
UEXPComponent();
@ -51,4 +57,8 @@ public:
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
void ProcessExp(int oldEXP, int oldLevel, int newEXP);
};

View File

@ -0,0 +1,4 @@
// Louis Hobbs | 2024-2025
#include "ExpTableRow.h"

View File

@ -0,0 +1,27 @@
// Louis Hobbs | 2024-2025
#pragma once
#include "CoreMinimal.h"
#include "ExpTableRow.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FExpTableRow : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int Level;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int ExpRequiredForNextLevel;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int CumulativeExpForNextLevel;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int CumulativeExpForPreviousLevel;
};