- Home /
How to save data to selected save slot
I make saveGame function and I have 6 slots for saving. How can I say, to which slot should I save data? Should I create variables for each of 6 slots ?
private int currentActiveSlot;
private int latestSaveSlot;
const int _numberOfSlots = 7;
string[] dateTime = new string[_numberOfSlots];
int[] actNumber = new int[_numberOfSlots];
int[] stepNumber = new int[_numberOfSlots];
int[] transformPositionX = new int[_numberOfSlots];
int[] transformPositionY = new int[_numberOfSlots];
int[] transformPositionZ = new int[_numberOfSlots];
int[] transformRotationX = new int[_numberOfSlots];
int[] transformRotationY = new int[_numberOfSlots];
int[] transformRotationZ = new int[_numberOfSlots];
int[] task1Completed = new int[_numberOfSlots];
int[] task2Completed = new int[_numberOfSlots];
int[] task3Completed = new int[_numberOfSlots];
int[] task4Completed = new int[_numberOfSlots];
int[] task5Completed = new int[_numberOfSlots];
void Start()
{
latestSaveSlot = PlayerPrefs.GetInt("latestSaveSlot");
}
public void ButtonSave()
{
// How to say, to which slot should I save?
latestSaveSlot = currentActiveSlot;
PlayerPrefs.SetString("date time", "");
PlayerPrefs.SetInt("act number", 0);
PlayerPrefs.SetInt("step number", 0);
//..
PlayerPrefs.SetInt("latestSaveSlot", latestSaveSlot);
PlayerPrefs.SetString("sceneName", SceneManager.GetActiveScene().name);
PlayerPrefs.Save();
dateTime[latestSaveSlot] = PlayerPrefs.GetString("date time");
}
public void ButtonLoad()
{
dateTime[currentActiveSlot] = PlayerPrefs.GetString("date time");
actNumber[currentActiveSlot] = PlayerPrefs.GetInt("act number");
stepNumber[currentActiveSlot] = PlayerPrefs.GetInt("step number");
//..
}
I'd say, use an array with a Length of 6. This way, you only need one variable, and you can easily select one element from it by index.
Edited question. Now I use array, but still don't understand, how to say, to which slot should I save with PlayerPrefs.Set
?
Answer by danivdwerf · Oct 18, 2016 at 07:57 AM
I think you should just make a script which will create a file where it'll save your settings and the name/location can be changed depending on which button you will click
But what the differene? I can save it to file and can save with PlayerPrefs. I just don't want to create 6 variables for each parameter for each slot. I can, but lookig for more beautiful solution
$$anonymous$$ore code does not mean less beautiful, check out the terms OOP and SRP it will do good.
Ofcourse Unity's build in Functions are handy, but You can create a game with so little effort and $$anonymous$$imum coding. Also, with a save file YOU control everything ins$$anonymous$$d of unity.
Sorry, but I need example of code, which will help with my problem
Answer by IgorAherne · Oct 18, 2016 at 10:36 AM
There is a post on habrahabr.ru, which is describing your case exactly. They are using Serialization into XML, but it's all in russian.
You can use google translate nonetheless, here is the link https://habrahabr.ru/post/163071/
Я знаю, как сохранять в файл. Вопрос о том, как засунуть PlayerPrefs в массив