Loading it back is really easy with the .load() function ??
Also, You can save a single UDT but you must first convert a single UDT to a string and use write string for a single UDT
this code saves your UDT array as a JSON then loads it back
it also saves a non array UDT and loads that back
// Project: JSON
// Created: 2019-03-19
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "JSON" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
filename$ = "test.json"
filename2$ = "test2.json"
type spritetype
ID as integer
x as float
y as float
width as float
height as float
endtype
// Array version
MyType as spritetype[0]
MyType[0].ID = 1
MyType[0].height = 16
MyType[0].width = 16
MyType[0].x = 100
MyType[0].y = 200
// Single UDT version
MyType2 as spritetype
MyType2.ID = 2
MyType2.height = 32
MyType2.width = 32
MyType2.x = 400
MyType2.y = 300
// save using the array function
MyType.save(filename$)
// Also save a JSON file using the text function
opentowrite(1,filename2$)
WriteString(1,MyType2.tojson())
closefile(1)
// Create a new array which we can load the array into
NewType as spritetype[]
NewType.load(filename$)
// create a single type which loads from the text saved
NewType2 as spritetype
OpenToRead(1,filename2$)
NewType2.fromjson(readstring(1))
Closefile(1)
do
// Array version
print ("Array Version ")
Print(NewType[0].x)
Print(NewType[0].y)
Print(NewType[0].width)
Print(NewType[0].height)
Print(NewType[0].ID)
print (" ")
// Single UDT version
print ("Single UDT Version ")
Print(NewType2.x)
Print(NewType2.y)
Print(NewType2.width)
Print(NewType2.height)
Print(NewType2.ID)
Sync()
loop
I think the easiest way is pretty much as you have done it ....with an array with only 1 element in it. Then you can just use the .save() and .load() commands as long as you make all the data structures array even if they will only have 1 element. Otherwise you can save individual types with the .tojson() and .fromjson() commands.
The JSON commands are really great. you can populate entire game levels really quickly and save states really easily too. You can also just read the files in a text editor and make changes easily too. Its some of the best functionality in AGK.