-1

Is there any simple way to store variable when my game closes and load it when it opens next time? Even better would be if I could store instance of my class and then load it.

Mykybo
  • 1,429
  • 1
  • 12
  • 24
  • 2
    write it to a text file and read it on startup? – Colm Prunty May 18 '14 at 12:36
  • See http://stackoverflow.com/questions/4266875/how-to-quickly-save-load-class-instance-to-file or http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file. – Mihai8 May 18 '14 at 12:37
  • 1
    What do you want to store? Is this like a saved game, or are these settings for how your application should run? – just.another.programmer May 18 '14 at 12:38
  • 2
    You could serilize your class and read it next time the game gets started – Kimmax May 18 '14 at 12:39
  • 1
    You could try the Settings class http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx Scroll down to the bit about using settings at run time – user184994 May 18 '14 at 12:41
  • 1
    There are too many ways to do this, though given your wish, serialisation and dumpt to a file looks like the best one. – Tony Hopkinson May 18 '14 at 12:51

2 Answers2

2

You can serialize the object to an .xml file or to a DB and on load deserialize it and you will have the object in the memory.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
Shakedav
  • 63
  • 6
0

You can use Json.NET to serialize the instance of a class. And upon loading all you have to do is deserialize it, everything is done by the library.

Serialization example:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Sizes": [
//    "Small"
//  ]
//}

Deserialization example:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
Bruno Klein
  • 3,217
  • 5
  • 29
  • 39