Search Unity

Tips on game level saving

Discussion in 'Scripting' started by BS-er, Jul 15, 2009.

  1. BS-er

    BS-er

    Joined:
    May 24, 2009
    Posts:
    115
    Unity obviously doesn't have built-in high-level functionality for saving/loading a game level. I would appreciate some pointers on getting it done though.

    Example: I have a game world full of prefabs that each have a javascript file (let's call it SpaceShip.js) with variables as follows:

    private var velocity : Vector3;
    private var otherScript : MonoBehaviour;

    For my game save purposes, I only care to save those variables, and the tranform variables of the game object to which it is attached.

    I'm quite interested in how one might save that MonoBehavior variable (as a reference, not a deep copy), and restore it in a saved game, such that it's referencing the same MonoBehavior instance that it was when saved (assume under the same game object as this script).

    Even if the knowledge is contained throughout the docs, I'm having a hard time getting it.

    I seem to pose some real stumpers in general (likely pushing beyond Unity's limitations), so I'll be a bit surprised if there are easy answers.
     
  2. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    62
    As far as I can tell, you would have to create a script that will traverse all the items in the scene, saving their state in some sort of well specified/documented format (be it xml or binary or whatever). For things like transforms and the like this could easily be a generic class that would potentially be useful for others too.

    But when it comes to custom scripts and saving their state you will have to write something specifically for those scripts.

    Of course, with any saver you will have to write a loader, that will re-build the scene by adding the objects/prefabs to the scene via code and place in your variables.

    Ultimately, you will need to design your file format very carefully especially if you plan to throw in new stuff at a later date and wish to save that too.

    One potential method is to have all of your scripts have a SaveState() and LoadState() method in them if they're attached to objects that need save/load. And in your saver function, as it traverses these items and encounters the scripts (you will need to make it look for these scripts) you can simply call SaveState() and it will return a string/data for your saver to work with.

    One method of cycling all your scripts would be to structure your entire scene inside one single gameobject ("the scene") and use getcomponentsinchildren to get all of said components from the objects

    http://unity3d.com/support/documentation/ScriptReference/Component.GetComponentsInChildren.html

    Here you could find all your SaverScript.js/cs scripts in the scene which could return to you relevant info about the object in which you're trying to save (although, it can get very complicated if you're talking about things like object scene hierarchy or things like the combinechildren script). Just an idea...

    I've never tried it myself, but something like that is what I'dd probably attempt if I were to save out entire scenes including variable states e.t.c.

    Sounds like a big project though and others might have better ideas about how to go about it than me, but it should work.

    Ultimately, before you even build your scene you'll need to think about how you're going to structure it, how it's going to be saved and how it's going to be loaded. Sadly there is no easy 'save/load' state in Unity.

    Good luck.
     
  3. Armagon

    Armagon

    Joined:
    May 29, 2009
    Posts:
    246
    Gee. We really need a built-in solution for Saving and Loading games. :x
     
  4. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    663
    Yea, creating something like this is a pretty detailed project, maybe someone suggested it for Unity's Summer of Code :).
     
  5. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    Yes. I was also going to suggest that someone propose that for the SoC project.