Author Topic: Save a TYPE array object  (Read 713 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Save a TYPE array object
« 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?
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: Save a TYPE array object
« Reply #1 on: 2022-May-04 »
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)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5116
  • O Rly?
    • View Profile
Re: Save a TYPE array object
« Reply #2 on: 2022-May-04 »
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

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Save a TYPE array object
« Reply #3 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.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Save a TYPE array object
« Reply #4 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...

« Last Edit: 2022-May-05 by MrPlow »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Save a TYPE array object
« Reply #5 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.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 323
    • View Profile
Re: Save a TYPE array object
« Reply #6 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

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Save a TYPE array object
« Reply #7 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.


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

Offline hardyx

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 504
    • View Profile
Re: Save a TYPE array object
« Reply #8 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
« Last Edit: 2022-May-13 by hardyx »

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Save a TYPE array object
« Reply #9 on: 2022-May-16 »
Thanks Hardyx

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