(solved) does anyone see a problem with my method?

The following is the code I will be using to save character data in my game. I’ve never used this before, or needed to save ingame information at all. So its very new to me.

As I understand it, this is a simple way of saving information, without much fuss, and avoiding saving to player prefs (which I dont want). The Script is added to a game object which is always present no matter what scene(to easily reference) . Im working with 5 scenes, always starting at the same scene.(why I have a “dont destroy” and a singelton(ish) design just in case).

but for the actual save and load code, I don’t know if this will work correctly. I followed a tutorial on saving/loading to unity persistantpath but there were some comments saying that the method is flawed.

Please tell me anything you see as potentially problematic or just wrong. I’m here to learn.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class CharacterInfo : MonoBehaviour
{
    public static CharacterInfo character;
    public string characterName;


    void Awake ()
    {
        if (character == null)
        {
            DontDestroyOnLoad(gameObject);
            character = this;
        }
        if (character != this)
        {
            Destroy(gameObject);
        }
       
    }

    public void SaveCharacter()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + characterName);
        CharacterSaveData saveData = new CharacterSaveData();

        //saveData.Stats = This.stats
        saveData.hCharacterName = characterName;


        bf.Serialize(file, saveData);
        file.Close();
    }

    public void LoadCharacter()
    {
        if (File.Exists(Application.persistentDataPath + characterName))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + characterName, FileMode.Open);
            CharacterSaveData saveData = (CharacterSaveData)bf.Deserialize(file);
            file.Close();

            //This.stats = saveData.stats
            characterName = saveData.hCharacterName;

        }
    }


}

[Serializable]
class CharacterSaveData
{
    public string hCharacterName;
    //stats, stuff

}

My main concern is defiantly the SaveCharacter() function. Namely that the file is created, then information is changed. which seems weird to me. shouldnt that line be at the end? or at least after local data is copied to data to be saved?

i tested this out, and i can save and load information correctly. just need to know if anyone sees any FUTURE problems that might occur

Does it have to be a BinaryFormatter? You can make your live a lot easier by just serializing to Json. Added benefit being that you can manually edit savedata when needed.

2 Likes

/\ this.
plus look at JsonUtility class

1 Like

quite honestly, I don’t know. I would like to have some semblance of security so that players can’t just change their stats to what they fancy or anything like that, and binary was suggested to me as the “simplest option that doesn’t require more explanation”

as a question of interest, why exactly do you mean change manually? Like open up the save file like a txt and edit it however you want? how would that be different from playerPrefs, which I am trying to avoid.?

oh so the utility lets you load information directly form the saved files without actually bringing up the whole file? am I reading that right?

You need both file reading / saving methods and JsonUltility.

For example
You generate Json string from class instance and then save to file. You can encode if you like.
In reverse
Then read file to string (decode if needed) and use Json to convert string to class.

1 Like

exactly, you can use json and for release builds you could build in the option to encrypt/decrypt the file‘s contents using standard .net encryption methods. Benefit: taken an encrypted savgame you and only you will still be able to decrypt and debug it. Everyone else will have it much harder to crack into your savegame compared to binary which for some people is essentially a second language.

Well, the encryption key will most likely be in your app though but finding that and applying it correctly is still harder than fiddling with binary formats.

1 Like

Since nobody answered:

Yeah, that’s strange. It doesn’t really matter, but I’d definitely do it in the order:

  • create save data
  • create/open file
  • write to file
  • close file

Though if you go with JSON, you just use File.WriteAllText, and skip the opening and closing of the file, as that happens as a part of that method.

1 Like

thankyou all for the suggestion, I’ll look into Json. and reorder the code see if anything changes. Thread marked solved.