Skip to content

Commit 362f555

Browse files
committed
1.1 Update, 4.21
1 parent a334bb0 commit 362f555

7 files changed

Lines changed: 110 additions & 18 deletions

File tree

AttributesExtension.uplugin

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"FileVersion": 3,
3-
"Version": 1,
4-
"VersionName": "1.0",
3+
"Version": 2,
4+
"VersionName": "1.1",
55
"FriendlyName": "Attributes Extension",
66
"Description": "A lightweight attributes system for Unreal Engine 4",
77
"MarketplaceURL": "com.epicgames.launcher://ue/marketplace/content/1f0ba37099a14e228a1ce5e4891ed70a",
@@ -10,7 +10,7 @@
1010
"CreatedByURL": "http://piperift.com/",
1111
"DocsURL": "https://piperift.com/AttributesExtension/",
1212
"SupportURL": "info@piperift.com",
13-
"EngineVersion": "4.20.0",
13+
"EngineVersion": "4.21.0",
1414
"CanContainContent": false,
1515
"IsBetaVersion": false,
1616
"Modules": [

Source/Attributes/Private/AttrCategory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "AttributesModule.h"
66

77

8-
const FAttrCategory FAttrCategory::NoCategory(NO_ATTRCATEGORY_NAME);
8+
const FAttrCategory FAttrCategory::NoCategory{ NO_ATTRCATEGORY_NAME };
99

1010
bool FAttrCategory::IsNone() const
1111
{

Source/Attributes/Private/AttrModifier.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,32 @@
55

66

77
void FAttrModifier::Apply(const FFloatAttr& Attribute, float& ActualValue) const {
8-
if (PercentageIncrement != 0.f) {
9-
ActualValue *= 1.f + (PercentageIncrement / 100.f);
10-
}
8+
if (!FMath::IsNearlyZero(PercentageIncrement)) {
9+
ActualValue *= 1.f + (PercentageIncrement * 0.01f);
10+
}
1111

12-
if (BasePercentageIncrement != 0.f) {
13-
ActualValue += Attribute.GetBaseValue() * (BasePercentageIncrement / 100.f);
14-
}
12+
if (!FMath::IsNearlyZero(BasePercentageIncrement)) {
13+
ActualValue += Attribute.GetBaseValue() * (BasePercentageIncrement * 0.01f);
14+
}
1515

16-
ActualValue += ScalarIncrement;
17-
}
16+
ActualValue += ScalarIncrement;
17+
}
18+
19+
void FAttrModifier::StackMods(const TArray<FAttrModifier>& OtherMods)
20+
{
21+
}
22+
23+
void FAttrModifier::StackMod(const FAttrModifier& OtherMod)
24+
{
25+
if (!FMath::IsNearlyZero(OtherMod.PercentageIncrement)) {
26+
// Stack % value
27+
PercentageIncrement = ((1.f + PercentageIncrement * 0.01f) * (1.f + OtherMod.PercentageIncrement * 0.01f) - 1) * 100.f;
28+
}
29+
30+
if (!FMath::IsNearlyZero(OtherMod.BasePercentageIncrement)) {
31+
// Stack % base value
32+
BasePercentageIncrement = ((1.f + BasePercentageIncrement * 0.01f) * (1.f + OtherMod.BasePercentageIncrement * 0.01f) - 1) * 100.f;
33+
}
34+
35+
ScalarIncrement += OtherMod.ScalarIncrement;
36+
}

Source/Attributes/Private/FloatAttr.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool FFloatAttr::RemoveModifier(const FAttrModifier& Modifier, const FAttrCatego
9494
return bChanged;
9595
}
9696

97-
const TArray<FAttrModifier>& FFloatAttr::GetModifiers(const FAttrCategory& Category) const
97+
const TArray<FAttrModifier>& FFloatAttr::GetModifiers(const FAttrCategory Category) const
9898
{
9999
int32 Index = CategoryMods.IndexOfByKey(Category);
100100
if (Index != INDEX_NONE)
@@ -104,6 +104,24 @@ const TArray<FAttrModifier>& FFloatAttr::GetModifiers(const FAttrCategory& Categ
104104
return BaseModifiers;
105105
}
106106

107+
void FFloatAttr::GetModifiedCategories(TArray<FAttrCategory>& OutCategories) const
108+
{
109+
if (BaseModifiers.Num() > 0)
110+
{
111+
OutCategories.Reserve(CategoryMods.Num() + 1);
112+
OutCategories.Add(FAttrCategory::NoCategory);
113+
}
114+
else
115+
{
116+
OutCategories.Reserve(CategoryMods.Num());
117+
}
118+
119+
for (const auto& CategoryMod : CategoryMods)
120+
{
121+
OutCategories.Add(CategoryMod.Category);
122+
}
123+
}
124+
107125
void FFloatAttr::CleanCategoryModifiers(const FAttrCategory& Category)
108126
{
109127
if (Category.IsNone())

Source/Attributes/Public/AttrModifier.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ struct ATTRIBUTES_API FAttrModifier
4848

4949
void Apply(const FFloatAttr& Attribute, float& ActualValue) const;
5050

51+
52+
/** Stack other modifiers values into this mod.
53+
* Now applying this modifier will be equivalent to applying all the others at the same time
54+
*/
55+
void StackMods(const TArray<FAttrModifier>& OtherMods);
56+
57+
/** Stack other modifier's values into this mod.
58+
* Applying this mod will be equivalent to applying both
59+
*/
60+
void StackMod(const FAttrModifier& OtherMod);
61+
5162
//compare two modifications by guid
5263
FORCEINLINE bool operator==(const FAttrModifier& Other) const
5364
{

Source/Attributes/Public/AttributesFunctionLibrary.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ATTRIBUTES_API UAttributesFunctionLibrary : public UBlueprintFunctionLibra
7979
* @return true if any modifier was removed
8080
*/
8181
UFUNCTION(BlueprintCallable, Category = Attributes, meta=(AdvancedDisplay="Category,bRemoveFromAllCategories"))
82-
static bool RemoveModifier(UPARAM(ref) FFloatAttr& Attribute, const FAttrModifier& Modifier, const FAttrCategory Category, bool bRemoveFromAllCategories = false)
82+
static FORCEINLINE bool RemoveModifier(UPARAM(ref) FFloatAttr& Attribute, const FAttrModifier& Modifier, const FAttrCategory Category, bool bRemoveFromAllCategories = false)
8383
{
8484
return Attribute.RemoveModifier(Modifier, Category, bRemoveFromAllCategories);
8585
}
@@ -89,10 +89,21 @@ class ATTRIBUTES_API UAttributesFunctionLibrary : public UBlueprintFunctionLibra
8989
* @param Attribute to get modifiers from
9090
* @return Modifiers of a category as an Array
9191
*/
92+
UFUNCTION(BlueprintPure, Category = Attributes, meta = (AdvancedDisplay = "Category"))
93+
static void GetModifiers(const FFloatAttr& Attribute, const FAttrCategory Category, TArray<FAttrModifier>& Modifiers)
94+
{
95+
Modifiers = Attribute.GetModifiers(Category);
96+
}
97+
98+
/**
99+
* Get all categories where the attribute has any modifiers
100+
* @param Attribute to get categories from
101+
* @return Categories of an attribute as an Array
102+
*/
92103
UFUNCTION(BlueprintPure, Category = Attributes)
93-
static FORCEINLINE TArray<FAttrModifier> GetModifiers(const FFloatAttr& Attribute)
104+
static void GetModifiedCategories(const FFloatAttr& Attribute, TArray<FAttrCategory>& Categories)
94105
{
95-
return Attribute.GetModifiers();
106+
Attribute.GetModifiedCategories(Categories);
96107
}
97108

98109
/**
@@ -122,4 +133,27 @@ class ATTRIBUTES_API UAttributesFunctionLibrary : public UBlueprintFunctionLibra
122133
{
123134
Attribute.OnModified.Remove(Event);
124135
}
136+
137+
138+
/** Stack other modifiers values into this mod.
139+
* Now applying this modifier will be equivalent to applying all the others at the same time
140+
* @param Mods to be stacked together as one
141+
* @return the resulting stacked mod
142+
*/
143+
UFUNCTION(BlueprintPure, Category = "Attributes|Modifiers")
144+
static FORCEINLINE FAttrModifier StackMods(const TArray<FAttrModifier>& Mods) {
145+
FAttrModifier ResultMod{};
146+
ResultMod.StackMods(Mods);
147+
return MoveTemp(ResultMod);
148+
}
149+
150+
/** Stack other modifier's values into target mod.
151+
* Applying this mod will be equivalent to applying both
152+
* @param TargetMod to be modified
153+
* @param OtherMod to be stacked into TargetMod
154+
*/
155+
UFUNCTION(BlueprintCallable, Category = "Attributes|Modifiers")
156+
static void StackMod(UPARAM(ref) FAttrModifier& TargetMod, const FAttrModifier& OtherMod) {
157+
TargetMod.StackMod(OtherMod);
158+
}
125159
};

Source/Attributes/Public/FloatAttr.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,18 @@ struct ATTRIBUTES_API FFloatAttr
7272

7373
void AddModifier(const FAttrModifier& Modifier, const FAttrCategory& Category = FAttrCategory::NoCategory);
7474
bool RemoveModifier(const FAttrModifier& Modifier, const FAttrCategory& Category = FAttrCategory::NoCategory, bool bRemoveFromAllCategories = false);
75-
76-
const TArray<FAttrModifier>& GetModifiers(const FAttrCategory& Category = FAttrCategory::NoCategory) const;
75+
76+
/**
77+
* Get all modifiers of a category, base mods will be returned if category is None
78+
* @return Modifiers of a category as an Array
79+
*/
80+
const TArray<FAttrModifier>& GetModifiers(const FAttrCategory Category = FAttrCategory::NoCategory) const;
81+
82+
/**
83+
* Get all categories where the attribute has any modifiers
84+
* @return Categories of an attribute as an Array
85+
*/
86+
void GetModifiedCategories(TArray<FAttrCategory>& OutCategories) const;
7787
void CleanCategoryModifiers(const FAttrCategory& Category);
7888
void CleanModifiers();
7989

0 commit comments

Comments
 (0)