Chickensoft.SaveFileBuilder 1.1.0

dotnet add package Chickensoft.SaveFileBuilder --version 1.1.0
NuGet\Install-Package Chickensoft.SaveFileBuilder -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chickensoft.SaveFileBuilder" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chickensoft.SaveFileBuilder --version 1.1.0
#r "nuget: Chickensoft.SaveFileBuilder, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Chickensoft.SaveFileBuilder as a Cake Addin
#addin nuget:?package=Chickensoft.SaveFileBuilder&version=1.1.0

// Install Chickensoft.SaveFileBuilder as a Cake Tool
#tool nuget:?package=Chickensoft.SaveFileBuilder&version=1.1.0

👽 SaveFileBuilder

Chickensoft Badge Discord Read the docs line coverage branch coverage

Compose chunks of save data into a single data type by creating loosely coupled save chunks at various points in the scene tree.


<p align="center"> <img alt="Chickensoft.SaveFileBuilder" src="Chickensoft.SaveFileBuilder/icon.png" width="200"> </p>

🥚 Getting Started

Find the latest version of Chickensoft.SaveFileBuilder on nuget.

dotnet add package Chickensoft.SaveFileBuilder

📄 SaveFile and Root SaveChunk

Find the highest node in your scene tree that needs to be concerned with save data to use as the root of your save file. Use AutoInject to provide the root save chunk to all its descendant nodes.

[!TIP] Check out the Chickensoft Game Demo for a complete, working example of using SaveFileBuilder to save composed states of everything that needs to be persisted in a game.

using Chickensoft.Introspection;
using Chickensoft.AutoInject;
using Chickensoft.SaveFileBuilder;
using Godot;

[Meta(typeof(IAutoNode))]
public partial class Game : Node3D {
  public SaveFile<GameData> SaveFile { get; set; } = default!;

  // Provide the root save chunk to all descendant nodes.
  ISaveChunk<GameData> IProvide<ISaveChunk<GameData>>.Value() => SaveFile.Root;

  public void Setup() {
    SaveFile = new SaveFile<GameData>(
      root: new SaveChunk<GameData>(
        onSave: (chunk) => {
          // Use root chunk to get child chunks that were added to us
          // lower in the scene tree.
          var gameData = new GameData() {
            MapData = chunk.GetChunkSaveData<MapData>(),
            PlayerData = chunk.GetChunkSaveData<PlayerData>(),
            PlayerCameraData = chunk.GetChunkSaveData<PlayerCameraData>()
          };

          return gameData;
        },
        onLoad: (chunk, data) => {
          // Break up the game data and send it to the child chunks so that
          // they can load the data into the nodes they belong to.
          chunk.LoadChunkSaveData(data.MapData);
          chunk.LoadChunkSaveData(data.PlayerData);
          chunk.LoadChunkSaveData(data.PlayerCameraData);
        }
      ),
      onSave: async (GameData data) => {
        // Save the game data to disk.
        var json = JsonSerializer.Serialize(data, JsonOptions);
        await FileSystem.File.WriteAllTextAsync(SaveFilePath, json);
      },
      onLoad: async () => {
        // Load the game data from disk.
        if (!FileSystem.File.Exists(SaveFilePath)) {
          GD.Print("No save file to load :'(");
          return null;
        }

        var json = await FileSystem.File.ReadAllTextAsync(SaveFilePath);
        return JsonSerializer.Deserialize<GameData>(json, JsonOptions);
      }
    );

    ...
  }
}

🍪 Defining Save Chunks

SaveChunks are smaller pieces of save data that are composed together into the overall save file's data. Simply add a chunk to a descendant node of the scene with the root SaveChunk and register it with the root save chunk once you've resolved dependencies with AutoInject.

[Meta(typeof(IAutoNode))]
public partial class Player : CharacterBody3D {
  [Dependency]
  public ISaveChunk<GameData> GameChunk => this.DependOn<ISaveChunk<GameData>>();
  public ISaveChunk<PlayerData> PlayerChunk { get; set; } = default!;

  public void Setup() {
    ...

    PlayerChunk = new SaveChunk<PlayerData>(
      onSave: (chunk) => new PlayerData() {
        GlobalTransform = GlobalTransform,
        StateMachine = (PlayerLogic)PlayerLogic,
        Velocity = Velocity
      },
      onLoad: (chunk, data) => {
        GlobalTransform = data.GlobalTransform;
        Velocity = data.Velocity;
        PlayerLogic.RestoreFrom(data.StateMachine);
        PlayerLogic.Start();
      }
    );

    ...
  }

  public void OnResolved() {
    // Add a child to our parent save chunk (the game chunk) so that it can
    // look up the player chunk when loading and saving the game.
    GameChunk.AddChunk(PlayerChunk);

    ...
  }
}

Once a save chunk has been added to a parent save chunk, the parent save chunk can access it from the callbacks specified by onSave and onLoad, querying its data or forcing it load data into its node.

[!TIP] You can define easily serializable types, as well as serialize entire LogicBlocks with Chickensoft.Serialization.


🐣 Package generated from a 🐤 Chickensoft Template — https://chickensoft.games

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 175 6/11/2024
1.0.0 72 6/10/2024

SaveFileBuilder release.