Skip to content

Commit 36d1fb8

Browse files
committed
VariableBaseSO - Refactor state reset logic and add protection settings
This commit introduces a new method for forcefully resetting the state and adds a protection flag to prevent resets under certain conditions.
1 parent dd8f046 commit 36d1fb8

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

Runtime/Base/VariableBaseSO.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ public abstract class VariableBaseSO : DescriptionBaseSO
99
private void OnEnable()
1010
{
1111
hideFlags = HideFlags.DontUnloadUnusedAsset;
12-
ResetState();
12+
ForceResetState();
1313
}
1414

15-
public override abstract void ResetState();
15+
/// Force reset state regardless of any protection settings.
16+
public abstract void ForceResetState();
17+
18+
public override void ResetState() => ForceResetState();
1619
}
1720

1821
/// <summary>
@@ -22,9 +25,17 @@ private void OnEnable()
2225
/// <typeparam name="T">Unity Serializable</typeparam>
2326
public class VariableBaseSO<T> : VariableBaseSO
2427
{
28+
[Tooltip("Value to which the variable will be reset on OnEnable or ResetState call.")]
2529
[SerializeField] protected T defaultValue;
30+
31+
[Tooltip("Current value of variable - Runtime only value.")]
2632
[SerializeField] private T currentValue;
2733

34+
[Header("Settings")]
35+
[Tooltip(
36+
"If true, the variable will not be reset on plain ResetState call. It will be reset only on OnEnable. Useful for game-system variables.")]
37+
[SerializeField] protected bool protectedDontReset = false;
38+
2839
public virtual T CurrentValue
2940
{
3041
get => currentValue;
@@ -35,6 +46,7 @@ public virtual T CurrentValue
3546
}
3647
}
3748

49+
/// Event invoked when CurrentValue changes.
3850
public UnityAction<T> onValueChanged;
3951

4052
public void SetValue(T value) => CurrentValue = value;
@@ -44,6 +56,14 @@ public virtual T CurrentValue
4456
public static implicit operator T(VariableBaseSO<T> variable) =>
4557
variable != null ? variable.CurrentValue : default;
4658

47-
public override void ResetState() => CurrentValue = defaultValue;
59+
public override void ResetState()
60+
{
61+
if (protectedDontReset) return; // skip reset if protected
62+
63+
ForceResetState();
64+
}
65+
66+
public override void ForceResetState() => CurrentValue = defaultValue;
67+
4868
}
4969
}

Runtime/Base/VariableModifierBaseSO.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public override T CurrentValue
2424
get => ModifyValue(variable.CurrentValue);
2525
set
2626
{
27-
if (disableWrite.Value) return;
27+
if (disableWrite.Value)
28+
{
29+
Debug.LogWarning($"Trying to set value of {name} but writing is disabled.", this);
30+
return;
31+
}
32+
2833
variable.CurrentValue = ModifyValue(value);
2934
}
3035
}

0 commit comments

Comments
 (0)