0

I'm trying to port my pc XNA game to the xbox and have tried to implement xna easystorage alongside my existing pc file management for highscores. Basically trying to combine http://xnaessentials.com/tutorials/highscores.aspx/tutorials/highscores.aspx with http://easystorage.codeplex.com/

I'm running into one specific error regarding the LoadHighScores() as error with 'return (data);' - Use of unassigned local variable 'data'.

I presume this is due to async design of easystorage/xbox!? but not sure how to resolve - below are code samples:

ORIGINAL PC CODE: (works on PC)

public static HighScoreData LoadHighScores(string filename) { HighScoreData data; // Get the path of the save game

string fullpath = "Content/highscores.lst";
// Open the file
FileStream stream = File.Open(fullpath, FileMode.Open,FileAccess.Read);    
        try    
        {         // Read the data from the file
    XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
    data = (HighScoreData)serializer.Deserialize(stream);    
        }    
        finally
         {        // Close the file        
            stream.Close();    
        }     
        return (data);
    }

XBOX PORT: (with error)

public static HighScoreData LoadHighScores(string container, string filename) { HighScoreData data;

        if (Global.SaveDevice.FileExists(container, filename))
                {
                    Global.SaveDevice.Load(container, filename, stream =>
                            {
                                File.Open(Global.fileName_options, FileMode.Open,//FileMode.OpenOrCreate,
                                         FileAccess.Read);  
                                try    
                            {         
                                            // Read the data from the file
                                        XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
                                        data = (HighScoreData)serializer.Deserialize(stream);    
                            }    
                              finally
                             {      
                                    // Close the file  

                                stream.Close();

                             }   

                            });

                }


        return (data);
    }

Any ideas?

1 Answers1

2

Assign data before return. ;)

  data = (if_struct) ? new your_struct() : null;
  if (Global.SaveDevice.FileExists(container, filename))
  {
     ......
  }
  return (data);
}
Blau
  • 5,742
  • 1
  • 18
  • 27
  • +1 I've noticed the Xbox can be very picky about this where the desktop CLR is a lot more forgiving.. – MattDavey Oct 19 '11 at 14:21
  • Thanks but that doesn't work as data is a struct and they are non-nullable - Any other ideas? public struct HighScoreData { public string[] Name; public int[] Score; public int[] Level; public int Count; public HighScoreData(int count) { Name = new string[count]; Score = new int[count]; Level = new int[count]; Count = count; } } – user1003211 Oct 20 '11 at 07:18
  • 1
    In that case you just need to put data = new MyStruct(). The compiler is complaining that it's possible to reach the exit of the method without data being assigned a value.. – MattDavey Oct 20 '11 at 09:02
  • Thanks - I think I'm going to have to rethink this whole approach or have break till my brain clears!, as in the PC version as outlined in the first link in my original post - I 'initailise' top ten scores and create file, and when porting have tried to move this after the savedevice is selected, by implementing a createscores() if no file exists, but the global.savedevice is still undefined and no file is being created, even though I get the prompt to select memory option. SAving data on the xbox is far more complex than the PC! must be a simpler way to save a bunch of numbers to file :) – user1003211 Oct 20 '11 at 11:00