"named" Save slots

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

"named" Save slots

Post by annathehank »

Hello again,

Today I have what is, hopefully, a quick question. I was thinking of incorporating multiple save slots into my game (probably 3 at the most) and was curious if there's a way to create 'named' save slots through the dialogue manager. Such that, at the start of the game, the conversation uses the user input to get the player's name and then would display that as the button text when clicking to save or load a file.

Is there a way to make a variable in the manager static, in a sense? Like a save file 1 variable string that fills in from the input that would display outside of that game's run? (as a small aside, can we use Lua coding in UI elements? Like a button text that says [var=whatever]?) Or am I looking at some non-dialogue manager scripting to get it to work?

Thank you very much for all your help and support <3 I would probably not be able to do this (or at least not well) without you and your system!
User avatar
Tony Li
Posts: 22037
Joined: Thu Jul 18, 2013 1:27 pm

Re: "named" Save slots

Post by Tony Li »

Hi,

You'll have to manage the slot's "name" yourself.

If you're using PlayerPrefsSavedGameDataStorer, you could store additional PlayerPrefs keys for the 3 slots:

Code: Select all

SaveSystem.SaveToSlot(1);
PlayerPrefs.SetString("Slot1Name", "Anna");
If you're using DiskSavedGameDataStorer, you could store a separate file that contains the names for the 3 slots:

Code: Select all

List<string> slotNames;
...
slotNames[0] = "Anna";
...
SaveSystem.SaveToSlot(1);
System.IO.File.WriteAllLines("SlotNames.txt", slotNames);
Alternatively, you could store the slot name inside the saved game file itself by using a custom saver script:

Code: Select all

public class SlotNameSaver : Saver
{
    public string slotName;
    
    public override string RecordData() { return slotName; }
    public override void ApplyData(string s) { slotName = s; }
}
The catch is that you'll need to retrieve the data for all 3 slots in order to get at their SlotNameSaver data.

I can provide more details on any of the 3 approaches above if you have questions.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: "named" Save slots

Post by annathehank »

If I'm using the playerpref save method, how would I get the button UI text to display the string it's set to? And would I still use the PlayerPrefs.SetString("Slot1Name", "Anna"); in the conversation node script and just replace the "Anna" with "Var=[playername]" the variable of which would be set when the player types in their name?
User avatar
Tony Li
Posts: 22037
Joined: Thu Jul 18, 2013 1:27 pm

Re: "named" Save slots

Post by Tony Li »

Hi,

Let's say you have an array of 3 UI Buttons. You can set up a "select slot" screen using code similar to this:
(Just an example.)

Code: Select all

int currentSlot;

Button[] loadGameButtons; // Assign 3 UI buttons in inspector.
 const int TotalSlots = 3;
 
 void SetupSelectSlotButtons()
 {
     for (int i = 0; i < TotalSlots; i++)
     {
         int slotNum = i;
         Text buttonText = loadGameButtons[i].GetComponentInChildren<Text>();
         bool hasGameInSlot = SaveSystem.HasSavedGameInSlot(slotNum);
         if (hasGameInSlot)
         {
             // If there's already a game saved in a slot, set up the button to load that game:
             buttonText.text = PlayerPrefs.GetString($"Slot{slotNum}Name");
             loadGameButtons[i].onClick.RemoveAllListeners();
             loadGameButtons[i].onClick.AddListener(() => 
             {
                 currentSlot = slotNum; // Record that we've loaded this slot.
                 SaveSystem.LoadFromSlot(slotNum);
             });
         }
         else
         {
             // Otherwise set up the button to start a new game:
             buttonText.text = "New Game";
             loadGameButtons[i].onClick.RemoveAllListeners();
             loadGameButtons[i].onClick.AddListener(() => 
             {
                 currentSlot = slotNum;
                 SaveSystem.RestartGame("First Gameplay Scene"); // We'll assume Variable["playername"] gets set in the first gameplay scene.
             });
         }
     }
 }
When saving the game: (again, just a rough example)

Code: Select all

void SaveGame()
{
    SaveSystem.SaveToSlot(currentSlot);
    string playerName = DialogueLua.GetVariable("playername").asString;
    PlayerPrefs.SetString($"Slot{slotNum}Name", playerName);
}
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: "named" Save slots

Post by annathehank »

ahhhh I see thank you thank you!!! <3
User avatar
Tony Li
Posts: 22037
Joined: Thu Jul 18, 2013 1:27 pm

Re: "named" Save slots

Post by Tony Li »

You're welcome!
Post Reply