Component that tracks and exposes a global Difficulty value for the game. On awake it sets a singleton Instance, reads the player's saved "selected_difficulty" stat (clamped to Manager.MinDifficulty/MaxDifficulty), and marks its GameObject to not be destroyed on load.
using Sandbox;
public sealed class DifficultyTracker : Component
{
public static DifficultyTracker Instance { get; private set; }
public int Difficulty { get; set; }
protected override void OnAwake()
{
base.OnAwake();
Instance = this;
Difficulty = Math.Clamp((int)Sandbox.Services.Stats.LocalPlayer.Get( "selected_difficulty" ).LastValue, Manager.MinDifficulty, Manager.MaxDifficulty);
GameObject.Flags |= GameObjectFlags.DontDestroyOnLoad;
}
}