Save a TYPE array object

Previous topic - Next topic

MrPlow

Hi All,

Been a while - I need to get back into my coding...
I am looking for a way to SAVE a TYPEs with all the entities etc.

Is there a handy way to do this or do I have to custom save for each specific TYPE, etc.

E,g,
TYPE
a%=5
b%=8
name$="Hans"
ENDTTYPE


So a method to iterate through the TYPE and save in format and load back?

I expect someone else might have cracked this before?

Any ideas?
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

bigsofty

I don't think it's possible, GLBasic would need a reflection system, which it does not have obviously.  :S

Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Schranz0r

You have to code it by your own.
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

The information is there. See the gpc_temp.h file. Each type has a dbg() function. In what format would you want to store the data? Consider you also have arrays.

MrPlow

#4
Thanks - for it to work well, it would be great to save the data in a fixed structure to have assurance that the order is kept.

I am think mostly of the large arrays created as TYPEs...

With Each type object array, each item under the type would be saved so that the key/name of the item was known and could be easily verified when reloaded?

Perhaps if a fixed index of the items for the arrays is obtainable - that might work?

I will look into the dgb() thing too - I am unfamiliar with that right now...

Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

MrPlow

Another approach could be to save entire variable memory data?
Is that possible?

As game saving states is hard to implement for GLB - I think.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

nabz32

Write a save function in your type,
This function writes all the variables of the type
Then call this function n times for the size of the array you want to save.
If n is variable than store n before saving the n lines of data.
This way you know the array size for the loading algorithm, or you could save a special
ending block after the array has been saved to determine the array ending.

When saving subtypes just call the save function from this subtype inside the save
function of the parent type.
Of course a load function wouöd be needed as well

MrPlow

Thanks guys,

I'll try some of these suggestions - and will share my final solution...
it wont be earth-shattering or anything...just suitable to save data from TYPES and load data back into into TYPES.


Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

hardyx

#8
I propose a solution based in Java class saving/loading, adapted to GLBasic TYPEs. Each type knows how to manage their data and a parent type (like an array or list), calls the loading or saving functions of the elements. Add functions to your TYPEs to save/load a single element to the file, and do the saving/loading loop in the parent list TYPE. Open the file from the container TYPE or array and pass the file channel to each element of the list.

Code (glbasic) Select
TYPE MyType
  a%=5
  b%=8
  name$="Hans"

  // assign properties
  function Init: _a%, _b%, _name$
      self.a = _a
      self.b = _b
      self.name$ = _name$
  endfunction

  // write element to file
  // file must be opened for write
  function SaveToFile: file%
      writeword file, self.a
      writeword file, self.b
      writeline file, self.name$
  endfunction

  // load the element from file
  // file must be opened for read
  function LoadFromFile: file%
      readword file, self.a
      readword file, self.b
      readline file, self.name$
  endfunction

ENDTTYPE

// declare list of TYPEs
local mylist[] as MyType
dim mylist[3]

// fill mylist with elements
mylist(0).Init(1, 22, "Fist")
mylist(1).Init(2, 33, "Second")
mylist(2).Init(3, 44, "Third")

// save the list to a file
Openfile(1, "mylist.dat", FALSE)

For elem in list[]
   elem.SaveToFile(1)
Next

Closefile 1


MrPlow

Thanks Hardyx

That looks like a possible solution for me.... :good:
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs