- Home /
Strategy for saving game
Hello, looking for some advice on a good way to save data between scenes.
What needs to be saved: (Other stuff as well, but most concerned about these):
Booleans (doesn't necessarily need to be in Array)
Weapon Objects (preferably a List or Array, See below)
using UnityEngine;
using System.Collections;
public class Weapon
{
// INSTANCE FIELDS
private string weaponType;
private string weaponName;
private Texture weaponImage;
private AudioClip weaponSound;
private float damage;
private int clipSize;
private float range;
private float rateOfFire;
private int currentClip;
private float reloadTime;
private bool isAutomatic;
// CONSTRUCTOR
public Weapon(string weaponTypeIn, string weaponNameIn, Texture weaponImageIn, AudioClip weaponSoundIn, float damageIn, int clipSizeIn, float rangeIn, float rateOfFireIn, float reloadTimeIn, bool isAutomaticIn)
{
weaponType = weaponTypeIn;
weaponName = weaponNameIn;
weaponImage = weaponImageIn;
weaponSound = weaponSoundIn;
damage = damageIn;
clipSize = clipSizeIn;
range = rangeIn;
rateOfFire = rateOfFireIn;
reloadTime = reloadTimeIn;
isAutomatic = isAutomaticIn;
currentClip = clipSize;
}
}
Game Structure
2 Primary Scenes : Scene A - > Scene B - > Scene A
The Current Plan
Have a dontdestroy gameobject called tempSave in scene A store all the changes. These changes would affect the game, but wouldn't be permanently stored on the HD. This object would persist until, when in Scene B, the user chooses to return to Scene A. Then all the information would be serialised, and stored on the HD. When Scene A begins, the data would be retreived and a new tempSave object would be created and populated with the info. Does this approach make sense? Are there any problems with it?
Additional Question
I understand there are 2 options for serialising data in unity, one using XML, the other a mono class. Which would be best for my purposes?
Sorry for the long post, and thanks for reading and your help!
use PlayerPrefs ins$$anonymous$$d of DontDestroy. Also, do a search for PlayerPrefsX to save to playerprefs as arrays.
Hello, thanks for the help. Had a quick look at player prefs x. Not sure if it will work, cause I need to store arrays of custom objects. Still trying to get my head around saving, the most I've done in this line so far is writing to text files in java.
Answer by jogo13 · Jan 02, 2013 at 10:51 PM
I've used this and it works great: http://pixelplacement.com/2012/03/21/simple-state-saving/
Keep in mind XML is verbose(BLOATED) and saving many variables in serialized XML seems like it could be a bit slow.
I also use this rewrite of player prefs for faster save/load on portable devices: http://www.previewlabs.com/writing-playerprefs-fast/
Thanks. I was looking at Unity Serializer also. Do you think it makes sense to store all the data needed across scenes in a single dontdestroy gameobject, and load it back into the individual objects when necesary, as opposed to having multiple don't destroyOnLoad object?