How do I save and load data files on runtime?

Get help using Construct 2

Post » Fri Jan 02, 2015 5:39 pm

Hello, all.

I would like to save and load/parse files with a data structure similar to this:

Frame, X, Y, W, H, Angle - so basically all integers, like this:

0 256 256 64 -32 0

And there would be a bunch of them, say 100 or so.

They need to be parsed in the correct order, but there's no need to access any specific lines or such. So the system would do a For each on some objects, add data on each to a file and then save that as a file on the users system (as a system download).

Same should happen in the opposite order.

Can this be done in runtime and what would be the best way to do it?
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Fri Jan 02, 2015 9:13 pm

Ok, found some docs on Array saving and managed to save the data, but when I try to load it like this nothing happens:

Untitled.png


On the bottom is a file chooser plugin - on change it should load the array in JSON format, but nothing happens? What could be the solution?
You do not have the required permissions to view the files attached to this post.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Sat Jan 03, 2015 6:57 pm

Anyone? This has really stumped me, unfortunately, cannot really find a workable solution by searching.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Sun Jan 04, 2015 6:52 am

FileChooser.fileUrlAt(0) is just a file name. You need the contents of the file which you can get with Ajax.

https://www.scirra.com/manual/182/file-chooser
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Sun Jan 04, 2015 8:55 am

Thanks, @R0J0hound! If you ever need some artwork (icons, objects) or, perhaps, if you accept donations let me know.

I previously put an AJAX object in, BUT didn't add the "on completed" action so it never returned usable results. Now it works! I can actually save and load my arrays and then process them to create objects, which is very cool:

ScreenCapture_3.gif


I'll write down the necessary steps "for dummies" (like me) if someone searches for something similar in the future:

Saving:
In my case the data is an array so saving is easy - just have a "Download as JSON data" action.

For loading:
Place a FileChooser and AJAX objects in your project. The Filechooser acts like an actual button so place that on the layout. It also has some properties, like are multiple files possible and what filetypes to accept.

In the event sheet:
Add a FileChooser "On Changed" condition of AJAX - Request Url - FileChooser.FileURLAt(0) "LoadFile" <== This is your tag, to know where the file comes from

Add an AJAX "On 'LoadFile' completed" condition with an Array - Load from JSON string - AJAX.LastData

After this add some actions to process your array.

Voila! File saving and loading.

---

Extra question, if I may - is there a way to replace the ugly Load button with something else? It's an eyesore and it gets shifted around when zooming in and out in the browser. Ideally there would be a way to shift it off-screen and trigger from another sprite button, but I suspect security measures won't let that happen?
You do not have the required permissions to view the files attached to this post.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Sun Jan 04, 2015 10:13 am

Somebody wrote:Voila! File saving and loading.

Wow! Thanks man, i searched for saveload via files system so long, but not for arrays, for simple c2's saveload system, now it works!

Here's an answer for your extra question i guess - How do I cover the File Choose button with a sprite?
B
22
S
9
G
7
Posts: 421
Reputation: 6,543

Post » Sun Jan 04, 2015 10:45 am

Awesome, glad to have helped by proxy. Thanks for the link, sounds like this might help.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Sun Feb 08, 2015 1:40 pm

@Somebody, I hope you don't mind me asking a bit cause I am really stumped with Json a bit. Is the only way to save a Json file to use the download option? It opens a new window and ask to save the file. I am making a mobile game so that is out of the question. What is the other option to automatically save it? I am using it to save a 100+ array. Thanks.
B
45
S
23
G
31
Posts: 691
Reputation: 20,608

Post » Sun Feb 08, 2015 1:46 pm

I think you should look into using webstorage (instead of making the browser download it). That would take care of seamless saving. In my case I needed a tangible file so this approach was used.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Sun Feb 08, 2015 1:54 pm

I have been using webstorage all this while, the problem is I have over 100 values in the array, and I am not sure if there is a way to enter all the values into the webstorage more effectively than using (Array.At(0)&","& Array.At(1)&","&Array.At(2)&","& Array.At(3)....etc etc) Someone mention that Json could do this, which was why I am looking into it. Haha might need to turn back again.
B
45
S
23
G
31
Posts: 691
Reputation: 20,608

Next

Return to How do I....?

Who is online

Users browsing this forum: FacuLNF and 63 guests