Advertisement



Go Back   Zune Boards > Zune Discussions > Zune Games > Development Discussions

New Member?



 
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Development Discussions All developers who are coding games may stop by here for any help, suggestions, and everything development related.

Reply
 
LinkBack Thread Tools
Old 07-08-2008, 12:27 PM   #1 (permalink)
Zune Guardian
 
ShotgunSnipist's Avatar
 
Join Date: May 2008
Location: Colorado
Posts: 738
Reputation: 74
Send a message via AIM to ShotgunSnipist
Default Saving game data to Zune HDD


Okay so how would I make it so I could save... I think 4 different types of data?

and int and 3 bytes,
and then be able to read them back as soon as the game starts again.


On a side note:
Does anyone know how to display how long the song has been playing/how long it is?
So it would look like: 1:53/47



__________________
Invisible Text. =P
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



ShotgunSnipist is offline   Reply With Quote
Old 07-08-2008, 01:27 PM   #2 (permalink)
zB Programmer
Experienced Member
 
LedZepp's Avatar
 
Join Date: Mar 2007
Posts: 975
Reputation: 119
Default

MediaPlayer.PlayPostition is where you are
MediaPlayer.Queue.ActiveSong.Duration is how long it is

There are a couple ways to save/read data, but both need to start like:
Code:
IAsyncResult result;
result = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
StorageDevice device = Guide.EndShowStorageDeviceSelector(result);
Then as a precaution you can have
Code:
if (device.IsConnected)
{
but i think its always connected.
You then have to open a storagecontainer for your game/app and specify where you want the file saved and named
Code:
StorageContainer container = device.OpenContainer("MyApp");
String filename = Path.Combine(container.Path, "My File.txt");
Now this is where it changes based on if you are reading or writing
to read
Code:
if (File.Exists(filename))
{
StreamReader reader = new StreamReader(filename);
You first check if the file exists then if it does you can load it into a reader(I used to use the Serializer class, but Netrix pointed out to me that streamreader/writer works better on the zune and causes less errors)
If you are writing to a file you dont need the file check, and instead of StreamReader you use StreamWriter.

For writing your int and bytes you would use you streamwriter variable like so
Code:
writer.WriteLine(YourInt);
writer.WriteLine(1stByte);
writer.WriteLine(2ndByte);
writer.WriteLine(3rdByte);
Then you would close the stream like Writer.Close() and close the container Container.Dispose();

For reading your values, you would just use the reader variable like
Code:
while (reader.Peek() >= 0) 
{
variable = reader.ReadLine();
}
Its a bit tricky with your situation, you could create a string array, with a length of 4, and store each line in to it and then just parse the first array variable as int and store it into your int variable and then do the same for the next 3 but for bytes.

After finishing reading you would again close the reader and dispose the container.

You also need this at the very top
Code:
using System.IO;
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

Last edited by LedZepp : 07-08-2008 at 01:36 PM.




LedZepp is offline   Reply With Quote
Old 07-08-2008, 05:59 PM   #3 (permalink)
Zune Guardian
 
ShotgunSnipist's Avatar
 
Join Date: May 2008
Location: Colorado
Posts: 738
Reputation: 74
Send a message via AIM to ShotgunSnipist
Default

Quote:
Originally Posted by LedZepp View Post
MediaPlayer.PlayPostition is where you are
MediaPlayer.Queue.ActiveSong.Duration is how long it is

There are a couple ways to save/read data, but both need to start like:
Code:
IAsyncResult result;
result = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
StorageDevice device = Guide.EndShowStorageDeviceSelector(result);
Then as a precaution you can have
Code:
if (device.IsConnected)
{
but i think its always connected.
You then have to open a storagecontainer for your game/app and specify where you want the file saved and named
Code:
StorageContainer container = device.OpenContainer("MyApp");
String filename = Path.Combine(container.Path, "My File.txt");
Now this is where it changes based on if you are reading or writing
to read
Code:
if (File.Exists(filename))
{
StreamReader reader = new StreamReader(filename);
You first check if the file exists then if it does you can load it into a reader(I used to use the Serializer class, but Netrix pointed out to me that streamreader/writer works better on the zune and causes less errors)
If you are writing to a file you dont need the file check, and instead of StreamReader you use StreamWriter.

For writing your int and bytes you would use you streamwriter variable like so
Code:
writer.WriteLine(YourInt);
writer.WriteLine(1stByte);
writer.WriteLine(2ndByte);
writer.WriteLine(3rdByte);
Then you would close the stream like Writer.Close() and close the container Container.Dispose();

For reading your values, you would just use the reader variable like
Code:
while (reader.Peek() >= 0) 
{
variable = reader.ReadLine();
}
Its a bit tricky with your situation, you could create a string array, with a length of 4, and store each line in to it and then just parse the first array variable as int and store it into your int variable and then do the same for the next 3 but for bytes.

After finishing reading you would again close the reader and dispose the container.

You also need this at the very top
Code:
using System.IO;
Awesome!
I'll see if I can get this to work.
Thanks!
__________________
Invisible Text. =P
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



ShotgunSnipist is offline   Reply With Quote
Old 07-08-2008, 07:10 PM   #4 (permalink)
zB Programmer
Experienced Member
 
LedZepp's Avatar
 
Join Date: Mar 2007
Posts: 975
Reputation: 119
Default

Heres a small example
http://pastebin.com/m75792695
I havent tested it but it should work
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




LedZepp is offline   Reply With Quote
Old 07-08-2008, 10:07 PM   #5 (permalink)
Jr. Member
 
Tiptup300's Avatar
 
Join Date: Apr 2008
Posts: 392
Reputation: 121
Default

Wait, potentionally, that wouldn't work. Would it? There is a chance it wouldn't find a storage device...I think....



Tiptup300 is offline   Reply With Quote
Old 07-09-2008, 06:46 AM   #6 (permalink)
Jr. Member
 
Anachostic's Avatar
 
Join Date: Jun 2007
Posts: 407
Reputation: 77
Default

I'm making do by taking a class and running it through the XML serializer into an XML document, then saving the XML document to the local storage. Ditto coming back out: load the XML, deserialize into a class.




Anachostic is offline   Reply With Quote
Old 07-09-2008, 08:39 AM   #7 (permalink)
you lost the game.
zB Programmer
Section Staff
Zune Freak
 
itsnotabigtruck's Avatar
 
Join Date: May 2008
Posts: 1,218
Reputation: 462
Awards Showcase
Best username 
Total Awards: 1
Default

@Tiptup300: No, there isn't. The API is designed for the Xbox, where it can prompt the user for a location to store their stuff, however, on the Zune, it allocates a folder and returns the path to it no matter what.

@ShotgunSnipist: You might also want to try BinaryWriter/BinaryReader. Those classes let you write strings, numbers, and byte arrays to a file in raw form. Using XML will cause a large delay when users start your game, as it doesn't have to load the XML parser (System.Xml.dll) unless you use it. System.Xml.dll is by far the largest file in the XNA runtime.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

signature by
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




itsnotabigtruck is online now   Reply With Quote
Old 07-09-2008, 04:19 PM   #8 (permalink)
Jr. Member
 
Join Date: Jun 2007
Location: Mass
Posts: 312
Reputation: 78
Default

Fantastic answer LedZepp - very helpful for my new game im workign on, asteroids:

http://www.zuneboards.com/forums/dev...asteroids.html

But really, VERY helpful!!!!!!




shutout5591 is offline   Reply With Quote
Old 07-19-2008, 10:32 AM   #9 (permalink)
Experienced Zuner
 
Rockman87's Avatar
 
Join Date: Nov 2007
Location: UT, USA
Posts: 199
Reputation: 25
Send a message via MSN to Rockman87
Default

Is there a way to write or save any other file to the hardrive than a txt file? Would it be possible to make like a paint program and then save the image that is on the screen to the hd for access later?
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




Rockman87 is offline   Reply With Quote
Old 07-19-2008, 12:08 PM   #10 (permalink)
Jr. Member
 
SimReality's Avatar
 
Join Date: May 2008
Posts: 410
Reputation: 60
Default

You can save any data to the hard drive that you want. Images, text, XML, or even completely arbitrary binary data.



SimReality is offline   Reply With Quote
Old 07-19-2008, 02:37 PM   #11 (permalink)
zB Programmer
Jr. Member
 
DiNoGames's Avatar
 
Join Date: May 2008
Location: Bremen, germany
Posts: 454
Reputation: 246
Send a message via Skype™ to DiNoGames
Default

Quote:
Originally Posted by itsnotabigtruck View Post
@ShotgunSnipist: You might also want to try BinaryWriter/BinaryReader. Those classes let you write strings, numbers, and byte arrays to a file in raw form.
Yup, Nick is right, and Trucky already posted "how" before...
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

Last edited by DiNoGames : 07-19-2008 at 11:32 PM.







DiNoGames is offline   Reply With Quote
Old 07-19-2008, 08:55 PM   #12 (permalink)
Experienced Zuner
 
Rockman87's Avatar
 
Join Date: Nov 2007
Location: UT, USA
Posts: 199
Reputation: 25
Send a message via MSN to Rockman87
Default

Quote:
Originally Posted by DiNoGames View Post
Yup, Nick is right, and Trucky already posted "how" before...
I'm sorry I've tried searching for how to do it... I can't find the thread. Do I just use the Filewriter?
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




Rockman87 is offline   Reply With Quote
Old 07-19-2008, 09:08 PM   #13 (permalink)
zB Programmer
Experienced Member
 
LedZepp's Avatar
 
Join Date: Mar 2007
Posts: 975
Reputation: 119
Default

Quote:
Originally Posted by itsnotabigtruck View Post
@ShotgunSnipist: You might also want to try BinaryWriter/BinaryReader. Those classes let you write strings, numbers, and byte arrays to a file in raw form. Using XML will cause a large delay when users start your game, as it doesn't have to load the XML parser (System.Xml.dll) unless you use it. System.Xml.dll is by far the largest file in the XNA runtime.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




LedZepp is offline   Reply With Quote
Old 07-20-2008, 12:26 PM   #14 (permalink)
Experienced Zuner
 
Rockman87's Avatar
 
Join Date: Nov 2007
Location: UT, USA
Posts: 199
Reputation: 25
Send a message via MSN to Rockman87
Default

ok thanks... I just didn't understand properly... my bad.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




Rockman87 is offline   Reply With Quote
Old 07-20-2008, 12:30 PM   #15 (permalink)
Zuner
 
CouchPotato99's Avatar
 
Join Date: May 2008
Posts: 75
Reputation: 53
Default

If anyone's confused about how to write and read binary files, this is the code I used in my game:

Code:
public void GetOptions()
{
    if (File.Exists(Path.Combine(container.Path, "options.dat")))
    {
        BinaryReader dataFile;
        dataFile = new BinaryReader(new FileStream(Path.Combine(container.Path, "options.dat"), FileMode.Open));
        cantSelectRevealed = dataFile.ReadBoolean();
        flagWithPlay = dataFile.ReadBoolean();
        selectedSkin = dataFile.ReadInt32();
        dataFile.Close();
    }
    else SetOptions();
}

public void SetOptions()
{
    BinaryWriter dataFile;
    dataFile = new BinaryWriter(new FileStream(Path.Combine(container.Path, "options.dat"), FileMode.Create));
    dataFile.Write(cantSelectRevealed);
    dataFile.Write(flagWithPlay);
    dataFile.Write(selectedSkin);
    dataFile.Close();
}
Note that, for all I know, that could be horrible inefficient code... but it does work...




CouchPotato99 is offline   Reply With Quote
Old 07-26-2008, 04:57 PM   #16 (permalink)
Experienced Zuner
 
Rockman87's Avatar
 
Join Date: Nov 2007
Location: UT, USA
Posts: 199
Reputation: 25
Send a message via MSN to Rockman87
Default

Is there a way to save the current zune screen to a jpeg file or someother kind of file that will know the color of the pixels on the screen.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




Rockman87 is offline   Reply With Quote
Old 07-26-2008, 07:03 PM   #17 (permalink)
you lost the game.
zB Programmer
Section Staff
Zune Freak
 
itsnotabigtruck's Avatar
 
Join Date: May 2008
Posts: 1,218
Reputation: 462
Awards Showcase
Best username 
Total Awards: 1
Default

Quote:
Originally Posted by Rockman87 View Post
Is there a way to save the current zune screen to a jpeg file or someother kind of file that will know the color of the pixels on the screen.
Well, you could render to a RenderTarget, which lets you get a Texture2D of everything you rendered. Not sure what you would want a JPEG for, as XNA itself can't read JPEG files, and there isn't an effective way of uploading files back to the computer.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

signature by
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




itsnotabigtruck is online now   Reply With Quote
Old 07-26-2008, 07:44 PM   #18 (permalink)
Experienced Zuner
 
Rockman87's Avatar
 
Join Date: Nov 2007
Location: UT, USA
Posts: 199
Reputation: 25
Send a message via MSN to Rockman87
Default

Quote:
Originally Posted by itsnotabigtruck View Post
Well, you could render to a RenderTarget, which lets you get a Texture2D of everything you rendered. Not sure what you would want a JPEG for, as XNA itself can't read JPEG files, and there isn't an effective way of uploading files back to the computer.

oh ok... however this sparks another question. So if you RenderTarget and get a Texture2D of the screen, would it then be possible to save the Texture to the HDD and then load it when you start the app again?
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.



To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.




Rockman87 is offline   Reply With Quote
Old 07-26-2008, 07:51 PM   #19 (permalink)
Purger of Ignorance
zB Programmer
Section Staff
Super Zuner
 
Netrix's Avatar
 
Join Date: Jun 2008
Location: In my own world
Posts: 1,670
Reputation: 231
Send a message via MSN to Netrix
Awards Showcase
Favorite zB Extremity 
Total Awards: 1
Default

Quote:
Originally Posted by Rockman87 View Post