Page 1 of 1

Storing objects in lua

Posted: Thu Jan 23, 2020 8:28 pm
by VoodooDetective
I've got a minigame in my game that has an initial state, and as you play that state needs to be modified and saved. I was contemplating trying to use the lua state so that my minigame state gets saved with all the rest of the state, but I wasn't sure the best approach.

Here's what my objects look like:

Code: Select all

    /// <summary>
    /// Represents the state of the minigame.
    /// </summary>
    public class MinigameState
    {
        public bool complete { get; set; }
        public PatientDifficulty currentDifficulty { get; set; }
        public List<Patient> easy { get; set; }
        public List<Patient> medium { get; set; }
        public List<Patient> hard { get; set; }
    }

    /// <summary>
    /// Represents a patient with an ailment. 
    /// </summary>
    public class Patient
    {
        public PatientStatus status { get; set; }
        public PatientWalkerAnimator walkerAnimator { get; set; }
        public PatientPortraitAnimator portraitAnimator { get; set; }
        public string ailmentTextId { get; set; } // unique id
        public List<CureIngredientInformation> cureIngredientList { get; set; }
    }

    /// <summary>
    /// CureIngredientInformation contains information about a cure's ingredient and it's value.
    /// </summary>
    public class CureIngredientInformation
    {
        public string name { get; set; }
        public float value { get; set; }
        public bool isBonus { get; set; }
    }

    /// <summary>
    /// Various statuses a patient can have.  If they are healed, the are removed from the list during the scoring phase.
    /// </summary>
    public enum PatientStatus
    {
        Sick,
        Angry,
        Healthy,
        VeryHealthy,
    }

    /// <summary>
    /// Enum representing difficulty of the patient.
    /// </summary>
    public enum PatientDifficulty
    {
        Easy,
        Medium,
        Hard,
    }
As you play, the game will remove patients and update their status as they are healed or made worse.


My question is should I be side-stepping the save system and just serialize the data to a text file, or is there some way to write this object to a lua table or something.

ALSO if there is some way to do that, what's the best place to do it? Start of a newgame?

Re: Storing objects in lua

Posted: Thu Jan 23, 2020 9:55 pm
by Tony Li
Use the full save system (SaveSystem, [Json/Binary]DataSerializer, [PlayerPrefs/Disk]SavedGameDataStorer) and write a Saver component for your script. It's easier than fiddling with Lua.

Mark each of your classes Serializable, and use variables instead of properties. I know properties are better form, but Unity won't serialize them like they do variables. For example:

Code: Select all

    /// <summary>
    /// Represents the state of the minigame.
    /// </summary>
    [System.Serializable]
    public class MinigameState
    {
        public bool complete;
        public PatientDifficulty currentDifficulty;
        public List<Patient> easy;
        public List<Patient> medium;
        public List<Patient> hard;
    }

    /// <summary>
    /// Represents a patient with an ailment. 
    /// </summary>
    [System.Serializable]
    public class Patient
    {
        public PatientStatus status;
        public PatientWalkerAnimator walkerAnimator;
        public PatientPortraitAnimator portraitAnimator;
        public string ailmentTextId;
        public List<CureIngredientInformation> cureIngredientList;
    }
    
    etc.
Let's assume you have a MonoBehaviour script named MinigameManager that has a MinigameState variable:

Code: Select all

public class MinigameManager : MonoBehaviour
{
    public MinigameState minigameState;
    ...
Then add a script like this to the same GameObject:

Code: Select all

using UnityEngine;
using PixelCrushers;

[RequireComponent(typeof(MinigameManager))]
public class MinigameSaver : Saver
{
    public override string RecordData()
    {
        var data = GetComponent<MinigameManager>().minigameState;
        return SaveSystem.Serialize(data);
    }
    
    public override void ApplyData(string s)
    {
        if (string.IsNullOrEmpty(s)) return;
        var data = SaveSystem.Deserialize<MinigameData>(s);
        if (data != null)
        {
            GetComponent<MinigameManager>().minigameData = data;
        }
    }
}

Re: Storing objects in lua

Posted: Fri Jan 24, 2020 1:35 pm
by VoodooDetective
Oh fantastic, thank you!

Re: Storing objects in lua

Posted: Fri Jan 24, 2020 2:13 pm
by Tony Li
Happy to help!