0

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

enter image description here

 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];

 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.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");
     //..
 }
Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • 1
    This has been answered many times. You don't need variable for each slot. Create a simple class that holds those variables then use json and PlayerPrefs to save it. See [here](http://stackoverflow.com/a/40097623/3785314) for a full example. – Programmer Oct 19 '16 at 08:21

2 Answers2

1

If I understood correctly, then I would do the following:

Each Save slot would have the "SavedOn(DateTime)", "SavedById (int-> user that did the action of saving), "".

Then, when you want to make a new saving, I would search first for the slot that is "null" (order by id asc), if there is none, I would display the oldest one and ask the user if he wants to rewrite it (by losing the old data and saving the new one).

Is this what you are looking for?

Dryadwoods
  • 2,875
  • 5
  • 42
  • 72
1

The most basic approach (not the best one, I know) would be to add the slot's index directly into the PlayerPrefs' key. You just have 6 of them, so the number of keys will remain acceptable. Something like this:

 public void ButtonSave()
 {
     // How to say, to which slot should I save?
     latestSaveSlot = currentActiveSlot;
     PlayerPrefs.SetString("date time" + latestSaveSlot, "");
     //...

And, similarly for the loading phase.

 public void ButtonLoad()
 {
     dateTime[currentActiveSlot]  = PlayerPrefs.GetString("date time" + currentActiveSlot);
     //...
Andrea
  • 6,032
  • 2
  • 28
  • 55