Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklabs/8bffab28d055b0e58a3f5c4e343e554a to your computer and use it in GitHub Desktop.
Save mklabs/8bffab28d055b0e58a3f5c4e343e554a to your computer and use it in GitHub Desktop.
UserWidget patch for GASALS plugin
From 23d81a547ace5e1672649e6e3eba61ee17ad0c16 Mon Sep 17 00:00:00 2001
From: Mickael Daniel <daniel.mickael@gmail.com>
Date: Sat, 12 Jun 2021 02:22:56 +0200
Subject: [PATCH] feat: slight changes to UGSCUWHud to work with ACharacter and
allow better integrations
---
Source/GSC/Private/UI/GSCUWHud.cpp | 61 +++++++++----------
.../Private/UI/GSCUWHudAttributesDebug.cpp | 12 ++--
.../Actors/Characters/GSCPlayerCharacter.h | 1 +
Source/GSC/Public/UI/GSCUWHud.h | 12 ++--
4 files changed, 42 insertions(+), 44 deletions(-)
diff --git a/Source/GSC/Private/UI/GSCUWHud.cpp b/Source/GSC/Private/UI/GSCUWHud.cpp
index 02e07fe..44e3963 100644
--- a/Source/GSC/Private/UI/GSCUWHud.cpp
+++ b/Source/GSC/Private/UI/GSCUWHud.cpp
@@ -2,59 +2,61 @@
#include "UI/GSCUWHud.h"
+
+#include "Abilities/GSCBlueprintFunctionLibrary.h"
#include "Actors/Characters/GSCCharacterBase.h"
#include "Components/GSCCoreComponent.h"
-void UGSCUWHud::SetOwnerCharacter(AGSCCharacterBase* Character)
+void UGSCUWHud::SetOwnerCharacter(ACharacter* Character)
{
- OwnerCharacter = Character;
+ OwnerCoreComponent = UGSCBlueprintFunctionLibrary::GetCompanionCoreComponent(Character);
}
void UGSCUWHud::InitFromCharacter()
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
- SetHealth(OwnerCharacter->GSCCoreComponent->GetHealth());
- SetStamina(OwnerCharacter->GSCCoreComponent->GetStamina());
- SetMana(OwnerCharacter->GSCCoreComponent->GetMana());
+ SetHealth(OwnerCoreComponent->GetHealth());
+ SetStamina(OwnerCoreComponent->GetStamina());
+ SetMana(OwnerCoreComponent->GetMana());
}
void UGSCUWHud::SetMaxHealth(const float MaxHealth)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
if (HealthText)
{
- HealthText->SetText(FText::FromString(GetAttributeFormatString(OwnerCharacter->GSCCoreComponent->GetHealth(), MaxHealth)));
+ HealthText->SetText(FText::FromString(GetAttributeFormatString(OwnerCoreComponent->GetHealth(), MaxHealth)));
}
if (MaxHealth != 0)
{
- SetHealthPercentage(OwnerCharacter->GSCCoreComponent->GetHealth() / MaxHealth);
+ SetHealthPercentage(OwnerCoreComponent->GetHealth() / MaxHealth);
}
}
void UGSCUWHud::SetHealth(const float Health)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
if (HealthText)
{
- HealthText->SetText(FText::FromString(GetAttributeFormatString(Health, OwnerCharacter->GSCCoreComponent->GetMaxHealth())));
+ HealthText->SetText(FText::FromString(GetAttributeFormatString(Health, OwnerCoreComponent->GetMaxHealth())));
}
- if (OwnerCharacter->GSCCoreComponent->GetMaxHealth() != 0)
+ if (OwnerCoreComponent->GetMaxHealth() != 0)
{
- SetHealthPercentage(Health / OwnerCharacter->GSCCoreComponent->GetMaxHealth());
+ SetHealthPercentage(Health / OwnerCoreComponent->GetMaxHealth());
}
}
@@ -68,37 +70,37 @@ void UGSCUWHud::SetHealthPercentage(const float HealthPercentage)
void UGSCUWHud::SetMaxStamina(const float MaxStamina)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
if (StaminaText)
{
- StaminaText->SetText(FText::FromString(GetAttributeFormatString(OwnerCharacter->GSCCoreComponent->GetStamina(), MaxStamina)));
+ StaminaText->SetText(FText::FromString(GetAttributeFormatString(OwnerCoreComponent->GetStamina(), MaxStamina)));
}
if (MaxStamina != 0)
{
- SetStaminaPercentage(OwnerCharacter->GSCCoreComponent->GetStamina() / MaxStamina);
+ SetStaminaPercentage(OwnerCoreComponent->GetStamina() / MaxStamina);
}
}
void UGSCUWHud::SetStamina(const float Stamina)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
if (StaminaText)
{
- StaminaText->SetText(FText::FromString(GetAttributeFormatString(Stamina, OwnerCharacter->GSCCoreComponent->GetMaxStamina())));
+ StaminaText->SetText(FText::FromString(GetAttributeFormatString(Stamina, OwnerCoreComponent->GetMaxStamina())));
}
- if (OwnerCharacter->GSCCoreComponent->GetMaxStamina() != 0)
+ if (OwnerCoreComponent->GetMaxStamina() != 0)
{
- SetStaminaPercentage(Stamina / OwnerCharacter->GSCCoreComponent->GetMaxStamina());
+ SetStaminaPercentage(Stamina / OwnerCoreComponent->GetMaxStamina());
}
}
@@ -112,7 +114,7 @@ void UGSCUWHud::SetStaminaPercentage(const float StaminaPercentage)
void UGSCUWHud::SetMaxMana(const float MaxMana)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
@@ -120,30 +122,30 @@ void UGSCUWHud::SetMaxMana(const float MaxMana)
if (ManaText)
{
- ManaText->SetText(FText::FromString(GetAttributeFormatString(OwnerCharacter->GSCCoreComponent->GetMana(), MaxMana)));
+ ManaText->SetText(FText::FromString(GetAttributeFormatString(OwnerCoreComponent->GetMana(), MaxMana)));
}
if (MaxMana != 0)
{
- SetManaPercentage(OwnerCharacter->GSCCoreComponent->GetMana() / MaxMana);
+ SetManaPercentage(OwnerCoreComponent->GetMana() / MaxMana);
}
}
void UGSCUWHud::SetMana(const float Mana)
{
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
if (ManaText)
{
- ManaText->SetText(FText::FromString(GetAttributeFormatString(Mana, OwnerCharacter->GSCCoreComponent->GetMaxMana())));
+ ManaText->SetText(FText::FromString(GetAttributeFormatString(Mana, OwnerCoreComponent->GetMaxMana())));
}
- if (OwnerCharacter->GSCCoreComponent->GetMaxMana() != 0)
+ if (OwnerCoreComponent->GetMaxMana() != 0)
{
- SetManaPercentage(Mana / OwnerCharacter->GSCCoreComponent->GetMaxMana());
+ SetManaPercentage(Mana / OwnerCoreComponent->GetMaxMana());
}
}
@@ -160,11 +162,6 @@ void UGSCUWHud::SetAttribute(const FGameplayAttribute Attribute, const float New
OnAttributeChange(Attribute, NewValue, OldValue);
}
-AGSCCharacterBase* UGSCUWHud::GetOwnerCharacter()
-{
- return OwnerCharacter.IsValid() ? OwnerCharacter.Get() : nullptr;
-}
-
FString UGSCUWHud::GetAttributeFormatString(const float BaseValue, const float MaxValue)
{
return FString::Printf(TEXT("%d / %d"), FMath::FloorToInt(BaseValue), FMath::FloorToInt(MaxValue));
diff --git a/Source/GSC/Private/UI/GSCUWHudAttributesDebug.cpp b/Source/GSC/Private/UI/GSCUWHudAttributesDebug.cpp
index ad5c277..9d5d319 100644
--- a/Source/GSC/Private/UI/GSCUWHudAttributesDebug.cpp
+++ b/Source/GSC/Private/UI/GSCUWHudAttributesDebug.cpp
@@ -15,13 +15,13 @@ void UGSCUWHudAttributesDebug::SetAttribute(const FGameplayAttribute Attribute,
{
Super::SetAttribute(Attribute, NewValue, OldValue);
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
UTextBlock* AttributeValue = *AttributeValueMaps.Find(Attribute);
- const float Value = OwnerCharacter->GSCCoreComponent->GetAttributeValue(Attribute);
+ const float Value = OwnerCoreComponent->GetAttributeValue(Attribute);
AttributeValue->SetText(FloatToText(Value));
}
@@ -29,7 +29,7 @@ void UGSCUWHudAttributesDebug::NativeConstruct()
{
Super::NativeConstruct();
- if (!OwnerCharacter.IsValid() || !OwnerCharacter->GSCCoreComponent)
+ if (!OwnerCoreComponent.IsValid())
{
return;
}
@@ -55,9 +55,9 @@ void UGSCUWHudAttributesDebug::NativeConstruct()
DebugActors.AddUnique(TaggedActor);
}
- SetDebugActor(OwnerCharacter.Get());
+ SetDebugActor(OwnerCoreComponent->OwnerActor);
- UGSCAbilitySystemComponent* ASC = OwnerCharacter->GetASC();
+ UGSCAbilitySystemComponent* ASC = Cast<UGSCAbilitySystemComponent>(OwnerCoreComponent->OwnerAbilitySystemComponent);
TArray<FGameplayAttribute> Attributes;
ASC->GetAllAttributes(Attributes);
@@ -70,7 +70,7 @@ void UGSCUWHudAttributesDebug::NativeConstruct()
UImage* Image = DuplicateObject(Cast<UImage>(Row->GetChildAt(1)), this);
UTextBlock* AttributeValue = DuplicateObject(Cast<UTextBlock>(Row->GetChildAt(2)), this);
AttributeName->SetText(FText::FromString(Attribute.GetName()));
- AttributeValue->SetText(FloatToText(OwnerCharacter->GSCCoreComponent->GetAttributeValue(Attribute)));
+ AttributeValue->SetText(FloatToText(OwnerCoreComponent->GetAttributeValue(Attribute)));
AttributeValueMaps.Add(Attribute, AttributeValue);
diff --git a/Source/GSC/Public/Actors/Characters/GSCPlayerCharacter.h b/Source/GSC/Public/Actors/Characters/GSCPlayerCharacter.h
index b7ea365..f213b36 100644
--- a/Source/GSC/Public/Actors/Characters/GSCPlayerCharacter.h
+++ b/Source/GSC/Public/Actors/Characters/GSCPlayerCharacter.h
@@ -15,6 +15,7 @@ class GASCOMPANION_API AGSCPlayerCharacter : public AGSCCharacterBase
GENERATED_BODY()
public:
+
AGSCPlayerCharacter(const FObjectInitializer& ObjectInitializer);
// Only called on the Server. Calls before Server's AcknowledgePossession.
diff --git a/Source/GSC/Public/UI/GSCUWHud.h b/Source/GSC/Public/UI/GSCUWHud.h
index 3c85b5c..4a35733 100644
--- a/Source/GSC/Public/UI/GSCUWHud.h
+++ b/Source/GSC/Public/UI/GSCUWHud.h
@@ -10,6 +10,7 @@
#include "GSCUWHud.generated.h"
+class UGSCCoreComponent;
class AGSCCharacterBase;
UCLASS(Abstract, Blueprintable)
@@ -18,7 +19,9 @@ class GASCOMPANION_API UGSCUWHud : public UGSCUserWidget
GENERATED_BODY()
public:
- void SetOwnerCharacter(AGSCCharacterBase* Character);
+
+ // void SetOwnerCharacter(AGSCCharacterBase* Character);
+ void SetOwnerCharacter(ACharacter* Character);
/** Init widget with attributes from owner character **/
virtual void InitFromCharacter();
@@ -66,12 +69,9 @@ public:
UFUNCTION(BlueprintImplementableEvent, Category = "GSC|UI")
void OnAttributeChange(FGameplayAttribute Attribute, float NewValue, float OldValue);
- /** Returns the Character reference this HUD widget belongs to */
- UFUNCTION(BlueprintCallable, BlueprintPure, Category="GSC|UI")
- AGSCCharacterBase* GetOwnerCharacter();
-
protected:
- TWeakObjectPtr<AGSCCharacterBase> OwnerCharacter;
+ // TWeakObjectPtr<AGSCCharacterBase> OwnerCharacter;
+ TWeakObjectPtr<UGSCCoreComponent> OwnerCoreComponent;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "GSC|UI")
UTextBlock* HealthText = nullptr;
--
2.27.0.windows.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment