How do I make "download as JSON" work when exported as desktop app (NW.js) ?

0 favourites
  • 14 posts
From the Asset Store
The internet is bombarded daily with new apps (app is the short term for application)
  • Hello everyone,

    I am very new to programming and found myself stuck with a problem I am sure you will be able to help me solve in no time.

    As a PhD student in linguistics, I am not exactly making games but little programs that help me in my research.

    The one I am having trouble with is a "Japanese -ru verb generator". In short, it creates random (non) verbs ending in "-ru" in Japanese while respecting the basic phonotactic rules and phoneme frequencies of the language.

    The good thing is: everything works just fine on my computer when I am previewing from Construct2. I may not have programmed everything in the most elegant way, but it works ! It generates the verbs, saves everything I want to save on 5 dictionaries (the list of verbs and some of their basic inflections), and downloads those dictionaries as JSON when I click the right button.

    However, I want to share my program with some colleagues. Since I could not find a way to upload it on a server (again, I am quite a noob), I thought the best option was to export it as a desktop app. So I downloaded and installed the NW.js support to do that. But if I run the game from the desktop app, when I click the "save" button, the downloading of the dictionaries doesn't work. Instead, 5 blank pages pop up (one for each dictionary).

    What did I do wrong ?

    By the way, here is the .capx file :

    drive.google.com/open

  • It works for me, but I can only download the first dictionary.

    You can try adding Browser object and do "Browser invoke download of string", with string DictionaryName.AsJSON

    .

    Also, saving 5 dictionaries one by one is pretty annoying. You need to combine them into one string with line breaks between them, and then save that string.

    Text Variable s
    Set s to Dict1.AsJson & newline & Dict2.AsJson & newline & ....
    Browser invoke download of string s
    

    If you need to load dictionaries from the saved file, parse it using tokenat expression:

    Dict1 load from JSON string tokenat(s, 0, newline)

    Dict2 load from JSON string tokenat(s, 1, newline)

  • Oh yeah !! It worked ! Thanks a lot !

    However, I did not manage to get everything into one file. As you said, it would be a lot more convenient. Actually I can get the file as JSON but when I try to open it on Firefox I get this error message :

    "SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data"

    The problem appears only when I try to combine several dictionaries. It works fine if I download them one by one, which is already great.

    Here is the new .capx, the file that does not work for me is the "FullList.json" :

    drive.google.com/open

  • Well, I didn't know why did you need to export those dictionaries. I thought you want to be able to load them back into the game.

    If you're saving them to use in some other application, then, I suppose, you have to save them all separately.

  • Oh ok.

    Indeed, I don't need to load them back. I'm directly using the data in the dictionaries for my research. In short, this program helps me to create stimuli for a linguistic experiment, all I need in the end is an excel file with the list of verbs I created and their inflections.

    So now I just download them separately, open them in firefox and copy/paste in excel. That works just fine even if that may not be the most convenient way to do it.

    So thank you very much for your help !

  • You can create and save a CSV file in Construct. CSV is basically a list of values separated by comma and Excel can open it easily.

    So you can do something like this:

    Dict1 for each key
     -> Set s to (s & Dict1.CurrentKey & "," & Dict1.CurrentValue & newline)
    
    Dict2 for each key
     -> Set s to (s & Dict2.CurrentKey & "," & Dict2.CurrentValue & newline)
    
    ...
    

    Then save s in a text file with .csv exctension.

  • It works ! But the Japanese characters won't appear. I get weird symbols instead... any idea why :D ?

    By the way, I had no idea you could create all kinds of files with construct 2. That's really good to know !

  • Instead of opening CSV file in Excel, create a blank document and import CVS into it. On the ribbon click Data-> From Text/CSV

    In the data import dialog there will be an option to choose encoding, select "Unicode UTF-8"

    Also, in "Browser invoke download" action set MIME type to "text/csv"

    .

    By the way, you can actually create files without user interaction. Add NW.js object to your project and use "NWjs Write File". For example, you can ask user to pick a folder ("NWjs Show Folder dialog") and then export multiple files into it with one click.

  • Yes that works ! Thanks ! I found that solution too, but I couldn't edit my post for some reason...

    Anyway. What you say about creating files without user interaction is very interesting for another project. But I'll ask my question pretending I am going to use my Japanese verb generator instead :

    What if I want to do an online survey? I export my program as HTML5 and upload it to a server, and in the end I want the .csv file to be saved on the server, and not on the user's computer. Can I do that ? The solution you described seems to be NW.js specific. So it wouldn't work on a server, right ? But I am sure construct 2 has a way to do it :D

  • I'm not sure about saving files on the server, but you can save survey answers into a database.

    scirra.com/tutorials/346/online-high-score-table-ajax-php-mysql

    scirra.com/tutorials/4839/creating-your-own-leaderboard-highscores-easy-and-free-php-mysql

    Another option is to use Firebase:

    scirra.com/tutorials/5015/getting-started-with-firebase

    I also recently completed a little project where data from Construct 2 was sent to a Google Spreadsheet. If you want, I can explain how 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
  • Hum, for once it sounds more complicated than I would have expected.

    I would have thought that the AJAX object could post a file to a URL in some way...

    Guess I'll have to look deeper into that ! Thanks.

  • You can probably post a file with AJAX, but when I tested it didn't work. Maybe something was wrong with permissions or incorrect headers or something like that..

  • Oh... too bad it did not work. Can you tell me what you tried ? If it's just a matter of permissions, maybe I can get around that ?

    Anyway, since my verb generator is done (thanks to your help) and those questions about servers are unrelated, I made a new topic on my other project with more detailed questions. If you want to have a look at it : construct.net/forum/construct-2/how-do-i-18/how-do-i-save-a-csv-file-to-my-137535

  • Well, I tried "AJAX Post to URL" action with the following settings:

    URL: "test.txt"

    Data: "some data"

    Method: "POST"

    I exported and uploaded the project onto the server, set folder permission to 777, but when I run it, the file is not created.

    If I create the file manually in this folder, I receive its content in AJAX response, but still can't write to it.

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