Giter VIP home page Giter VIP logo

saveaccess's Introduction

SaveAccess (C#)

SaveAccess is a system for easily saving data to text files in a JSON format.

Features Include:

  • Saving entire scene trees.
  • Automatic JSON serialization.
  • Support for recursive save structures.
  • ISaveable interface to allow modular save/load structures.

Classes

This plugin consists of 3 central parts: (all are located under the Ardot.SaveSystems namespace)

  • SaveAccess (used to save ISaveable nodes and SaveData to files)
  • SaveData (used to store and move data to and from ISaveable objects)
  • ISaveable (used by SaveAccess to interface with nodes and objects that need to be saved)

Together, these allow entire scenes of ISaveable nodes to be saved in just a few lines of code.

For more information, see the in-code documentation.

Example

You have a node, 'Player'. You need to save its position. This is its code:

public partial class Player : Node2D
{
    public override void _Process()
    {
        //Movement stuff here
    }
}

To make it save, you would need to set it up like this. (this is using the ISaveable default implementation template)

using Ardot.SaveSystems;

public partial class Player : Node2D, ISaveable
{
    public override void _Process(double delta)
    {
        //Movement stuff here
    }
  
    public SaveData Save(params Variant[] parameters)
    {
        //Create and return a new SaveData with its key as GetLoadKey(), and
        //its first value as GlobalPosition
        return new SaveData(GetLoadKey(), GlobalPosition); 
    }   
    
    public void Load(SaveData data, params Variant[] parameters)
    {
        //Setting GlobalPosition to the first value of data, as a Vector2
        GlobalPosition = data[0].AsVector2();
    }
    
    public StringName GetLoadKey(params Variant[] parameters)
    {
        //Returning the LoadKey as 'Player'. It's important that this is
        //unique, otherwise data can be confused.
        //If there were going to be more than one player, we may want
        //this key to include some other identifier, like the node's path
        return "Player";
    }
}

Now, all you would need to save the player would be to have some script that creates a SaveAccess and runs SaveTree().

public partial class SceneRootNode : Node2D
{
    public void SaveScene()
    {
        SaveAccess saveAccess = SaveAccess.Open("user://Save.txt");

        saveAccess.SaveTree(this);
        saveAccess.Commit();
    }
}

Advanced Example

Now, imagine that Player has an inventory. This inventory is a Resource with ISaveable implemented. It won't be automatically saved like nodes, because it isn't directly included in the scene tree.

What you can do is set up Player to save and load its inventory by including the inventory's SaveData in the player's SaveData.

using Ardot.SaveSystems;

public partial class Player : Node2D, ISaveable
{
    public Inventory playerInventory;

    public override void _Process(double delta)
    {
        //Movement stuff here
    }
  
    public SaveData Save(params Variant[] parameters)
    {
        //Create and return a new SaveData with its key as GetLoadKey(),
        //its first value as GlobalPosition,
        //and its second value as playerInventory's SaveData.
        return new SaveData(GetLoadKey(), GlobalPosition, playerInventory.Save());
    }   
    
    public void Load(SaveData data, params Variant[] parameters)
    {
        //Checking if data is null, in case something went wrong.
        if(data == null)
          return;

        //Setting GlobalPosition to the first value of data, as a Vector2
        GlobalPosition = data[0].AsVector2();

        //Loading playerInventory with the second value of data, which is SaveData
        playerInventory.Load(data[1]);
    }
    
    public StringName GetLoadKey(params Variant[] parameters)
    {
        //Returning the LoadKey as 'Player'. It's important that this is
        //unique, otherwise data can be confused.
        //If there were going to be more than one player, we may want
        //this key to include some other identifier, like the node's path
        return "Player";
    }
}

saveaccess's People

Contributors

ardot66 avatar

Stargazers

吃了半条鱼 avatar Wouter Commandeur avatar bluepenguin01 avatar Paul avatar  avatar Maxime Larichellière avatar Tony Bark avatar Arik Rahman avatar BananaHolograma avatar  avatar

Watchers

Samuel DIDIER avatar  avatar

Forkers

bluepenguin01

saveaccess's Issues

OpenEncryptedWithPass() cannot take ModeFlags.WriteRead

I found a bug, that revolves around encrypting a file and calling OpenEncryptedWithPass() function (SaveAccess.cs:90):

FileAccess.OpenEncryptedWithPass(filePath, FileAccess.ModeFlags.WriteRead, pass);

Changing from FileAccess.ModeFlags.WriteRead to FileAccess.ModeFlags.Write will allow the file to be written and saved correctly.

EDIT:

The same goes for OpenEncrypted() function.

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.