Skip to content

Commit 858e438

Browse files
committed
Added Int32 attributes, performance improvements
1 parent 6d270f7 commit 858e438

16 files changed

Lines changed: 719 additions & 284 deletions

AttributesExtension.uplugin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"CreatedByURL": "http://piperift.com/",
1111
"DocsURL": "https://piperift.com/AttributesExtension/",
1212
"SupportURL": "info@piperift.com",
13-
"EngineVersion": "4.22.0",
13+
"EngineVersion": "4.22",
1414
"CanContainContent": false,
1515
"IsBetaVersion": false,
1616
"Modules": [
@@ -43,6 +43,6 @@
4343
"XboxOne",
4444
"Switch"
4545
]
46-
},
46+
}
4747
]
4848
}

Source/Attributes/Private/AttrModifier.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@
44
#include "FloatAttr.h"
55

66

7-
void FAttrModifier::Apply(const FFloatAttr& Attribute, float& ActualValue) const {
8-
if (!FMath::IsNearlyZero(PercentageIncrement)) {
9-
ActualValue *= 1.f + (PercentageIncrement * 0.01f);
7+
void FAttrModifier::Apply(float& Value, float BaseValue) const
8+
{
9+
if (!FMath::IsNearlyZero(LastValueMultiplier))
10+
{
11+
Value *= 1.0f + LastValueMultiplier;
1012
}
11-
12-
if (!FMath::IsNearlyZero(BasePercentageIncrement)) {
13-
ActualValue += Attribute.GetBaseValue() * (BasePercentageIncrement * 0.01f);
13+
if (!FMath::IsNearlyZero(BaseValueMultiplier))
14+
{
15+
Value += BaseValueMultiplier * BaseValue;
1416
}
17+
Value += Increment;
18+
}
1519

16-
ActualValue += ScalarIncrement;
20+
void FAttrModifier::Apply(double& Value, int32 BaseValue) const
21+
{
22+
if (!FMath::IsNearlyZero(LastValueMultiplier))
23+
{
24+
Value *= 1.0 + LastValueMultiplier;
25+
}
26+
if (!FMath::IsNearlyZero(BaseValueMultiplier))
27+
{
28+
Value += BaseValueMultiplier * BaseValue;
29+
}
30+
Value += Increment;
1731
}
1832

1933
void FAttrModifier::StackMods(const TArray<FAttrModifier>& OtherMods)
@@ -22,15 +36,15 @@ void FAttrModifier::StackMods(const TArray<FAttrModifier>& OtherMods)
2236

2337
void FAttrModifier::StackMod(const FAttrModifier& OtherMod)
2438
{
25-
if (!FMath::IsNearlyZero(OtherMod.PercentageIncrement)) {
39+
if (!FMath::IsNearlyZero(OtherMod.LastValueMultiplier))
40+
{
2641
// Stack % value
27-
PercentageIncrement = ((1.f + PercentageIncrement * 0.01f) * (1.f + OtherMod.PercentageIncrement * 0.01f) - 1) * 100.f;
42+
LastValueMultiplier = (1.f + LastValueMultiplier) * (1.f + OtherMod.LastValueMultiplier) - 1.0f;
2843
}
29-
30-
if (!FMath::IsNearlyZero(OtherMod.BasePercentageIncrement)) {
44+
if (!FMath::IsNearlyZero(OtherMod.BaseValueMultiplier))
45+
{
3146
// Stack % base value
32-
BasePercentageIncrement = ((1.f + BasePercentageIncrement * 0.01f) * (1.f + OtherMod.BasePercentageIncrement * 0.01f) - 1) * 100.f;
47+
BaseValueMultiplier = (1.f + BaseValueMultiplier) * (1.f + OtherMod.BaseValueMultiplier) - 1.0f;
3348
}
34-
35-
ScalarIncrement += OtherMod.ScalarIncrement;
49+
Increment += OtherMod.Increment;
3650
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright 2015-2019 Piperift. All Rights Reserved.
2+
3+
#include "BaseAttr.h"
4+
#include "AttributesModule.h"
5+
6+
7+
uint32 FBaseAttr::IdCount{ 0 };
8+
9+
10+
void FBaseAttr::AddModifier(const FAttrModifier& Modifier, const FAttrCategory& Category)
11+
{
12+
if (Category == FAttrCategory::NoCategory)
13+
{
14+
BaseModifiers.Add(Modifier);
15+
}
16+
else if(!Category.IsNone())
17+
{
18+
int32 Index = CategoryMods.IndexOfByKey(Category);
19+
if (Index == INDEX_NONE)
20+
{
21+
// Add category if non existent
22+
Index = CategoryMods.HeapPush({ Category });
23+
}
24+
25+
CategoryMods[Index].Modifiers.Add(Modifier);
26+
}
27+
else
28+
{
29+
UE_LOG(LogAttributes, Warning, TEXT("Attributes: tried to add a modifier to the unexisting category '%s'"), *Category.Name.ToString());
30+
return;
31+
}
32+
33+
RefreshValue();
34+
OnModified.Broadcast(EAttributeOperation::Add, Modifier, Category);
35+
}
36+
37+
bool FBaseAttr::RemoveModifier(const FAttrModifier& Modifier, const FAttrCategory& Category, bool bRemoveFromAllCategories)
38+
{
39+
bool bChanged = false;
40+
41+
if (bRemoveFromAllCategories)
42+
{
43+
// Remove possible modifiers from all categories
44+
bChanged = BaseModifiers.Remove(Modifier) > 0;
45+
46+
for (int32 CatId = 0; CatId < CategoryMods.Num(); ++CatId)
47+
{
48+
FAttributeCategoryMods& CategoryMod = CategoryMods[CatId];
49+
50+
bChanged = CategoryMod.Modifiers.Remove(Modifier) > 0 || bChanged;
51+
52+
if (CategoryMod.Modifiers.Num() <= 0)
53+
{
54+
// Remove category if empty
55+
CategoryMods.HeapRemoveAt(CatId);
56+
57+
//Reduce Id, because current one doesn't exist anymore
58+
--CatId;
59+
}
60+
}
61+
}
62+
else if (Category == FAttrCategory::NoCategory)
63+
{
64+
// Remove modifier from base modifiers
65+
bChanged = BaseModifiers.Remove(Modifier) > 0;
66+
}
67+
else
68+
{
69+
// Remove modifier from a category
70+
int32 Index = CategoryMods.IndexOfByKey(Category);
71+
if (Index != INDEX_NONE)
72+
{
73+
FAttributeCategoryMods& CategoryMod = CategoryMods[Index];
74+
bChanged = CategoryMod.Modifiers.Remove(Modifier) > 0;
75+
76+
if (CategoryMod.Modifiers.Num() <= 0)
77+
{
78+
// Remove category if empty
79+
CategoryMods.HeapRemoveAt(Index);
80+
}
81+
}
82+
else
83+
{
84+
UE_LOG(LogAttributes, Warning, TEXT("Attributes: Tried to remove with modifier category '%s', but it doesnt exist on the attribute"), *Category.Name.ToString());
85+
return false;
86+
}
87+
}
88+
89+
if (bChanged)
90+
{
91+
RefreshValue();
92+
OnModified.Broadcast(EAttributeOperation::Remove, Modifier, Category);
93+
}
94+
return bChanged;
95+
}
96+
97+
const TArray<FAttrModifier>& FBaseAttr::GetModifiers(const FAttrCategory Category) const
98+
{
99+
int32 Index = CategoryMods.IndexOfByKey(Category);
100+
if (Index != INDEX_NONE)
101+
{
102+
return CategoryMods[Index].Modifiers;
103+
}
104+
return BaseModifiers;
105+
}
106+
107+
void FBaseAttr::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+
125+
void FBaseAttr::CleanCategoryModifiers(const FAttrCategory& Category)
126+
{
127+
if (Category.IsNone())
128+
{
129+
if (BaseModifiers.Num() > 0)
130+
{
131+
BaseModifiers.Empty();
132+
133+
// Notify
134+
RefreshValue();
135+
OnModified.Broadcast(EAttributeOperation::RemoveCategory, {}, Category);
136+
}
137+
}
138+
else
139+
{
140+
int32 Index = CategoryMods.IndexOfByKey(Category);
141+
if (Index != INDEX_NONE)
142+
{
143+
// Remove category if empty
144+
CategoryMods.HeapRemoveAt(Index);
145+
146+
// Notify
147+
RefreshValue();
148+
OnModified.Broadcast(EAttributeOperation::RemoveCategory, {}, Category);
149+
}
150+
else
151+
{
152+
UE_LOG(LogAttributes, Warning, TEXT("Attributes: Tried to remove all modifiers of category '%s', but it didnt exist on the attribute"), *Category.Name.ToString());
153+
return;
154+
}
155+
}
156+
}
157+
158+
void FBaseAttr::CleanModifiers()
159+
{
160+
// Is there any modifier at all?
161+
if (BaseModifiers.Num() > 0 || CategoryMods.Num() > 0)
162+
{
163+
BaseModifiers.Empty();
164+
CategoryMods.Empty();
165+
166+
RefreshValue();
167+
OnModified.Broadcast(EAttributeOperation::RemoveAll, {}, FAttrCategory::NoCategory);
168+
}
169+
}

0 commit comments

Comments
 (0)