- Home /
Saving and loading seperate data
I was wondering how to go about saving to separate text files depending on the save box that the User selects. My load menu is in a separate scene from the levels so I'm unsure how to tell if I select game1 to load game1 variables when the separate scene loads. I'm planning on having up to 3 different save files.
Also what's the simpilist way to save and load basic variables such as.
var randomData : int = 5;
Answer by Piflik · Jul 10, 2012 at 07:42 PM
I create simple text files to save my games (after doing some magic to the variables, so it is not too easy to manipulate them) and then store in PlayerPrefs if a certain save file is existing (and the name of the level the player is in).
import System.IO;
static function autoSave(slotNmbr) {
var sPath : String = Application.persistentDataPath + "/savegame" + slotNmbr + ".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.WriteLine(encrypt(PlayerStatus.maxRange));
sData.WriteLine(encrypt(PlayerStatus.maxMelee));
sData.WriteLine(encrypt(PlayerStatus.range));
sData.WriteLine(encrypt(PlayerStatus.melee));
sData.WriteLine(encrypt(PlayerStatus.pick[0]));
sData.WriteLine(encrypt(PlayerStatus.pick[1]));
sData.WriteLine(encrypt(PlayerStatus.pick[2]));
sData.WriteLine(encrypt(PlayerStatus.pick[3]));
sData.WriteLine(encrypt(parity(Application.loadedLevel, PlayerStatus.level, PlayerStatus.exp, PlayerStatus.plHealth, PlayerStatus.lives, PlayerStatus.maxRange, PlayerStatus.maxMelee, PlayerStatus.range, PlayerStatus.melee, PlayerStatus.pick[0], PlayerStatus.pick[1], PlayerStatus.pick[2], PlayerStatus.pick[3])));
sData.Flush();
sData.Close();
PlayerPrefs.SetString("Slot" + slotNmbr, 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());
var maxRange = parseInt(lData.ReadLine());
var maxMelee = parseInt(lData.ReadLine());
var range = parseInt(lData.ReadLine());
var melee = parseInt(lData.ReadLine());
var shields = parseInt(lData.ReadLine());
var mines = parseInt(lData.ReadLine());
var bombs = parseInt(lData.ReadLine());
var turbo = parseInt(lData.ReadLine());
var parTest = parseInt(lData.ReadLine());
lData.Close();
if(decrypt(parTest) == parity(decrypt(stage), decrypt(level), decrypt(exp), decrypt(plHealth), decrypt(lives), decrypt(maxRange), decrypt(maxMelee), decrypt(range), decrypt(melee), decrypt(shields), decrypt(mines), decrypt(bombs), decrypt(turbo))) {
GameObject.FindWithTag("Player").SendMessage("freeWP");
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);
PlayerStatus.maxRange = decrypt(maxRange);
PlayerStatus.maxMelee = decrypt(maxMelee);
PlayerStatus.range = decrypt(range);
PlayerStatus.melee = decrypt(melee);
PlayerStatus.pick[0] = decrypt(shields);
PlayerStatus.pick[1] = decrypt(mines);
PlayerStatus.pick[2] = decrypt(bombs);
PlayerStatus.pick[3] = decrypt(turbo);
MainMenu.gameOver = false;
statics.turn = 0;
Application.LoadLevel(decrypt(stage));
}
}
I only allow to save at the beginning of a level.
Is it just as simple to load up all the variables when the game starts?
Answer by delstrega · Jul 10, 2012 at 07:29 PM
The easiest way to save data I guess is using PlayerPrefs. The wiki has a powerful script that allows for easily saving and loading arrays:
http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2
Your answer

Follow this Question
Related Questions
saving class array states 1 Answer
Saving a game for the player 2 Answers
Accessing Functions from other Scripts 1 Answer
saving volume between scenes 2 Answers
[Please Help!]How do I make a player save their position when I pause the game? 3 Answers