While not new to game development I am new to both unity and have extremely limited knowledge when it comes to coding.
I would like to make a game in unity, and it seems my first order of business should be setting up a data storage metric. Now prior to this I co developed two games on a platform called roblox, and the simplest way to handle data storage were game assets called variables. These differed from script variables as they were in game objects, meaning they could be children and parents. These were incredibly useful for making dialog trees and for saving data between servers (closes unity equivalent would be scene). These could be stored in a data store folder which made saving a breeze. (player joins, if player has save data load it, if not clone template data and store it.)
Now for my game I am going to have alot of data to store, and if my understanding is correct you cannot use script variables to save cross session. I heard mention of playerpref being used, but that seems at least to me incredibly impractical.
Any suggestions on how to set up a central data store that would work cross session would be greatly appreciated. Thanks in advance for any suggestions.
To save/load larger files, you want to use binary to store them more efficiently, and make them slightly harder to edit.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public List<MyObject> objects = new List<MyObject>();
public class MyObject { /* TODO: Fill in your object */ }
public void Save () {
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = File.Open(Application.persistentDataPath + "/" + Application.loadedLevelName + optionalUniqueSceneIdentifier, FileMode.Create);
formatter.Serialize(stream, objects);
}
public void Load () {
if (!File.Exists(Application.persistentDataPath + "/" + Application.loadedLevelName + optionalUniqueSceneIdentifier)) {
Debug.LogError("Scene Object List Serializer " + gameObject.name + " could not find file " + Application.persistentDataPath + "/" + Application.loadedLevelName + optionalUniqueSceneIdentifier);
return;
}
else {
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream;
try {
stream = File.Open(Application.persistentDataPath + "/" + Application.loadedLevelName + optionalUniqueSceneIdentifier, FileMode.Open);
}
catch {
Debug.LogError("Loading file failed.");
return;
}
objects.Clear();
objects.AddRange((List<SceneObject>)formatter.Deserialize(stream));
if (debug)
Debug.Log("Deserializing " + objects.Count + " objects.");
if (autoInstantiate)
InstantiateObjectList();
}
You will need to fill in the rest of the file, but the important parts are there. This code saves a list of objects, but the same save/load paradigm can be adapted to any situation. You will need to create a custom data type (a Class) to store the specific parts of information you want.
I cant comment so I will post update as a answer.
@tanoshimi I diddnt even think about asset storage, so that will add basically every asset in the game to what I need to store (I will probably put them in a folder with a do not destroy script (assuming you can put models, audio, ect in a folder)
This game centers around the players interaction with AI (with AI vs AI interactions playing a role as well) with a fair bit of dialog, so on top of the players inventory there will be the AIs inventory, all relevant info to dialog trees, quests, an anything else I end up needing to store. Im thinking that instead of using separate scenes for each part of the game it will be easier to just have two and use one as asset storage from which I pull assets as I need them. The reason player pref would be impractical is due (to my understanding) its ability to only store a single variable per line, and I may be dealing with tens of thousands.
I may just end up using gui as a substitute variable object, name the text box to identify what it holds and use the text information to store data (this way I can have all my game information stored in one object and I can use children and parent to order how data is handled).
@impossible
I cant code but from what I understand line 10-20 creates a file somewhere on the hosts PC (where exactly is beyond me). The rest of the code seems to be used to load the file, though the code is beyond my ability to make sense of. And I have no clue what is going on after line 37, im guessing that its used to overwrite any existing data.