Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. namespace tslife
  2. {
  3. partial class game
  4. {
  5.  
  6.  
  7. world[] game_intro = _read_world<world>("intro");
  8.  
  9. //** other code **//
  10.  
  11. public void update()
  12. {
  13. //crashes: System.NullReferenceException: Object reference not set to an instance of an object
  14. Console.WriteLine(game_intro[0].data.Text);
  15.  
  16. }
  17.  
  18. private static T[] _read_world<T>(string level){
  19.  
  20. var json_data = string.Empty;
  21. string st = "";
  22. try
  23. {
  24. var stream = File.OpenText("Application/story/"+level+".json");
  25. //Read the file
  26. st = stream.ReadToEnd();
  27. }catch(SystemException e){}
  28. json_data = st;
  29.  
  30. //Console.WriteLine(json_data);
  31. // if string with JSON data is not empty, deserialize it to class and return its instance
  32. T[] dataObject = JsonConvert.DeserializeObject<T[]>(json_data);
  33. return dataObject;
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40. public class worldData {
  41. public string Text { get; set; }
  42. public string Icon { get; set; }
  43. public int sectionID { get; set; }
  44. }
  45.  
  46. public class world
  47. {
  48. public worldData data;
  49. }
  50.  
  51. [{
  52. "world":
  53. {
  54. "Text":"Hi",
  55. "Icon":"image01.png",
  56. "sectionID": 0
  57. }
  58. },
  59. {
  60. "world":
  61. {
  62. "Text":"Hey",
  63. "Icon":"image02.png",
  64. "sectionID": 1
  65. }
  66. }
  67. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement