GLBasic forum

Main forum => GLBasic - en => Topic started by: MrPlow on 2022-May-03

Title: Save a TYPE array object
Post by: MrPlow on 2022-May-03
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?
Title: Re: Save a TYPE array object
Post by: bigsofty on 2022-May-04
I don't think it's possible, GLBasic would need a reflection system, which it does not have obviously.  :S

Title: Re: Save a TYPE array object
Post by: Schranz0r on 2022-May-04
You have to code it by your own.
Title: Re: Save a TYPE array object
Post by: Kitty Hello on 2022-May-05
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.
Title: Re: Save a TYPE array object
Post by: MrPlow on 2022-May-05
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...

Title: Re: Save a TYPE array object
Post by: MrPlow on 2022-May-05
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.
Title: Re: Save a TYPE array object
Post by: nabz32 on 2022-May-05
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
Title: Re: Save a TYPE array object
Post by: MrPlow on 2022-May-06
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.


Title: Re: Save a TYPE array object
Post by: hardyx on 2022-May-13
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

Title: Re: Save a TYPE array object
Post by: MrPlow on 2022-May-16
Thanks Hardyx

That looks like a possible solution for me.... :good: