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?
I don't think it's possible, GLBasic would need a reflection system, which it does not have obviously. :S
You have to code it by your own.
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.
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...
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.
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
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.
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.
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
Thanks Hardyx
That looks like a possible solution for me.... :good: