Git Product home page Git Product logo

savefilebuilder's Introduction

๐Ÿ‘ฝ 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.


Chickensoft.SaveFileBuilder

๐Ÿฅš 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

savefilebuilder's People

Contributors

definitelyokay avatar

Stargazers

SabishiDev avatar Pierre-Yves Lebecq avatar Lei Chen avatar Nick avatar Randall Ehret avatar Nenpotato avatar Lysov Andrey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.