I need an easy solution for a save and load system, nothing to fancy.
All I want is to have 5 save/load files that store some PlayerPrefs and load them when you start the load scene.
I was thinking to create 5 empty xml files and when the player hit the save button to put everything in that xml and then when you load the game to read those xml, and then initialize those prefs to the value from the xml, but I’m not sure if it’s ok to do like this.
What do you guys think? Do you know an easier solution?
May I ask why you are against just using PlayerPrefs on its own?
What are the variables you want to save/load?
Uhm… To be honest I’m not against but I don’t know how to use them on its own.
The game it’s like a classic stat game. I want to store like:
Time, Money, Day, Choices, Skills Level, Stats Level… stuff like that
I would suggest using them then. Only because I don’t feel you’ve got too much / too complicated data that needs saving and loading.
They’re really easy to use.
PlayerPrefs.SetInt("Player Score", myScoreVariable);
You can set ints, floats and strings and they’re all done the same way as above.
PlayerPrefs.SetFloat("Some Float", myFloatVariable);
PlayerPrefs.SetString("Some String", myStringVariable);
…and when you are done setting your values make sure you save!
PlayerPrefs.Save()
To get the values back, you use their getters and assign them to a variable.
string playerName = PlayerPrefs.GetString("Player Name");
Really simple and easy to use once you grasp it.
I’d suggest taking a look here if anything is still unclear.
This I know… But my problem is that I need 5 differents saves. With different palyerPres.
For example save1 has money - 100 and hour - 5, But save two has money - 300 and hour 6.
How can I load different PlayerPrefs.Saves ?
How about adopting a naming convention for your saved variables? …and then dynamically looking for the saved preferences that match the name criteria?
You mean like :
PlayerPrefs.SetInt("S1Money",300);
PlayerPrefs.SetInt("S2Money",600);
if you are going to use playerprefs, you can have the player make a username. Then have the username added in front of the player pref key.
private string username;
PlayerPrefs.SetFloat(username + "StatName", /*statAmt*/);
Edit:
realised it was a different issue, although the logic can still be applied. change username for savenum
ok… but the problem is that I want my same player to have like 5 saves. So he can choose what save to load. For example he made a poor choice some time ago. He may want to load the save1 or save3
Try something like this.
public static class FileManager {
public enum SaveFile {
FILE_ONE,
FILE_TWO,
FILE_THREE,
FILE_FOUR,
FILE_FIVE
}
//Default save file
static SaveFile currentFile = SaveFile.FILE_ONE;
public static void SaveScore(int score){
PlayerPrefs.SetInt(currentFile.ToString() + "_SCORE", score);
PlayerPrefs.Save();
}
public static int LoadScore(){
int score = PlayerPrefs.GetInt(currentFile.ToString() + "_SCORE");
return score;
}
public static void ChangeFile(SaveFile file){
currentFile = file;
}
}
I think this may be just what I need. Thank you very much!
Best regrads
I prefere to use binary files for things like this myself as playerprefs is easy for any user to modify outside the program. Binary isn’t 100% secure but it would take more than a novice user to alter plus you can save a class in one function and simply add to the class later if you like without having to add new functions.
I create a LoadSaveManager.cs file like this;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class LoadSaveManager
{
private const string SaveFile = "Settings";
public static void Save(List<PlayerSettings> settings)
{
var bf = new BinaryFormatter();
var file = File.Create(Application.persistentDataPath + "/" + SaveFile + ".bin");
bf.Serialize(file, settings);
file.Close();
}
public static List<PlayerSettings> Load()
{
if (!File.Exists(Application.persistentDataPath + "/" + SaveFile + ".bin"))
{
Debug.LogError("No Save File Found");
return null;
}
try
{
var bf = new BinaryFormatter();
var file = File.Open(Application.persistentDataPath + "/" + SaveFile + ".bin", FileMode.Open);
List<PlayerSettings> loadedSettings = (List<PlayerSettings>)bf.Deserialize(file);
file.Close();
return loadedSettings;
}
catch
{
Debug.LogError("Error Loading SaveFile");
return null;
}
}
}
[Serializable]
//Add Variables to this class for your player stats
public class PlayerSettings
{
public string PlayerName;
public float Time = 0f;
public float Money = 0f;
public string Day = "Today";
}
Example of usage in say your game manager
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public List<PlayerSettings> settings;
private int _currentPlayer = 0;
private void Start()
{
if (LoadSaveManager.Load() == null)
{
settings.Add(AddPlayer());
}
else
{
settings = LoadSaveManager.Load();
}
}
private PlayerSettings AddPlayer(string player = "New Player")
{
var ret = new PlayerSettings
{
PlayerName = player
};
return ret;
}
void Update()
{
//Assign Values to current player
if (Input.GetKeyDown(KeyCode.A))
{
settings[_currentPlayer].PlayerName = "Player One";
settings[_currentPlayer].Day = "Monday";
settings[_currentPlayer].Money = 50f;
settings[_currentPlayer].Time = 10f;
}
//Save Settings
if (Input.GetKeyDown(KeyCode.S))
{
LoadSaveManager.Save(settings);
}
//Add new Player
if (Input.GetKeyDown(KeyCode.Q))
{
settings.Add(AddPlayer());
}
//Load Settings
if (Input.GetKeyDown(KeyCode.L))
{
settings = LoadSaveManager.Load();
}
//Display current Settings
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Day " + settings[_currentPlayer].Day + " Money " + settings[_currentPlayer].Money + " Time " + settings[_currentPlayer].Time);
}
}
}
I like this solution for the fact that it’s hard to modify but I’m a novice in programming so I don’t get the hole picture of this so I have a few questions to see if I get it right.
what is _currentPlayer?
can I use this solution to create multiple save files?
how can I get the number of the binary save files that I have?
Also I have a deadline for version 0.0.2 soon and I don’t think I have enough time to change the whole game to use this method because I already used the PlayerPrefs stuff… But for version 0.0.3 I will change to your method because it looks quite clean and easy.
_currentPlayer is the index value of your saved player. It is zero based so your first player would be at 0.
You don’t need multiple files, because it uses a list you can store as many players data as you like in a single file.
Here’s an example of how to use it for 5 players. Just attact this script to an empty gameobject and make sure the settings script is saved somewhere in the project.
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public List<PlayerSettings> settings;
private int _currentPlayer = 0;
private void Start()
{
if (LoadSaveManager.Load() == null)
{
settings.Add(AddPlayer());
}
else
{
settings = LoadSaveManager.Load();
}
}
private PlayerSettings AddPlayer(string player = "New Player")
{
var ret = new PlayerSettings
{
PlayerName = player
};
return ret;
}
void Update()
{
//Create Players
if (Input.GetKeyDown(KeyCode.A))
{
CreateFivePlayers();
}
//Save Settings
if (Input.GetKeyDown(KeyCode.S))
{
LoadSaveManager.Save(settings);
}
//Load Settings
if (Input.GetKeyDown(KeyCode.L))
{
settings = LoadSaveManager.Load();
}
//Change current player using the 1 - 5 keys
for(int i = 1; i < 6; i++)
if(Input.GetKeyDown(i.ToString()))
{
_currentPlayer = i - 1;
}
}
private void CreateFivePlayers()
{
settings.Clear(); //Empty current list
for (int i = 0; i < 5; i++)
{
PlayerSettings newPlayer = new PlayerSettings()
{
PlayerName = "Player " + (i + 1).ToString(),
Day = "Day " + (i + 1).ToString(),
Time = 10 * i,
Money = 100 * i
};
settings.Add(newPlayer); //Add created player to the list
}
}
private void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 50), settings[_currentPlayer].PlayerName);
GUI.Label(new Rect(10, 50, 100, 50), settings[_currentPlayer].Day);
GUI.Label(new Rect(10, 90, 100, 50), settings[_currentPlayer].Time.ToString());
GUI.Label(new Rect(10, 130, 100, 50), settings[_currentPlayer].Money.ToString());
}
}
Thank you very much. I will try this first thing in the morning.
Best regards