So, I’m trying to port a text adventure engine I originally wrote to run in browsers, using Javascript and jQuery, with a plugin thingy called jStorage for saving to the browser’s local storage.
With the original, I was able to establish a series of objects without giving them any contents:
var loc = {}
var ctr = {}
var dat = {}
var str = {}
Then write each “location” in the game in neat little groups, like this:
loc.INTRO1 = function(){}
//Figure out if the player is entering area (set up location's descriptions etc), or performing action *from* area.
ctr.INTRO1 = function(){}
//Big fat switch for playerCommand, fired by loc.INTRO1 if player enters command *from* area.
dat.INTRO1 = {}
//Object containing all variables for area (state of items, characters, story events)
str.INTRO1 = {}
//Object containing all the strings used by this area, such as descriptions and dialogue.
Using this system, in conjunction with jStorage, I was able to save EVERY variable for EVERY area in the entire game, with this line:
$.jStorage.set("storeDAT", dat);
Now, I just spent a while going over the Unity data persistence tutorial video, and recreated a (slightly modified) version of the C# script therein:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameStorage : MonoBehaviour {
public static GameStorage storage;
public float health;
public float experience;
//Note to self: access by GameStorage.storage.health etc
public void Save(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
playerData data = new playerData();
data.health = health;
data.experience = experience;
bf.Serialize(file, data);
file.Close();
}
public void Load(){
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
playerData data = (playerData)bf.Deserialize(file);
file.Close();
health = data.health;
experience = data.experience;
}
}
[Serializable]
class playerData {
public float health;
public float experience;
}
}
(I stripped out the DontDestroy stuff for the game object he uses, because my little text adventure is only going to need a single scene).
Now, I can see what this script does and how it does it… but I’m not really fluent in C# yet, and I’m trying to wrap my head around how I’d port/mimic my old system using this one.
So, after much rambling, my question is kinda simple:
Is it possible, using this sort of process, to save the contents of an entire class (instead of saving each variable one by one)?
Bonus/Alternate question:
Does UnityScript support the kind of object usage shown above? Can I var up some empty objects, then add their contents, then save the overall object using that same C# function?
Any pointers you guys could give would be much appreciated (note: I’m aware of the existence of “Unity Serializer”, but I’m a little reluctant to install such an extensive plugin just to save a pile of ints in a text adventure).