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

Save and Load to an external file?

Discussion in 'Scripting' started by Marscaleb, May 28, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    Okay, I'm at the point now where I am ready to get my game to create actual save files that save the game between play sessions.
    I have my data all set up and know what variables I need to save (most complex one is a List) but I don't know how to actually write this to an external file, and to read it again to restore it later.

    So... How do I write data to an external file? How do I read data from an external file?

    I'm also curious how I am supposed to handle this among different platforms. I'm sure PC can easily just create a file, but I'm not 100% that it would be so simple with android/mobile devices. I'm also curious about how I save files on a major console, but I suppose I'm not ready for that yet anyway.

    I searched the documentation, but keywords like "save," "load," and "file" turn up a lot of other results that I'm not looking for.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You can use Application.persistantDataPath to get a path to a folder to save files in.
    You will need to use System.IO of .net to read and write files.
    before saving, you will need to serialize your data. after reading you will need to deserialize it.
     
  3. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    ThermalFusion likes this.
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    @NomadKing
    That was a very helpful tutorial!

    After watching it, I am curious about adapting it to my needs.

    First of all, I already had a special class just to handle all my records. Mostly it serves as a collection of public variables that anyone can read or write to. In that tutorial, he basically created the same thing, except that it was NOT a MonoBehavior script. Also, he only bothered with that class's existence when he was actually saving and loading, whereas I keep my records class around for the whole game; in the tutorial he copied the data from the data class into the needed variables in his GameControl class, but I just use the variables in my records class directly.

    He said that there are problems when you serialize a monobehavior class, but didn't say much about what they were or how to avoid them, so I don't know if it would be wiser to change my records class to not be monobehavior anymore.

    Is there any kind of problem with creating a non-monobehavior class and carrying it around with me through a game? I mean, I don't want to have it picked up in the garbage collection or something like that. It it isn't monobehavior, it won't be visible as a component on my Game Manager's GameObject, will it? I don't even know how to properly destroy it when I load a different save file. Also my records class needed to initialize all of my lists in the Start function, and will that function even be called if the class isn't monobehavior?

    But I know even less about how I would need to properly serialize the class if I kept it as a monobehavior. All I know is that he said there are problems if I try to do it that way. And he talked about a serialization method within Unity, but it is "more complex" and "more powerful" than what I need.
    I've never even heard of serialization before today.
     
  5. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    There are some very useful gems hidden away in the live training archives :)

    As for the topic, I haven't really delved into external files beyond a small test based on that video I linked, so I'm going to be zero help with most of these questions. Sorry!
     
  6. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    I don't get what serialization is either, even after going through a bunch of articles/tutorials the last time. :confused: I ended up buying Easy Save 2 from the asset store. Works great, but I've only tested it on PC and Android so far.

    You can check out the docs to see if it fits your needs.
     
  7. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    Also when I added "using System" to my class it gave me an error because now "random" was ambiguous.
     
  8. Deathpunkin

    Deathpunkin

    Joined:
    Jun 15, 2018
    Posts:
    2
    I know this post is from 2015, and it's not 2022, but seeing as this is the first result in google...
    figure I'd give an explanation/solution to the problem quoted.

    This is because both
    UnityEngine
    and
    System
    have "
    Random
    " functions.
    If you were using
    Random
    before, just change it to
    UnityEngine.Random
    , and it'll work just like it was.