Skill Web
  • Skill Web Documentation
  • 🕹️Quick Start
    • Installation
  • Start Developing
  • 💡General
    • Running the Demos
  • Menu Options
  • UI Customizations
    • uGUI
      • Skill Node
      • Connection Line
      • Web View
      • Hovercard
  • Datasets
  • 🎓Tutorials
    • Runes Demo Walkthrough
  • ✏️Editors
    • Skill Bank
  • Web Creator
  • Settings
  • Components
    • Skill Web Initializer
    • uGUI
      • Skill Node UGUI
      • Connection Line
        • Curved Connection Line
      • Web View UGUI
        • Web View Selector UGUI
      • Skill Hovercard UGUI
    • Player Web Link
  • 📄Scripting API
    • Initialization
  • Skills
    • Skill
      • Skill Dataset Attributes
    • Skill Node
  • Webs
    • Web Graph
    • Web
  • UI
    • Loading a Web
    • Web View Input Handling
    • Customize Connection Line
    • Customize Hovercard
  • Player Web Link
  • Custom Logic
  • Saving & Loading
  • Events
  • Skill Web Settings
  • Procedural Generation (Beta)
  • 🛠️Support
    • Getting Help
  • 📚Changelogs
    • Latest Releases
    • Future Plans
    • ⭐Rate Me?
Powered by GitBook
On this page
  • To Savable (Saving)
  • To Web (Loading)
  • Examples
  • With ESave Free
  • With ESave Pro

Saving & Loading

How to save & load with Skill Web.

PreviousCustom LogicNextEvents

Last updated 11 days ago

Skill Web doesn't have an internal method for saving data, as it's not a save system. However, it does provide a way for save systems to save and load a Web. Any save system that uses JSON or binary serialization should be able to save any web.

To Savable (Saving)

You can create a savable version of a Web (SavableWeb) with the ToSavable method.

SavableWeb mySavableWeb = myWeb.ToSavable();

To Web (Loading)

You can convert a SavableWeb back to a Web with the ToWeb method.

Web myWeb = mySavableWeb.ToWeb();

Examples

The code below is how you can save and load a web with . ESave is my own save system—it's entirely free, and it's been tested with Skill Web. ESave is not a requirement; you can use the save system of your choice. However, the examples below will only work with ESave.

The examples below load a web from saved data and also load it in the UI.

With ESave Free

using Esper.ESave;
using Esper.SkillWeb;
using Esper.SkillWeb.DataManagement;
using Esper.SkillWeb.Graph;
using Esper.SkillWeb.UI.UGUI;
using UnityEngine;

public class SkillWebSaveExample : MonoBehaviour
{
    /// <summary>
    /// The ID used to save and laod the web.
    /// </summary>
    private const string webID = "Web";

    /// <summary>
    /// The save file.
    /// </summary>
    private SaveFile saveFile;

    private void Start()
    {
        // Get the save file from the SaveFileSetup component
        saveFile = GetComponent<SaveFileSetup>().saveFile;

        // Load the data on start
        Load();
    }

    private void OnApplicationQuit()
    {
        // Save on application quit or play mode exited
        Save();
    }

    /// <summary>
    /// Saves the active Web with ESave.
    /// </summary>
    private void Save()
    {
        // Convert the loaded web to a savable version
        var savableWeb = WebViewUGUI.Active.web.ToSavable();

        // Add to save state
        saveFile.AddOrUpdateData(webID, savableWeb);

        // Officially write the data into the save file
        saveFile.Save();
    }

    /// <summary>
    /// Loads a saved Web with ESave.
    /// </summary>
    private void Load()
    {
        if (saveFile.HasData(webID))
        {
            // Get the savable web from the save file
            var savableWeb = saveFile.GetData<SavableWeb>(webID);

            // Convert to a regular web
            var web = savableWeb.ToWeb();

            // Load the web in UI
            WebViewUGUI.Active.Load(web);
        }

        // Check if a saved web exists
        if (saveFile.HasData(webID))
        {
            // Get the savable web from the save file
            var savableWeb = saveFile.GetData<SavableWeb>(webID);

            // Convert to a regular web
            var web = savableWeb.ToWeb();

            // Load the web in UI
            WebViewUGUI.Active.Load(web);
        }
        else
        {
            // Get a web
            var webGraph = SkillWeb.GetWebGraph("My Web Graph");
            var web = new Web(webGraph);

            // Load the web in UI
            WebViewUGUI.Active.Load(web);
        }
    }
}

With ESave Pro

In case you're using the pro version...

using Esper.ESave;
using Esper.SkillWeb;
using Esper.SkillWeb.DataManagement;
using Esper.SkillWeb.Graph;
using Esper.SkillWeb.UI.UGUI;
using UnityEngine;

public class SkillWebSaveExample : MonoBehaviour
{
    /// <summary>
    /// The ID used to save and laod the web.
    /// </summary>
    private const string webID = "Web";

    /// <summary>
    /// The save state.
    /// </summary>
    private SaveState saveState;

    private void Start()
    {
        // Initialize ESave (if not already done)
        ESave.Initialize()
        
        if (!ESave.HasSave(0))
        {
            // Create a save if it doesn't exist
            ESave.CreateSave(0).OnComplete(saveState =>
            {
                this.saveState = saveState;

                // Load the web
                LoadWeb();
            });
        }
        else
        {
            // Load the save if it exists
            ESave.Load(0).OnComplete(saveState =>
            {
                this.saveState = saveState;

                // Load the web
                LoadWeb();
            });
        }
    }

    private void OnApplicationQuit()
    {
        // Save on application quit or play mode exited
        SaveWeb();
    }

    /// <summary>
    /// Saves the active Web with ESave.
    /// </summary>
    public void SaveWeb()
    {
        if (saveState == null)
        {
            return;
        }

        // Convert the loaded web to a savable version
        var savableWeb = WebViewUGUI.Instance.web.ToSavable();

        // Add to save state
        saveState.AddData(webID, savableWeb).OnComplete(success =>
        {
            // Save the state
            ESave.Save(saveState);
        });
    }

    /// <summary>
    /// Loads a saved Web with ESave.
    /// </summary>
    public void LoadWeb()
    {
        if (saveState == null)
        {
            return;
        }

        // Check if a saved web exists
        if (saveState.HasData(webID))
        {
            // Load the saved web
            saveState.GetData<SavableWeb>(webID).OnComplete(savedWeb =>
            {
                // Convert to a regular web
                var web = savedWeb.ToWeb();

                // Load the web in UI
                WebViewUGUI.Instance.Load(web);
            });
        }
        else
        {
            // Get a web
            var webGraph = SkillWeb.GetWebGraph("My Web Graph");
            var web = new Web(webGraph);

            // Load the web in UI
            WebViewUGUI.Instance.Load(web);
        }
    }
}
ESave