Skip to content

Instantly share code, notes, and snippets.

@pardeike
Created July 8, 2020 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pardeike/acfd2664173cfe2806b09a9530efadb5 to your computer and use it in GitHub Desktop.
Save pardeike/acfd2664173cfe2806b09a9530efadb5 to your computer and use it in GitHub Desktop.
// helper class for scribing ref-deep dicts
public class Scribe_Dictionary<T, S>
{
List<T> tmpKeys;
List<S> tmpVals;
public void Scribe(ref Dictionary<T, S> dict, string name)
{
if (Verse.Scribe.mode == LoadSaveMode.Saving)
{
tmpKeys = new List<T>(dict.Keys);
tmpVals = new List<S>(dict.Values);
}
Scribe_Collections.Look(ref tmpKeys, $"{name}.keys", LookMode.Reference);
Scribe_Collections.Look(ref tmpVals, $"{name}.vals", LookMode.Deep);
if (Verse.Scribe.mode == LoadSaveMode.PostLoadInit)
{
dict = new Dictionary<T, S>();
for (var i = 0; i < tmpKeys.Count; i++)
dict[tmpKeys[i]] = tmpVals[i];
}
}
}
// game state
public class GameState : GameComponent
{
Dictionary<Pawn, State> pawns;
readonly Scribe_Dictionary<Pawn, State> pawnsHelper;
public GameState(Game game) : base()
{
_ = game;
pawns = new Dictionary<Pawn, State>();
pawnsHelper = new Scribe_Dictionary<Pawn, State>();
}
public override void ExposeData()
{
base.ExposeData();
pawnsHelper.Scribe(ref pawns, "pawns");
}
public static State ForPawn(Pawn pawn)
{
var gameState = Current.Game.GetComponent<GameState>();
if (gameState.pawns.TryGetValue(pawn, out var state) == false)
{
state = new State();
gameState.pawns[pawn] = state;
}
return state;
}
}
// custom state per pawn
public class State : IExposable
{
public int someValue = 0;
public void ExposeData()
{
Scribe_Values.Look(ref someValue, "someValue", 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment