Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have difficulties to implement loading a savegame file in my Windows Phone project. I use EasyStorage to load and save files. I get the following exception when I try to load after saving a file.

An exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll but was not handled in user code

I'm sure that I get this exception because saving is not finished when I try to load the file. But how can I solve the problem? I want to load the file after it was created. The file needs to be created when you start the game the first time.

What is the best way to solve the problem?

public void LoadingData() 
    { 
        if (saveDevice.IsReady == true) 
        { 
            if ((saveDevice.FileExists("TestContainer", "Data" + ".txt")) == false) 
            { 
                // save a file asynchronously. this will trigger IsBusy to return true 
                // for the duration of the save process. 
                saveDevice.SaveAsync( 
                  "TestContainer", 
                  "Data" + ".txt", 
                  stream => 
                  { 
                      using (StreamWriter writer = new StreamWriter(stream)) 
                      { 
                          writer.WriteLine(10 + ";" + 2 + ";" + 2); 
                          int Start = -50; 
                          int Offset = 150; 
                          int Row = 150; 
                          for (int i = 0; i <= 5 - 1; i++) 
                          { 
                              Start += Offset; 
                              writer.WriteLine("Image" + (i + 1).ToString() + ";" + Start + ";" + Row + ";" + false); 
                          } 
                      } 
                  }); 

                saveDevice.Load( 
            "TestContainer", 
             "Data" + ".txt", 
            stream => 
            { 
                using (StreamReader reader = new StreamReader(stream)) 
                { 
                    strs = reader.ReadLine().Split(';'); 
                    game1.Levels = int.Parse(strs[0]); 
                    game1.LevelProgress = int.Parse(strs[1]); 
                    game1.CurrentLevel = int.Parse(strs[2]); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                } 
            });            
            } 
            else 
            { 
                saveDevice.Load( 
                "TestContainer", 
                 "Data" + ".txt", 
                stream => 
                { 
                    using (StreamReader reader = new StreamReader(stream)) 
                    { 
                        strs = reader.ReadLine().Split(';'); 
                        game1.Levels = int.Parse(strs[0]); 
                        game1.LevelProgress = int.Parse(strs[1]); 
                        game1.CurrentLevel = int.Parse(strs[2]); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                    } 
                }); 
            } 
        } 
    }
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Notice how you used saveAsync. Either listen for an event from saveAsync to tell you it's done, or use sycronous saving, then load when the call finishes

share|improve this answer
    
saveDevice.Save works. But I don't understand the difference between saveDevice.Save and saveDevice.SaveAsync. What is the difference? Is saveDevice.Save a good way to solve this problem? –  Bryan Jul 6 '13 at 16:18
    
The difference is that save async starts saving, then whilst it is saving, it allows the calling code to continue to run. Save runs synchronously, that is, the calling code waits for the save to finish before it continues. The advantage to async is that your game can do other things during the save, ie. show a saving screen, etc. –  Seven Hundred and Forty-Two Jul 6 '13 at 18:32
    
Thanx. I need no saving screen, so I will use saveDevice.Save. –  Bryan Jul 6 '13 at 19:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.