How to SAVE to user defined file Name, and then Load any other saved game by user Selection
Sorry for asking this, however I've looked at a dozen or more ave/Load examples.
I'm new to Unity, and I have not seen a single example that
A) works and or provides sample script that works without errors.
B) none actually show working example of prompting the user for a NAME of the saved game, OR ask the player what game to load.
The closest I've fount is the script below, I know it is saving the game, however I can't tell if it is actually loading the name.
How do I get the script to ask the player for saved game name to save, or load?
Thanks!
My guess is that I'm missing something really simple (stupid) that is preventing me from assigning a name to the saved game, and then recalling it at the load game function.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class Game
{
public static Game current;
public CharacterInfo knight;
public CharacterInfo rogue;
public CharacterInfo wizard;
public Game()
{
knight = new CharacterInfo();
rogue = new CharacterInfo();
wizard = new CharacterInfo();
}
public static class SaveLoad
{
public static List<Game> savedGames = new List<Game>();
//it's static so we can call it from anywhere
public static void Save()
{
SaveLoad.savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.Create(Application.persistentDataPath + savedGames); //you can call it anything you want
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
Debug.Log("Game Saved");
}
public static void Load()
{
if (File.Exists(Application.persistentDataPath + savedGames))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + savedGames, FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
Debug.Log ("Game Opened");
}
}
}
I'm just putting this out there, although you may know it, that a lot of times games will actually pull up the save data as a folder in a load section. Now if you do understand this, I am curious why you don't want to do that, or is that what you're trying to do?
Oh, okay now I understand. Have you tried setting a text area event in the GUI of the save?
Answer by mfarrell806 · Dec 12, 2016 at 05:50 PM
What I would ideally like to have happen....
When user clicks SAVE button, I want them to have a chance to give the saved game a NAME. The above posted code, does save the gave, however no user defined name.
The load button, as far as I can tell doesn't work because, it never asks for a file name to load, so how will it ever load the game the user saved?
So I want the button (script) to Prompt for a name to Save As... In the default save folder is fine.
And the Load Button to as the user which game to load.
It really should NOT e this difficult to get this information for such a widely popular game engine. Or does no one ever save their games with a name? (rhetorical question, because I'm pretty simple that way)
The code I posted is exactly what I have.
I've tried some other save/load codes as well that either produce too many errors on compile, or just do not work at all.
I am new to all of this, so any code that isn't well commented, is mostly useless. And even when well written the pieces I've found do not appear to work, and there are no clear examples of this in the Unity samples provided. Almost as if no one, ever actually saves a game in any manner that is useful.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102556im_/https://answers.unity.com/themes/thub/images/avi.jpg)