-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Utilities.ResetValue
Dennis Steffen edited this page Jan 4, 2026
·
1 revision
The ResetValue<T> utility is a simple wrapper for a variable that allows you to keep track of an "original" or "base" value while working with a "current" value. It is particularly useful in scenarios where you need to temporarily modify a value and then easily revert it to its previous state.
-
Value: The current value being used or modified. -
_originalValue: A private field that stores the "checkpoint" or default value. -
Reset(): A method that sets the currentValueback to the_originalValue. -
PermanentValue(T value, bool updateValue): This updates the "checkpoint." It changes what theReset()method will revert to. IfupdateValueis true, it also updates the currentValue.
-
Game Stat Buffs/Debuffs: Store a character's base movement speed. When they pick up a power-up, change the
Value. When the power-up expires, callReset()to return to the base speed. -
Configuration/Settings UI: When a user opens a settings menu, you can track their changes in
Value. If they click "Cancel," you callReset(). If they click "Save," you callPermanentValue(). -
Object Pooling/Resetting: When an object (like a projectile or enemy) is returned to a pool, you can call
Reset()to ensure its health, speed, or scale returns to its default state before being reused. - Temporary State Overrides: Temporarily changing a system's logging level or a physics property during a specific sequence and reverting it afterward.
In this scenario, we use ResetValue to handle a temporary speed boost.
using Meatcorps.Engine.Core.Utilities;
public class PlayerMovement
{
// The base speed is 5.0f
public ResetValue<float> MovementSpeed = new ResetValue<float>(5.0f);
public void ApplySpeedBoost(float boostMultiplier)
{
MovementSpeed.Value *= boostMultiplier;
Console.WriteLine($"Speed boosted to: {MovementSpeed.Value}");
}
public void OnBoostExpired()
{
MovementSpeed.Reset();
Console.WriteLine($"Speed reset to: {MovementSpeed.Value}");
}
}This example shows how PermanentValue can be used to commit changes.
using Meatcorps.Engine.Core.Utilities;
public class AudioSettings
{
public ResetValue<int> Volume = new ResetValue<int>(70);
public void OnVolumeSliderChanged(int newValue)
{
// Preview the change
Volume.Value = newValue;
}
public void OnSaveButtonClick()
{
// Make the current preview value the new "original" value
Volume.PermanentValue(Volume.Value);
}
public void OnCancelButtonClick()
{
// Revert to the last saved value
Volume.Reset();
}
}Because ResetValue<T> implements the IResetValue interface, you can manage groups of different types of values easily.
using Meatcorps.Engine.Core.Utilities;
using System.Collections.Generic;
public class GameStateManager
{
private List<IResetValue> _resettables = new List<IResetValue>();
public void Register()
{
var health = new ResetValue<int>(100);
var position = new ResetValue<float>(0f);
_resettables.Add(health);
_resettables.Add(position);
}
public void ResetAll()
{
foreach (var item in _resettables)
{
item.Reset();
}
}
}