- Home /
whats the best way to save a large list of variables?
I use UnityScript,
Ive been looking into saving game data for continuing the game where you last left off. I would like to know what the best method is for storing data, on Windows. Im making an RPG/Hack and slash, so i have many variables
(strength, magic, defense, health, chest1 : boolean = false, what items are the in the inventory, etc)
so what is the best method for creating a "Save File" that would keep the variables and load the Saved Values of each variable when i load game?
I know there is Serialization of sorts... but there seems to be many types and i dont know which methods do what... sometimes i cant even tell if the codes are Unityscript or C#.. The other method ive found is Playerprefs, and i cant seem to get that working at all. Lastly i know of XML.. which stores a file that can be edited easily in the harddisk of the computer.
so "What is the best method in UnityScript, for saving data with large amounts of variables and what does each method actually do?"
I just cant get my head around it.
Please and thank you for your time.
Answer by Piflik · Aug 25, 2012 at 11:02 PM
I have a quite 'light' game in terms of data to be saved, so I just wrote a simple script that outputs the needed variables into a text file to save and reads from it to load.
#pragma strict
import System.IO;
static function autoSave() {
var sPath : String = Application.persistentDataPath + "/savegame0.txt";
var sData : StreamWriter = new StreamWriter(sPath);
sData.WriteLine(encrypt(Application.loadedLevel));
sData.WriteLine(encrypt(PlayerStatus.level));
sData.WriteLine(encrypt(PlayerStatus.exp));
sData.WriteLine(encrypt(PlayerStatus.plHealth));
sData.WriteLine(encrypt(PlayerStatus.lives));
sData.Flush();
sData.Close();
PlayerPrefs.SetString("Slot0", Application.loadedLevelName);
}
static function load(slotNmbr) {
var lPath : String = Application.persistentDataPath + "/savegame" + slotNmbr + ".txt";
var lData : StreamReader = new StreamReader(lPath);
var stage = parseInt(lData.ReadLine());
var level = parseInt(lData.ReadLine());
var exp = parseInt(lData.ReadLine());
var plHealth = parseInt(lData.ReadLine());
var lives = parseInt(lData.ReadLine());
lData.Close();
PlayerStatus.level = decrypt(level);
PlayerStatus.expToLevel = PlayerStatus.expToLevelArr[PlayerStatus.level - 1];
PlayerStatus.exp = decrypt(exp);
PlayerStatus.plHealthMax = 10 * PlayerStatus.multArr[PlayerStatus.level-1];
PlayerStatus.plHealth = decrypt(plHealth);
PlayerStatus.lives = decrypt(lives);
Application.LoadLevel(decrypt(stage));
}
I use PlayerPrefs to determine, if a certain save file exists, and what to display in the save/load menu. encrypt() and decrypt() are simple custom functions I wrote, to obscure the actual data in the save files.
This seems very helpful. $$anonymous$$ay i ask a few questions though? If I may..
1) I dont understand what PlayerStatus is. Is that the name of the script your retrieving the values of your variable from?
2) What is sData.Flush(); doing? the API doesnt give much info about Flush();
Im just trying to find out exactly what each line is doing so i can make my own script. Been looking in the Unity API, but i dont think thats in the list.
Yeah. PlayerStatus is the script, and all the variables are static, so I can access them easily.
Flush clears the buffer and writes its contents into the file. Ins$$anonymous$$d of writing each line or character on its own, streamwriter first writes everything to the buffer and then puts it into the file in one go. This increases performance.
@Punkjim420, you'll not find StreamWriter in the Unity docs because it's .NET/$$anonymous$$ono stuff: to get more info about StremWriter, take a look at http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx