Loading and Saving a profile in XML to work on iPhone
by Andy Hawkins · in iTorque 2D · 08/25/2012 (7:41 am) · 7 replies
How would I go about saving and loading the player's profiles (name, highest score, level etc) via XML so that it works on an iPhone - meaning it's saved on the iPhone? It's so the player starts the game at a later date and they continue on from there?
Also would I be correct in assuming that if this works for iPhone it would also work on the PC?
Also would I be correct in assuming that if this works for iPhone it would also work on the PC?
#2
08/26/2012 (6:38 am)
Sweet thanks man. I'll try it out.
#3
10/07/2012 (8:18 am)
Okay this works perfectly, but the only problem is, I can't see where the file is being written. Is it on disk or in the executable RAMDRIVE, something, something?
#4
"Public" means the files can be accessed and transferred using the iTunes File Sharing window (you also have to enable "Application Supports iTunes File Sharing" in xcode (Targets->Info).
Private isn't really private, just concealed from the casual user. There are apps that can get at them, so don't store anything that you wouldn't mind being read.
10/07/2012 (1:17 pm)
For iOS hardware you'll need some functions that aren't in iT2d. Add to iPhonefileio.mm:ConsoleFunction(getPublicDocumentsDirectory, const char*, 1, 1, "Returns the documents directory.")
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
const char* tOutput = [documentPath UTF8String];
return tOutput;
}
ConsoleFunction(getPrivateDocumentsDirectory, const char*, 1, 1, "Returns the Library directory." )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Private Documents"];
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
const char* tOutput = [documentsDirectory UTF8String];
return tOutput;
}"Public" means the files can be accessed and transferred using the iTunes File Sharing window (you also have to enable "Application Supports iTunes File Sharing" in xcode (Targets->Info).
Private isn't really private, just concealed from the casual user. There are apps that can get at them, so don't store anything that you wouldn't mind being read.
#5
10/21/2012 (7:47 am)
Cool thanks again - but I can't see it saving on the PC. Where does it get saved? I've scoured the hard drive and can't find the xml.
#6
Here's the default local...
Now I just need to get it to connect to localhost but it appends this to it first for some reason like this...
11/02/2012 (6:36 pm)
Okay finally found it as I'm trying to write out to localhost now.Here's the default local...
C:/Users/<yourLoginName>/AppData/Roaming/Independent/Independent appears to be a default company name.
Now I just need to get it to connect to localhost but it appends this to it first for some reason like this...
C:/Users/<yourLoginName>/AppData/Roaming/Independent/<yourGameName>/http://127.0.0.1/playerProfile.xml
#7
I guess I have to go to source and strip it out, but that's out of scope because I need an out-of-the-box solution. The rule is not to modify the source.
11/02/2012 (6:51 pm)
The problem is the code appends C:/Users/<yourLoginName>/AppData/Roaming/Independent/<yourGameName>/ to it.I guess I have to go to source and strip it out, but that's out of scope because I need an out-of-the-box solution. The rule is not to modify the source.
Watermark
Watermark RPG
function SaveGame() { %txml = new ScriptObject() { class = "XML"; }; %fileFullname = "~/saves/Savefile.xml"; if(%txml.beginWrite(%fileFullname)) { %txml.writeClassBegin("SaveClass"); %txml.writeClassBegin("PlayerName"); %txml.writeValue($myName); %txml.writeClassEnd(); %txml.writeClassBegin("PlayerScore"); %txml.writeValue($myScore); %txml.writeClassEnd(); %txml.writeClassBegin("PlayerLocation"); %txml.writeAttribute("levelid", $myLevel); %txml.writeAttribute("levelx", $myX); %txml.writeAttribute("levely", $myY); %txml.writeClassEnd(); %txml.writeClassEnd(); %txml.endWrite(); } } function LoadGame() { %txml = new ScriptObject() { class = "XML"; }; %fileFullname = "~/saves/Savefile.xml"; if(%txml.beginRead(%fileFullname)) { if ( %txml.readClassBegin("SaveClass")) { $myName = %txml.readField("PlayerName"); $myScore = %txml.readField("PlayerScore"); %txml.readClassBegin("PlayerLocation"); $myLevel = %txml.readAttribute("levelid"); $myX = %txml.readAttribute("levelx"); $myY = %txml.readAttribute("levely"); %txml.readClassEnd(); } %txml.endRead(); } }