Ooops
November 28, 2020, 02:06:03 AM

Author Topic: [bmx] Save a list of objects by Warpy [ 1+ years ago ]  (Read 465 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Save a list of objects by Warpy [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Save a list of objects
Author : Warpy
Posted : 1+ years ago

Description : This might be useful for savegames and the like. Suppose you've got a list of objects of a custom type you want to save to a file, so you can load them back in later. An easy way to do this is to add save and load methods/functions to the type, then call each of them in turn by iterating through the list.

Code :
Code: BlitzMax
  1. 'Here's the first example type. It's got 4 fields - an int, a float, a string, and a pointer to an object of another type
  2. Type mytype1
  3.         Field anint%,afloat#,astring$
  4.         Field anotherobject:mytype2
  5.        
  6.         Method save(f:TStream)
  7.         'Call this method to save the object to a given filestream, which must be writeable
  8.        
  9.                 'Write the int, float and string to the file, they're easy to do
  10.                 WriteInt f,anint
  11.                 WriteFloat f,afloat
  12.                 WriteLine f,astring 'watch out, it's got to be writeline so the string's terminated and it can be loaded back in properly
  13.                 'For the pointer, we first need an int that shows whether it's null or not (you can't write 'Null' to a file!)
  14.                 If anotherobject
  15.                         WriteInt f,1
  16.                         'As the pointer isn't null, call the object's save method
  17.                         anotherobject.save(f)
  18.                 Else
  19.                         WriteInt f,0
  20.                 EndIf
  21.         End Method
  22.        
  23.         Function load:mytype1(f:TStream)
  24.         'Call this function (in the form m:mytype1=mytype1.load() because the object doesn't
  25.         'exist yet) To load an Object from a filestream f, which must be readable.
  26.  
  27.                 m:mytype1=New mytype1 'Create a new blank object to fill in
  28.                 'Load the easy bits in
  29.                 m.anint=Readint(f)
  30.                 m.afloat=ReadFloat(f)
  31.                 m.astring=ReadLine(f)
  32.                
  33.                 hasanotherobject=Readint(f) 'This int will = 1 if there's the other object to load
  34.                 If hasanotherobject
  35.                         m.anotherobject=mytype2.load(f) 'Load in the other object
  36.                 EndIf
  37.                 Return m
  38.         End Function
  39. End Type
  40.  
  41. 'This is the other demo type, just used to show how to save and load pointers to other objects
  42. Type mytype2
  43.         Field anotherint%
  44.        
  45.         Method save(f:TStream)
  46.                 WriteInt f,anotherint
  47.         End Method
  48.        
  49.         Function load:mytype2(f:TStream)
  50.                 m:mytype2=New mytype2
  51.                 m.anotherint=Readint(f)
  52.                 Return m
  53.         End Function
  54. End Type
  55.  
  56. 'This function saves a list of mytype1 objects to the given filestream, which must be writeable
  57. Function savelistofmytype1(list:TList,f:TStream)
  58.         For m:mytype1=EachIn list
  59.                 m.save(f)
  60.         Next
  61. End Function
  62.  
  63. 'This function loads a list of mytype1 objects from the given filestream, which must be readable
  64. Function loadlistofmytype1:TList(f:TStream)
  65.         list:TList=New TList
  66.         While Not Eof(f)
  67.                 m:mytype1=mytype1.load(f)
  68.                 list.addlast m
  69.         Wend
  70.         Return list
  71. End Function
  72.  
  73. 'This function prints out the values of all the objects in the list,
  74. 'so you can compare the saved and loaded data to check they're the same.
  75. Function printmytype1list(list:TList)
  76.         For m:mytype1=EachIn list
  77.                 Print m.anint
  78.                 Print m.afloat
  79.                 Print m.astring
  80.                 If m.anotherobject
  81.                         Print "another object: "+String(m.anotherobject.anotherint)
  82.                 EndIf          
  83.         Next
  84. End Function
  85.  
  86.  
  87. 'demo: Create a list, fill it with 10 objects, half of them with the anotherobject pointer
  88. ' non-null, save it, forget about it, then load it in again.
  89. mylist:TList=New TList
  90. For c=1 To 10
  91.         m:mytype1=New mytype1
  92.         m.anint=c
  93.         m.afloat=Sin(c)
  94.         m.astring=String(c)+"!"
  95.         If c Mod 2 = 0
  96.                 m.anotherobject=New mytype2
  97.                 m.anotherobject.anotherint=c*2
  98.         EndIf
  99.  
  100.         mylist.addlast m
  101. Next
  102.  
  103. 'Print created data
  104. printmytype1list(mylist)
  105.  
  106. Print "///SAVING///"
  107. f:TStream=OpenFile("save.txt",0,1) 'open save file, writeable
  108. savelistofmytype1(mylist,f) 'save list of objects
  109. CloseFile f 'close file
  110. mylist=Null 'get rid of list
  111. FlushMem 'make sure it's gone :)
  112.  
  113. Print "///LOADING///"
  114. f:TStream=OpenFile("save.txt",1,0) 'open save file again, readable
  115. mylist:TList=loadlistofmytype1(f) 'load list in again
  116. CloseFile f
  117.  
  118. 'Print loaded data
  119. printmytype1list(mylist)


Comments :


Matt McFarland(Posted 1+ years ago)

 Thanks Warp!


deps(Posted 1+ years ago)

 Thanks a lot. This might come in handy sometime soon.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal