Hello.
I am currently trying to learn how to implement a Save and Load function into my game.
A lot of tutorials mention player health, score, etc, but other than a virtual inventory script for door keys, it doesn’t have anything like that.
I really only need a way to save the player’s current position and any triggers / buttons that have been activated. Kind of like when you press “Pause” when play testing your game and then resuming afterwards.
So far I have made a script for a Main Menu, which handles the start of the game and quitting the game.
I searched around some tutorials and Frankenstein’d a code which I THOUGHT would work in some sort of way, but, of course, I am surely missing something, so I need help.
This is currently the code I use for my Main Menu: (There is a Canvas Panel with buttons that have been assigned each of the function, like: “Play”, “Save”, “Load” and “Quit”)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class MenuManager : MonoBehaviour {
public GameObject player;
public GameObject MenuCamera;
public GameObject MenuPanel;
public Text button;
public List<Game> savedGames = new List<Game>();
void Start()
{
player.SetActive(false);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
MenuCamera.SetActive(true);
MenuPanel.SetActive(true);
button.text = "Resume";
player.SetActive(false);
}
}
public void StartGame()
{
MenuCamera.SetActive(false);
MenuPanel.SetActive(false);
player.SetActive(true);
}
public void QuitGame()
{
Application.Quit();
}
public void Save()
{
SaveLoad.savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savedGames.gd");
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
Application.LoadLevel(1);
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/savedGames.gd"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
Application.LoadLevel(1);
}
}
}
And these are some of the additional scripts that the tutorial told me to create:
(Called “Game”)
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Game
{
public static Game current;
public Game()
{
}
}
(Called “Character”)
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Character
{
public string name;
public Character()
{
this.name = "";
}
}
Originally, the “Game” code looked like this:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Game
{
public static Game current;
public Character knight;
public Character rogue;
public Character wizard;
public Game()
{
knight = new Character();
rogue = new Character();
wizard = new Character();
}
}
But I don’t actually need the RPG type classes, as it is not that type of game, so I deleted them. Not sure what else I could turn those into.
Also, the Save and Load functions in the Main Menu script were on their own in a seperate script and marked as “Static”, to be able to access them from everywhere, or something. I thought it might’ve worked if I just simply added them into the already existing Main Menu script.
So anyway, this mish mash of scripts I created doesn’t seem to do anything, so I am not sure what exactly is the issue, as it is not showing any errors either.
Can anyone, please, look into this and explain what I might be missing?