Hey, I’m trying to create a directory where I can store player data, preferences, usernames, ect… I’ve created this script:
REMOVED SCRIPT - solved this problem
I then attached this script to an empty GO. All the debug messages execute, but when I try to navigate to this folder, it isn’t there. Is it creating it in another place I’m not aware of, or am I just doing this incorrectly?
Edit - I was able to create a folder in Library/Application Support, so I fixed that. But now, I’m able to serialize my Hashtable, but sometimes it deletes its self, and writes over it, and I KNOW it does when trying to right to it from either the editor or the standalone, but it does also do it others time for reasons I’m not sure of.
What I’m doing is I store a Hashtable of objects of type User (which is serializable and contains a member Score that us serializable as well). Then, on start of the application I create the directory and the file (I only do this the first time the game ever runs), then I deserialize the file containg the hastable of User objects like this:
void Awake ()
{
// Keep this alove each scene
DontDestroyOnLoad (this);
// Read in the PlayerPrefs data and load it into the HashTable
Stream fs;
if( !Application.isEditor) // The reason for this is that the editor and standalone have different encryptions for some odd reason
{
fs = File.OpenRead("/Library/Application Support/Asteroid Blaster/PlayerPrefs.abp");
}
else
{
fs = File.OpenRead("/Library/Application Support/Asteroid Blaster/PlayerPrefsEditor.abp");
}
try
{
//IFormatter formatter = new BinaryFormatter();
BinaryFormatter formatter = new BinaryFormatter();
m_UserList = formatter.Deserialize(fs) as Hashtable;
}
catch(SerializationException e)
{
System.Console.WriteLine("Failed to de-serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
This should reload hashtables with all the User back into the Hashtable object on startup everytime the game is ran.
Then in the menu I have a text field and button. The user enter ther username, and if the username doesn’t all ready exist, I stores it n hashtable:
void OnMouseDown()
{
// Get the text from the Inputfield
string UserNameString = GameObject.Find("MainMenuBackLayer/TextInput/InputField").guiText.text;
// If it doesn't all ready contain the key (the key will be the name of the username)
if( !GlobalData.Users.ContainsKey(UserNameString) )
{
System.Console.WriteLine( "Added a new user" );
// Create a new User
User newUser = new User( UserNameString );
// Add the user to the hash
GlobalData.Users.Add( UserNameString, newUser );
// TODO - Implement active user
//GlobalData.ActiveUser = UserNameString;
}
LoadMainGame();
}
Finally, at the end of the game when it quits I serialize the hashtable and write to the file where I will eventually load it from again next time the game is ran.
// Ensure that the instance is destroyed when the game is stopped in the editor.
void OnApplicationQuit()
{
// Save the user(s) data
Stream myStream;
if( !Application.isEditor)
myStream = File.OpenWrite("/Library/Application Support/Asteroid Blaster/PlayerPrefs.abp");
else
myStream = File.OpenWrite("/Library/Application Support/Asteroid Blaster/PlayerPrefsEditor.abp");
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(myStream, GlobalData.Users);
}
catch(SerializationException e)
{
System.Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
myStream.Close();
Debug.Log( "The newely serialized file is has closed");
}
s_Instance = null;
//Debug.Log( "we quit" );
}
I just don’t know why sometimes the it erases its self, and the two different games (the editor version compared to the standalone) do it differently. If it makes any different the Hashtable is stored in the Global Data class and the Awake and OnApplicationQuit is called from that class, and that class/GO exists through the whole program.
Thanks a bunch for any help!!
Jedd