Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Persistence Data Layer documentation

Discussion in 'Game Foundation' started by SkylorBeck, Sep 14, 2020.

  1. SkylorBeck

    SkylorBeck

    Joined:
    Aug 29, 2020
    Posts:
    13
    Hello. I just started using Game Foundation last night and I cannot for the life of me figure out how to use the Persistence Data Layer. The only documentation I can find is from very old versions. I've managed to get a full inventory, currency and transaction system in place but it's all kinda pointless without the ability to save its state. Alternatively, I have a serializer and data storage system already built that I could just integrate the Memory Data Layer into but I have no idea how to access that layer once it's initialized.

    Really impressed by the work you guys have done btw.
     
  2. SkylorBeck

    SkylorBeck

    Joined:
    Aug 29, 2020
    Posts:
    13
    Okay minor update. https://docs.unity3d.com/Packages/com.unity.game.foundation@0.3/manual/GFTutorial.html is outdated but with some small changes it appears to be working. Here's my code:

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.GameFoundation;
    5. using UnityEngine.GameFoundation.DefaultLayers;
    6. using UnityEngine.GameFoundation.DefaultLayers.Persistence;
    7.  
    8. public class GFInit : MonoBehaviour
    9. {
    10.     PersistenceDataLayer dataLayerP;
    11.     IDataPersistence localPersistence;
    12.     JsonDataSerializer dataSerializer;
    13.     void Start()
    14.     {
    15.         dataSerializer = new JsonDataSerializer();
    16.         localPersistence = new LocalPersistence("FILENAME", dataSerializer);
    17.         dataLayerP = new PersistenceDataLayer(localPersistence);
    18.         GameFoundation.Initialize(dataLayerP, OnInitSucceeded, OnInitFailed);
    19.     }
    20.     void OnInitSucceeded()
    21.     {
    22.  
    23.     }
    24.     void OnInitFailed(Exception error)
    25.     {
    26.         Debug.LogException(error);
    27.     }
    28.     public void Save()
    29.     {
    30.         dataLayerP.Save();
    31.     }
    32.  
    33.     public void Load()
    34.     {
    35.         GameFoundation.Uninitialize();
    36.         GameFoundation.Initialize(dataLayerP);
    37.     }
    38.     public void Delete()
    39.     {
    40.         LocalPersistence persistence = new LocalPersistence("FILENAME", dataSerializer);
    41.         persistence.Delete();
    42.         Load();
    43.     }
    44. }
    45.  
     
    Last edited: Sep 14, 2020
    Nowlz likes this.
  3. SkylorBeck

    SkylorBeck

    Joined:
    Aug 29, 2020
    Posts:
    13
    Added a delete function and changed thread type to feedback. It's really simple when you know how to use it proper. Love it.
     
    Last edited: Sep 14, 2020
    erika_d likes this.
  4. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @skylortrexler

    Glad you're enjoying Game Foundation! Thanks for the feedback that Persistence Data Layer needs better documentation, I'll add a task for that. Let us know if you have any other questions!
     
    mingz-unity likes this.