0
\$\begingroup\$

I have a class PlayerStatistics that doesn't sit on a monobehaviour:

public class PlayerStatistics
{
    public int currentHealth;
    public int maxHealth;
    public bool hasWeapon;
    public Collider weapon;
    public Transform weaponChild;
    public Animator weaponAnim;
    public GameObject droppedWeaponObject;
    public DialogueManager DM;
}

and I have PlayerController script that contains those variables shown above as well as a Save and Load function:

    public int currentHealth;
    public int maxHealth = 100;
    public bool hasWeapon;
    public Collider weapon;
    public Transform weaponChild;
    public Animator weaponAnim;
    public GameObject droppedWeaponObject;
    public DialogueManager DM;

    public PlayerStatistics localPlayerData = new PlayerStatistics();
    public void LoadPlayer()
    {
        localPlayerData = GlobalControl.Instance.savedPlayerData;
    }
    public void SavePlayer()
    {
        GlobalControl.Instance.savedPlayerData = localPlayerData;
    }

And a GlobalControl to store values for said variables across scenes:

    public class GlobalControl : MonoBehaviour {
    public static GlobalControl Instance;
    public string savedJson;
    public PlayerStatistics savedPlayerData = new PlayerStatistics();
    void Awake()
    {
        if (Instance == null)
        {
            DontDestroyOnLoad(gameObject);
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }
    }
    private void Update()
    {
        Debug.Log(savedJson);
    }
}

I can get each value by doing:

GlobalControl.Instance.savedPlayerData.<variablehere> = variableNameHere;

but the question is, if I have a lot variables, how do I get all of it at once. I followed a tutorial that thought me this and it was like:

GlobalControl.Instance.savedPlayerData = localPlayerData;
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$
  1. Make PlayerStatistics a struct instead of a class.

    That way, this line: localPlayerData = GlobalControl.Instance.savedPlayerData; will copy the GlobalControl's version of savedPlayerData instead of just taking a reference to it, and likewise create a separate copy when we save the data back to the GlobalControl.

    (This keeps us from accidentally corrupting our saved copy when we modify variables in the local copy)

  2. Make PlayerStatistics [System.Serializable] so that its members show up in the inspector, so you don't lose any editing flexibility.

  3. Delete everything in your PlayerController script that duplicates variables that are already in PlayerStatistics.

    You already have an up-to-date version of that data, so why make extra copying work for yourself? Just read and write straight from localPlayerData.currentHealth and such. localPlayerData is then your live working copy of all of these variables.

\$\endgroup\$
3
  • \$\begingroup\$ One question, can variables in PlayerStatistics have values, default values etc, like when unity have default values but uses set values instead? \$\endgroup\$ Nov 12, 2017 at 15:42
  • \$\begingroup\$ Structs can't have default values, no, but you can set up your GlobalControl script (or whatever script holds the "master" copy of this struct that you're editing) with a Reset() method that auto-populates it to the desired default values when you add or reset the component in the Inspector. \$\endgroup\$
    – DMGregory
    Nov 12, 2017 at 15:45
  • \$\begingroup\$ Thank you for your help! It finally worked! I wish I could buy you pizza if I weren't on the opposite side of the world haha. \$\endgroup\$ Nov 12, 2017 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .