- Home /
Inventory that works across scenes?
I'm pretty new at the C# game but I'd like to ask what I think is a tough question.
How can I make my inventory system work across different scenes?
I learned about PlayerPrefs but I'm not sure this would be the best way to accomplish this. A nudge in the right direction or some concepts I don't know about yet would be much appreciated!
Answer by melsy · Feb 02, 2018 at 12:48 AM
Im Editing the answer to include this sample save system.
put this class on a simple game object in the scene to get a visual of how it works.
public class TestingTheSaveData : MonoBehaviour
{
private void Awake()
{
// Load the vars and print them in the conosle
LoadVarsAndDebug();
// Change them and save them.
TestChageTheVars(5);
LoadVarsAndDebug();
TestChageTheVars(10);
}
void LoadVarsAndDebug()
{
SaveData.Load();
Debug.Log(SaveData.saveVars.taco);
Debug.Log(SaveData.saveVars.drink);
}
void TestChageTheVars(int value)
{
// This will change taco by value
SaveData.saveVars.taco += value;
SaveData.saveVars.drink += value / 10;
// This will save the class data.
SaveData.Save();
}
}
Then create a new script with these classes in there
[System.Serializable]
public class SaveData
{
// Creates a public reference that can be used to set the data
public static SaveVariables saveVars = new SaveVariables();
// Get the path to the save file
public static string GetPath()
{
return Application.persistentDataPath + saveVars.directory;
}
// Return the filename of the save file.
public static string GetFileName()
{
return saveVars.playerName + saveVars.extention;
}
// Call this once before anything else is done.
// This will create the new file and make sure the directory is correct.
public static void RunThisOnceToBuildTheInitailFile()
{
var path = GetPath();
var fileName = GetFileName();
var fullpath = (Path.Combine(path, fileName));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File.Exists(fullpath))
{
Debug.Log("File named " + fullpath + " already exists");
return;
}
else Debug.Log(fullpath);
// If the player has never played before write the defaults to the save data and
// then save the state so the game will start fresh
Save();
}
// Save the data.
public static void Save()
{
var path = GetPath();
var fileName = GetFileName();
var fullpath = (Path.Combine(path, fileName));
BinaryFormatter bf = new BinaryFormatter();
// Save the written variabes to the disk.
using (var file = File.Create(fullpath))
{
// Take the SaveVariables class and write the variables to disk.
bf.Serialize(file, saveVars);
}
Debug.Log("Data Saved in Directory" + fullpath);
}
// Load the data.
public static void Load()
{
var path = GetPath();
var fileName = GetFileName();
var fullpath = (Path.Combine(path, fileName));
// Load the file and write the data to the class vars.
if (File.Exists(fullpath))
{
FileStream inStr = new FileStream(fullpath, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
// Load the file as SaveData class
var data = bf.Deserialize(inStr) as SaveVariables;
// write the saved data to the saveVars class reference
saveVars = data;
Debug.Log("Data Loaded From Directory" + fullpath);
inStr.Close();
}
else
Debug.Log(fullpath + "Doesnt Exist To Load");
}
// To delete the file.
public static void Delete()
{
var path = GetPath();
var fileName = GetFileName();
var fullpath = (Path.Combine(path, fileName));
// Delete the file from the disk.
if (File.Exists(fullpath))
{
File.Delete(fullpath);
Debug.Log("File Deleted " + fullpath);
// Create a new class to reference that will load the defualts back
saveVars = new SaveVariables();
}
else Debug.Log("No File To Delete");
}
[System.Serializable]
public class SaveVariables
{
public readonly string directory = "/TowerDefenseProject/";
public readonly string playerName = "default";
public readonly string extention = ".bsd";
// Put any vaiables you want to save in here. This will be saved and
// loaded every time Save() or Load() is ran.
// For instance
public int taco = 17;
public float drink = .25f;
}
}
This will be the basis of a full on serialized game save system Ive commented as much as i thought it needed it. I can walk you thru more if needed but this will work. Forgot to mention, what ever the vars are set to in the class is what they return to if you delete and start a new save. They are the default values.
Yea my inventory system only holds about 10 right now so that shouldn't be too bad. Thanks!
No problem if you need i can write a example class and you can build off that.
I'd actually really appreciate that this stuff is a bit over my head right now. I based my inventory system off of Brackys rpg tutorial series idk if that matters
Sorry it's taking me so long to implement this! I really appreciate the help. I am more familiar with c# so I'm just trying to wrap my head around it that's all. Thanks again for the help though!
Your answer
Follow this Question
Related Questions
How to persist player inventory using dictionary between scenes 2 Answers
How can I have several scenes running at the same time? 1 Answer
Inventory GUI 0 Answers
create a Inventory system ? 2 Answers
Can't add items to inventory. 0 Answers