add values to the UI Widgets for Health and Mana Attributes
This commit is contained in:
Binary file not shown.
BIN
Content/Widgets/Gameplay/GameplayWidget_WBP.uasset
LFS
Normal file
BIN
Content/Widgets/Gameplay/GameplayWidget_WBP.uasset
LFS
Normal file
Binary file not shown.
Binary file not shown.
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include "Crunch/Public/Player/CPlayerController.h"
|
#include "Crunch/Public/Player/CPlayerController.h"
|
||||||
|
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
#include "Character/CPlayerCharacter.h"
|
#include "Character/CPlayerCharacter.h"
|
||||||
|
#include "Widgets/GameplayWidget.h"
|
||||||
|
|
||||||
void ACPlayerController::OnPossess(APawn* InPawn)
|
void ACPlayerController::OnPossess(APawn* InPawn)
|
||||||
{
|
{
|
||||||
@@ -20,5 +22,13 @@ void ACPlayerController::AcknowledgePossession(APawn* P)
|
|||||||
if (CPlayerCharacter = Cast<ACPlayerCharacter>(P); IsValid(CPlayerCharacter))
|
if (CPlayerCharacter = Cast<ACPlayerCharacter>(P); IsValid(CPlayerCharacter))
|
||||||
{
|
{
|
||||||
CPlayerCharacter->ClientSideInit();
|
CPlayerCharacter->ClientSideInit();
|
||||||
|
SpawnGameplayWidget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ACPlayerController::SpawnGameplayWidget()
|
||||||
|
{
|
||||||
|
if (!IsLocalPlayerController()) return;
|
||||||
|
GameplayWidget = CreateWidget<UGameplayWidget>(this, GameplayWidgetClass);
|
||||||
|
if (GameplayWidget) GameplayWidget->AddToViewport();
|
||||||
|
}
|
||||||
|
|||||||
27
Source/Crunch/Private/Widgets/GameplayWidget.cpp
Normal file
27
Source/Crunch/Private/Widgets/GameplayWidget.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Multiplayer By Caleb
|
||||||
|
|
||||||
|
|
||||||
|
#include "Widgets/GameplayWidget.h"
|
||||||
|
|
||||||
|
#include "AbilitySystemBlueprintLibrary.h"
|
||||||
|
#include "GAS/CAttributeSet.h"
|
||||||
|
#include "Widgets/ValueGauge.h"
|
||||||
|
|
||||||
|
void UGameplayWidget::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
OwnerAbilitySystemComponent = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwningPlayerPawn());
|
||||||
|
if (OwnerAbilitySystemComponent)
|
||||||
|
{
|
||||||
|
HealthBar->SetAndBoundToGameplayAttribute(
|
||||||
|
OwnerAbilitySystemComponent,
|
||||||
|
UCAttributeSet::GetHealthAttribute(),
|
||||||
|
UCAttributeSet::GetMaxHealthAttribute()
|
||||||
|
);
|
||||||
|
ManaBar->SetAndBoundToGameplayAttribute(
|
||||||
|
OwnerAbilitySystemComponent,
|
||||||
|
UCAttributeSet::GetManaAttribute(),
|
||||||
|
UCAttributeSet::GetMaxManaAttribute()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "Widgets/ValueGauge.h"
|
#include "Widgets/ValueGauge.h"
|
||||||
|
|
||||||
|
#include "AbilitySystemComponent.h"
|
||||||
#include "Components/ProgressBar.h"
|
#include "Components/ProgressBar.h"
|
||||||
#include "Components/TextBlock.h"
|
#include "Components/TextBlock.h"
|
||||||
|
|
||||||
@@ -12,8 +13,27 @@ void UValueGauge::NativePreConstruct()
|
|||||||
ProgressBar->SetFillColorAndOpacity(BarColor);
|
ProgressBar->SetFillColorAndOpacity(BarColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UValueGauge::SetAndBoundToGameplayAttribute(UAbilitySystemComponent* AbilitySystemComponent,
|
||||||
|
const FGameplayAttribute& Attribute,
|
||||||
|
const FGameplayAttribute& MaxAttribute)
|
||||||
|
{
|
||||||
|
if (AbilitySystemComponent)
|
||||||
|
{
|
||||||
|
bool bFound{false};
|
||||||
|
const float Value{AbilitySystemComponent->GetGameplayAttributeValue(Attribute, bFound)};
|
||||||
|
const float MaxValue{AbilitySystemComponent->GetGameplayAttributeValue(MaxAttribute, bFound)};
|
||||||
|
if (bFound) SetValue(Value, MaxValue);
|
||||||
|
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(Attribute).AddUObject(
|
||||||
|
this, &ThisClass::ValueChanged);
|
||||||
|
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(MaxAttribute).AddUObject(
|
||||||
|
this, &ThisClass::MaxValueChanged);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UValueGauge::SetValue(float NewValue, float NewMaxValue)
|
void UValueGauge::SetValue(float NewValue, float NewMaxValue)
|
||||||
{
|
{
|
||||||
|
CachedValue = NewValue;
|
||||||
|
CachedMaxValue = NewMaxValue;
|
||||||
if (NewMaxValue == 0)
|
if (NewMaxValue == 0)
|
||||||
{
|
{
|
||||||
UE_LOG(LogTemp, Warning, TEXT("Value Gauge: %s, NewMaxValue can't be 0"), *GetName());
|
UE_LOG(LogTemp, Warning, TEXT("Value Gauge: %s, NewMaxValue can't be 0"), *GetName());
|
||||||
@@ -29,3 +49,13 @@ void UValueGauge::SetValue(float NewValue, float NewMaxValue)
|
|||||||
FText::AsNumber(NewMaxValue, &FormatOps)
|
FText::AsNumber(NewMaxValue, &FormatOps)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UValueGauge::ValueChanged(const FOnAttributeChangeData& ChangedData)
|
||||||
|
{
|
||||||
|
SetValue(ChangedData.NewValue, CachedMaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UValueGauge::MaxValueChanged(const FOnAttributeChangeData& ChangedData)
|
||||||
|
{
|
||||||
|
SetValue(CachedValue, ChangedData.NewValue);
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,22 +40,23 @@ class CRUNCH_API UCAttributeSet : public UAttributeSet
|
|||||||
public:
|
public:
|
||||||
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
|
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
|
||||||
|
|
||||||
|
ATTRIBUTE_ACCESSORS(UCAttributeSet, Health);
|
||||||
|
ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxHealth);
|
||||||
|
ATTRIBUTE_ACCESSORS(UCAttributeSet, Mana);
|
||||||
|
ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxMana);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UPROPERTY(ReplicatedUsing=OnRep_Health)
|
UPROPERTY(ReplicatedUsing=OnRep_Health)
|
||||||
FGameplayAttributeData Health;
|
FGameplayAttributeData Health;
|
||||||
ATTRIBUTE_ACCESSORS(UCAttributeSet, Health);
|
|
||||||
|
|
||||||
UPROPERTY(ReplicatedUsing=OnRep_MaxHealth)
|
UPROPERTY(ReplicatedUsing=OnRep_MaxHealth)
|
||||||
FGameplayAttributeData MaxHealth;
|
FGameplayAttributeData MaxHealth;
|
||||||
ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxHealth);
|
|
||||||
|
|
||||||
UPROPERTY(ReplicatedUsing=OnRep_Mana)
|
UPROPERTY(ReplicatedUsing=OnRep_Mana)
|
||||||
FGameplayAttributeData Mana;
|
FGameplayAttributeData Mana;
|
||||||
ATTRIBUTE_ACCESSORS(UCAttributeSet, Mana);
|
|
||||||
|
|
||||||
UPROPERTY(ReplicatedUsing=OnRep_MaxMana)
|
UPROPERTY(ReplicatedUsing=OnRep_MaxMana)
|
||||||
FGameplayAttributeData MaxMana;
|
FGameplayAttributeData MaxMana;
|
||||||
ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxMana);
|
|
||||||
|
|
||||||
UFUNCTION()
|
UFUNCTION()
|
||||||
void OnRep_Health(const FGameplayAttributeData& OldValue);
|
void OnRep_Health(const FGameplayAttributeData& OldValue);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "GameFramework/PlayerController.h"
|
#include "GameFramework/PlayerController.h"
|
||||||
#include "CPlayerController.generated.h"
|
#include "CPlayerController.generated.h"
|
||||||
|
|
||||||
|
class UGameplayWidget;
|
||||||
class ACPlayerCharacter;
|
class ACPlayerCharacter;
|
||||||
|
|
||||||
UCLASS()
|
UCLASS()
|
||||||
@@ -18,6 +19,12 @@ public:
|
|||||||
virtual void AcknowledgePossession(APawn* P) override;
|
virtual void AcknowledgePossession(APawn* P) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void SpawnGameplayWidget();
|
||||||
|
|
||||||
UPROPERTY()
|
UPROPERTY()
|
||||||
TObjectPtr<ACPlayerCharacter> CPlayerCharacter;
|
TObjectPtr<ACPlayerCharacter> CPlayerCharacter;
|
||||||
|
UPROPERTY(EditDefaultsOnly, Category= "UI")
|
||||||
|
TSubclassOf<UGameplayWidget> GameplayWidgetClass;
|
||||||
|
UPROPERTY()
|
||||||
|
TObjectPtr<UGameplayWidget> GameplayWidget;
|
||||||
};
|
};
|
||||||
|
|||||||
29
Source/Crunch/Public/Widgets/GameplayWidget.h
Normal file
29
Source/Crunch/Public/Widgets/GameplayWidget.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Multiplayer By Caleb
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
#include "GameplayWidget.generated.h"
|
||||||
|
|
||||||
|
class UAbilitySystemComponent;
|
||||||
|
class UValueGauge;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class CRUNCH_API UGameplayWidget : public UUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
UPROPERTY(meta=(BindWidget))
|
||||||
|
TObjectPtr<UValueGauge> HealthBar;
|
||||||
|
UPROPERTY(meta=(BindWidget))
|
||||||
|
TObjectPtr<UValueGauge> ManaBar;
|
||||||
|
UPROPERTY()
|
||||||
|
TObjectPtr<UAbilitySystemComponent> OwnerAbilitySystemComponent;
|
||||||
|
};
|
||||||
@@ -6,6 +6,9 @@
|
|||||||
#include "Blueprint/UserWidget.h"
|
#include "Blueprint/UserWidget.h"
|
||||||
#include "ValueGauge.generated.h"
|
#include "ValueGauge.generated.h"
|
||||||
|
|
||||||
|
class UAbilitySystemComponent;
|
||||||
|
struct FOnAttributeChangeData;
|
||||||
|
struct FGameplayAttribute;
|
||||||
class UTextBlock;
|
class UTextBlock;
|
||||||
class UProgressBar;
|
class UProgressBar;
|
||||||
/**
|
/**
|
||||||
@@ -17,14 +20,22 @@ class CRUNCH_API UValueGauge : public UUserWidget
|
|||||||
GENERATED_BODY()
|
GENERATED_BODY()
|
||||||
public:
|
public:
|
||||||
virtual void NativePreConstruct() override;
|
virtual void NativePreConstruct() override;
|
||||||
|
void SetAndBoundToGameplayAttribute(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayAttribute& Attribute, const FGameplayAttribute& MaxAttribute);
|
||||||
void SetValue(float NewValue, float NewMaxValue);
|
void SetValue(float NewValue, float NewMaxValue);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
void ValueChanged(const FOnAttributeChangeData& ChangedData);
|
||||||
|
void MaxValueChanged(const FOnAttributeChangeData& ChangedData);
|
||||||
|
|
||||||
|
float CachedValue;
|
||||||
|
float CachedMaxValue;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, Category="Visual")
|
UPROPERTY(EditAnywhere, Category="Visual")
|
||||||
FLinearColor BarColor;
|
FLinearColor BarColor;
|
||||||
UPROPERTY(EditAnywhere, meta=(BindWidget))
|
UPROPERTY(VisibleAnywhere, meta=(BindWidget))
|
||||||
TObjectPtr<UProgressBar> ProgressBar;
|
TObjectPtr<UProgressBar> ProgressBar;
|
||||||
UPROPERTY(EditAnywhere, meta=(BindWidget))
|
UPROPERTY(VisibleAnywhere, meta=(BindWidget))
|
||||||
TObjectPtr<UTextBlock> ValueText;
|
TObjectPtr<UTextBlock> ValueText;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user