Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Loading and Saving JSON files?

Author
Message
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 19th Mar 2019 12:02 Edited at: 19th Mar 2019 12:09
Hi,
Has anyone been able to get the loading and saving of JSON files working?

I have learned that whilst you can create a JSON string from a UDT you can't save it unless it is in an array. So to save a JSON UDT this works:

I know that works because I can open the resultant JSON file in a text editor and view it. But that is nothing like what it says in the help files.

As for loading it back in again, I have on idea!
I've tried every combination of arrays and UDT loading that I can think of but nothing works.

Anyone?
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Mar 2019 12:21 Edited at: 19th Mar 2019 13:51
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



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.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Mar 2019 16:56 Edited at: 19th Mar 2019 16:57
You cant use .load() with a single instance of a UDT....it has to be an array. Even if the array itself only has 0 or 1 members then .load() and .save() can be used. .load() and .save() only work with arrays of UDT's

Otherwise if it a single UDT then .tojson() and .fromJson() are the only options

Basically....

a as UDT[] -> you can use .load() and .save()

a as UDT -> you can only use .tojson() and .fromJSON()
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Mar 2019 12:23
Thanks Bengismo,

I've gone with the to/fromJson and the writing/reading a string from file.
It seems a shame that there are load() and save() commands that we simply can't use but at least this seems to.


blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 20th Mar 2019 19:59 Edited at: 20th Mar 2019 20:13
Sorry. My bad

Login to post a reply

Server time is: 2024-03-28 20:57:02
Your offset time is: 2024-03-28 20:57:02