How do I save and load data files on runtime?

0 favourites
  • 11 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • 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?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ok, found some docs on Array saving and managed to save the data, but when I try to load it like this nothing happens:

    [attachment=0:2coptqc3][/attachment:2coptqc3]

    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?

  • Anyone? This has really stumped me, unfortunately, cannot really find a workable solution by searching.

  • 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

  • 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:

    [attachment=0:20qjag99][/attachment:20qjag99]

    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?

  • 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?

  • Awesome, glad to have helped by proxy. Thanks for the link, sounds like this might help.

  • 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.

  • 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.

  • 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.

  • Wow, I have been looking for game data save / load solution all week! I don't like the idea of webstorage - it sounds too temporary to me. I'll test it out though. I didn't know about FileChooser though.

    I don't really want the user to choose a file - I just need to be able to save several files in the root of the game folder - perhaps in a 'LevelPack' folder. I'm kind of working towards a good solution.

    Your topic helped. Thanks.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)