Skip to content

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.

Key Components

  • 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 current Value back to the _originalValue.
  • PermanentValue(T value, bool updateValue): This updates the "checkpoint." It changes what the Reset() method will revert to. If updateValue is true, it also updates the current Value.

Use Cases

  1. 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, call Reset() to return to the base speed.
  2. Configuration/Settings UI: When a user opens a settings menu, you can track their changes in Value. If they click "Cancel," you call Reset(). If they click "Save," you call PermanentValue().
  3. 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.
  4. Temporary State Overrides: Temporarily changing a system's logging level or a physics property during a specific sequence and reverting it afterward.

Examples

1. Game Character Stats (Temporary Buff)

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}");
    }
}

2. Settings Menu (Save vs. Cancel)

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();
    }
}

3. Managing Multiple Resettable Values

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();
        }
    }
}

Clone this wiki locally