I use EasysStorage to save and load my save file on Windows. http://easystorage.codeplex.com
Saving works, but I always get this error message when I'm loading the save file. I get the error message in the "throw new..." line of SaveDeviceSynchronous.cs:
private void VerifyIsReady()
{
if (!IsReady)
throw new InvalidOperationException(Strings.StorageDevice_is_not_valid);
}
InvalidOperationException was unhandled StorageDevice is not valid.
What is wrong? Is something wrong with my Loading method? You find the Loading method in the SaveGame class. I just want to load the highscore at the beginning of the game.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SaveGame savenow;
SpriteFont texthighscore;
GamePadState gps, gpsPrev;
KeyboardState ks, ksPrev;
public int highscore;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
}
protected override void Initialize()
{
savenow = new SaveGame(this);
savenow.Initialize();
savenow.Loading();
highscore = savenow.high;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
savenow.Load(Content);
texthighscore = Content.Load<SpriteFont>("Arial");
// set up touch gesture support: make vertical drag and flick the
// gestures that we're interested in.
TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.Flick;
}
protected override void Update(GameTime gameTime)
{
gpsPrev = gps;
ksPrev = ks;
gps = GamePad.GetState(PlayerIndex.One);
ks = Keyboard.GetState();
bool tapped = false;
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Tap)
tapped = true;
}
if ((gps.IsButtonDown(Buttons.A) && gpsPrev.IsButtonUp(Buttons.A)) ||
(ks.IsKeyDown(Keys.Space) && ksPrev.IsKeyUp(Keys.Space)) ||
tapped)
{
savenow.Saving();
}
highscore += 1;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(texthighscore, highscore.ToString(), new Vector2(200, 200), Color.White);
savenow.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
public class SaveGame
{
SpriteFont font;
IAsyncSaveDevice saveDevice;
Vector2 textPos = new Vector2(100, 100);
private Game1 game1;
public int high;
public SaveGame(Game1 game)
{
game1 = game;
}
public void Loading()
{
if (saveDevice.FileExists("TestContainer", "SaveFile.txt"))
{
saveDevice.Load(
"TestContainer",
"SaveFile.txt",
stream =>
{
using (StreamReader reader = new StreamReader(stream))
{
high = int.Parse(reader.ReadLine());
}
});
}
}
public void Saving()
{
// make sure the device is ready
if (saveDevice.IsReady)
{
// save a file asynchronously. this will trigger IsBusy to return true
// for the duration of the save process.
saveDevice.SaveAsync(
"TestContainer",
"SaveFile.txt",
stream =>
{
using (StreamWriter writer = new StreamWriter(stream))
writer.WriteLine(game1.highscore);
});
}
}
public void Initialize()
{
// we can set our supported languages explicitly or we can allow the
// game to support all the languages. the first language given will
// be the default if the current language is not one of the supported
// languages. this only affects the text found in message boxes shown
// by EasyStorage and does not have any affect on the rest of the game.
EasyStorageSettings.SetSupportedLanguages(Language.English);
// on Windows Phone we use a save device that uses IsolatedStorage
// on Windows and Xbox 360, we use a save device that gets a shared StorageDevice to handle our file IO.
#if WINDOWS_PHONE
saveDevice = new IsolatedStorageSaveDevice();
#else
// create and add our SaveDevice
SharedSaveDevice sharedSaveDevice = new SharedSaveDevice();
game1.Components.Add(sharedSaveDevice);
// make sure we hold on to the device
saveDevice = sharedSaveDevice;
// hook two event handlers to force the user to choose a new device if they cancel the
// device selector or if they disconnect the storage device after selecting it
sharedSaveDevice.DeviceSelectorCanceled += (s, e) => e.Response = SaveDeviceEventResponse.Force;
sharedSaveDevice.DeviceDisconnected += (s, e) => e.Response = SaveDeviceEventResponse.Force;
// prompt for a device on the first Update we can
sharedSaveDevice.PromptForDevice();
#endif
// we use the tap gesture for input on the phone
TouchPanel.EnabledGestures = GestureType.Tap;
#if XBOX
// add the GamerServicesComponent
game1.Components.Add(new Microsoft.Xna.Framework.GamerServices.GamerServicesComponent(game1));
#endif
// hook an event so we can see that it does fire
saveDevice.SaveCompleted += new SaveCompletedEventHandler(saveDevice_SaveCompleted);
}
void saveDevice_SaveCompleted(object sender, FileActionCompletedEventArgs args)
{
// just write some debug output for our verification
Debug.WriteLine("SaveCompleted!");
}
public void Load(ContentManager content)
{
font = content.Load<SpriteFont>("Font");
}
public void Draw(SpriteBatch spriteBatch)
{
game1.GraphicsDevice.Clear(Color.CornflowerBlue);
Vector2 textPos = new Vector2(game1.GraphicsDevice.Viewport.TitleSafeArea.X + 50, game1.GraphicsDevice.Viewport.TitleSafeArea.Y + 10);
spriteBatch.DrawString(font, string.Format("Save device {0} ready.", saveDevice.IsReady ? "is" : "is not"), textPos, Color.White);
textPos.Y += font.LineSpacing;
spriteBatch.DrawString(font, string.Format("Save device {0} busy.", saveDevice.IsBusy ? "is" : "is not"), textPos, Color.White);
textPos.Y += font.LineSpacing;
if (saveDevice.IsReady)
{
#if WINDOWS_PHONE
string instructions = "Tap the screen to save a file.";
#else
string instructions = "Press the A button or space key to save a file.";
#endif
spriteBatch.DrawString(font, instructions, textPos, Color.White);
}
}
}
You can download my project here: http://www.file-upload.net/download-6992121/Savetestwindows.rar.html
device.IsReadyis true? – ashes999 Dec 31 '12 at 18:33