A game component that tracks the player's score and death count, exposes a singleton Instance, loads saved data on start, updates high score via SaveManager, awards credits to a MetaProgression component when score is added, and provides methods to add deaths and reset score.
using Sandbox;
using System;
using System.Linq;
public class ScoreManager : Component
{
public static ScoreManager Instance { get; private set; }
public int Score { get; private set; } = 0;
public int Deaths { get; private set; } = 0;
protected override void OnStart()
{
Instance = this;
SaveManager.Load();
}
public void AddScore( int amount = 1 )
{
Score += amount;
SaveManager.TryUpdateHighScore( Score );
var meta = Scene.GetAllComponents<MetaProgression>().FirstOrDefault();
meta?.AddCredits( 25 );
}
public void AddDeath( int amount = 1 )
{
Deaths += amount;
Score = Math.Max( 0, Score - 20 );
}
public void ResetScore()
{
Score = 0;
}
}