Hi!
I am trying to work on a save game system for Unity and the community. I have, however, run into a slight problem. I am trying to access a serializable class from another script, but it is saying “The name “SavingSystemComponent.VectorsComponent” does not denote a valid type (“not found”).” This script is going to be working several times in a ‘for’ loop, so the data will be overwritten. I have a SaveingSystemComponent that can use the class for reference, but I have no idea how to get it to correctly use it. I have made the class public and have tried accessing it with a cached component, but nothing is working. Hopefully, as the Unity community, you will be able to set me right!
Here is the code:
SavingSystemManager:
#pragma strict
var SavingSystemComponents : SavingSystemComponent[];
var path : String;
function Start () {
path = PlayerPrefs.GetString("LoadFilePath","");
Save(path);
yield WaitForSeconds(5);
Load(path);
}
public function Save (path : String) {
SavingSystemComponents = FindObjectsOfType(SavingSystemComponent) as SavingSystemComponent[];
for (var SSC : SavingSystemComponent in SavingSystemComponents)
{
SSC.SavePosition(path);
}
}
public function Load(path : String)
{
var a : int = 0;
var loadfs : FileStream;
var loadformatter : BinaryFormatter = new BinaryFormatter();
//Buildings
var savedatafilesBuildings : String[] = Directory.GetFiles(path + "/Buildings/", "*.savedata");
for (a = 0; a <savedatafilesBuildings.Length;a++)
{
loadfs = new FileStream(savedatafilesBuildings[a], FileMode.Open);
var loaddata : SavingSystemComponent.VectorsComponent = loadformatter.Deserialize(loadfs);
print(loaddata.posX.ToString());
loadfs.Close();
}
/*//Entities
var savedatafilesEntities : String[] = Directory.GetFiles(path + "/Entities/", "*.savedata");
for (a = 0; a <savedatafilesEntities.Length;a++)
{
print(savedatafilesEntities[a]);
}
//Players
var savedatafilesPlayers : String[] = Directory.GetFiles(path + "/Players/", "*.savedata");
for (a = 0; a <savedatafilesPlayers.Length;a++)
{
print(savedatafilesPlayers[a]);
}*/
}
/*public function LoadPosition(savePath : String) {
var loadfs : FileStream = new FileStream(savePath + "/Buildings/Buildings.savedata", FileMode.Open);
var loadformatter : BinaryFormatter = new BinaryFormatter();
var loaddata : VectorsManager = loadformatter.Deserialize(loadfs);
print(loaddata.posX.ToString());
loadfs.Close();
}*/
SavingSystemComponent:
#pragma strict
import System.IO;
import System.Runtime.Serialization.Formatters.Binary;
public enum SaveType {Null=0, Building=1, Player=2,Entity=3}
public var saveType : SaveType = SaveType.Null;
public enum BuildingType {Null=0,Wall=1, Door=2,Window=3, Floor=4, Stairs=5, WinBarricade=6, Campfire=7}
public var buildingType : BuildingType = BuildingType.Null;
public enum BuildingMat {Null=0,Wood=1, ReinforcedWood=2,Steel=3}
public var buildingMat : BuildingMat = BuildingMat.Null;
public enum CampfireType {Null=0,Simple=1, Complex=2}
public var campfireType : CampfireType = CampfireType.Null;
var referenceClass : boolean = false;
private var savePath : String = "";
function Start () {
/*if (saveType == SaveType.Null)
{
print("This Save Component SaveType is null! Please assign a SaveType!");
enabled = false;
}
if (saveType == SaveType.Building && buildingType == BuildingType.Wall && buildingMat == BuildingMat.Wood)
{
print("Building Type:" + buildingMat + " " + buildingType);
}*/
}
public function SavePosition (path : String) {
if (referenceClass)
{
return;
}
var savefs : FileStream;
if (File.Exists(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata"))
{
savefs = new FileStream(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata", FileMode.Open);
}
else if (File.Exists(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata") == false)
{
savefs = new FileStream(path + "/Buildings/Buildings_"+transform.position.x+"."+transform.position.y+"."+transform.position.z+".savedata", FileMode.Create);
}
var saveformatter : BinaryFormatter = new BinaryFormatter();
var savedata : VectorsComponent = new VectorsComponent();
savedata.posX = transform.position.x;
savedata.posY = transform.position.y;
savedata.posZ = transform.position.z;
savedata.rotW = transform.rotation.w;
savedata.rotX = transform.rotation.x;
savedata.rotY = transform.rotation.y;
savedata.rotZ = transform.rotation.z;
saveformatter.Serialize(savefs, savedata);
savefs.Close();
}
@System.Serializable
public class VectorsComponent
{
public var posX : float;
public var posY : float;
public var posZ : float;
public var rotW : float;
public var rotX : float;
public var rotY : float;
public var rotZ : float;
}
Cheers, Stormy102