How do I read .json files from disk using NW.js ?

1 favourites
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hi!

    I'm having some trouble with NW.js functionnality.

    Basically, while the game is running, I want to import on demand a .json file (using a different string as a parameter to indicate the name of the file requested) and load it into a array.

    I've tested successfully the "open dialog" method (in which the user select which file he wants to import) but I want the same process to happen without having the user to select it (so no dialog box). I'm not sure I'm clear here but I am trying my best lol.

    I also know the AJAX function can import file but I feel it is pretty restricted because you have to manually select which file you want to import.

    tl;dr

    want to know if possible (and how in that case) if the game can import .json files ingame without user assistance.

    Thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Use AJAX Request URL, and put the path to json file into URL field. You need to set the preview browser to NW.js

  • Hi, do you want to read a JSON file from the disk?

    I am doing it without problems with NWJS, I simply use NWJS.readFile and get the JSON information from the file on disk.

  • Didn't know about ReadFile expression, it's much easier than requesting files with AJAX. Thanks!

  • Hi, do you want to read a JSON file from the disk?

    I am doing it without problems with NWJS, I simply use NWJS.readFile and get the JSON information from the file on disk.

    Yes I want to read the .json file from the disk and load its data into an array. The .json file is a array that was exported earlier.

    I'm not sure on where to use the expression. I've tried the NWJS.readFile expression with NWJS.appfolder into "load array" but without that doesn't seems to do anything.

  • Set s to NWJS.readFile("d:\temp\file.json")

    Or you can even load it directly into the array:

    Array load from JSON NWJS.readFile("d:\temp\file.json")

  • Interesting I was trying to do something similar no while ago and I couldn't make it work, I didn't know about the NWJS.readFile Thanks for the info.

    Still, I have a few problems when I connect the iPhone over wifi to the capx that request the NWJS.readFile("d:\temp\file.json") it doesn't work I tried many different ways but it always fails even I tried Ajax load from Url and still fails.

    dop2000 do you know of any working solution to read a file from Pc but while on Mobile Preview without using any external cloud service?

  • Set s to NWJS.readFile("d:\temp\file.json")

    Or you can even load it directly into the array:

    Array load from JSON NWJS.readFile("d:\temp\file.json")

    Using a newly created folder in d:\temp\ it worked! Thanks a lot!

    Although, using either NWJS.userfolder or NWJS.appfolder doesn't seems to work. Is there anyway

    without using hard-coded path? Like a subfolder from where the project is located instead?

  • tarek2 Path like "D:\..." will obviously not work on mobile. I guess, if you want to access the file from the local drive, you'll need to run some web or ftp server on your pc. Otherwise the easiest solution would be uploading it to the internet and requesting it with ajax.

    soviet shrek Try this:

    NWjs.ReadFile(NWjs.AppFolder & "test.json")

    In my case NWjs.AppFolder="C:\Program Files\NWjsForC2\win32\", so when I put test.json into that folder, it works.

  • Thanks dop2000

    I was just making sure that it wasn't possible to do, while back after I couldn't make it work someone showed me other online option as you said but I still had the curiosity if Pc-To Mobile will work with (NW.js or Ajax)

    Another Idea I had is to work and save directly on a JSON file already imported on C2 Files Folder, but I couldn't find the Path to save directly to that Json file inside c2 while you are on preview

    Example

    while you are on preview request download JSON and link to the Path where c2 has stored the JSON file and just replace it, do you think this is possible?

    Png to explain it more visual

    basically Request Download Json >>>saving directly to this folder while the game is running

  • Yeah, you can save your project as a folder (not as capx), and then you will be able to modify json file while the game is running in preview on mobile. If you request the file with AJAX again, it will load the updated file contents.

    Don't add any path to the file name. Use AJAX Request URL action, URL="Mobile_Test.json"

  • Yeah, you can save your project as a folder (not as capx), and then you will be able to modify json file while the game is running in preview on mobile. If you request the file with AJAX again, it will load the updated file contents.

    Don't add any path to the file name. Use AJAX Request URL action, URL="Mobile_Test.json"

    Weird the last time I been at it for very long and I give up because I couldn't get it to work I followed the same method that you described with the capx extracted on files, and today I tried again but this Time using Nw.js to save to the Json file with the Path Method you shared for subfolders and it finally worked, so it must be the Path that I was using probably wrong

    Awesome thanks dop2000 for your help :)

    Sorry Soviet shrek for hijacking your thread

  • soviet shrek Try this:

    NWjs.ReadFile(NWjs.AppFolder & "test.json")

    In my case NWjs.AppFolder="C:\Program Files\NWjsForC2\win32\", so when I put test.json into that folder, it works.

    Do you have to manually set NWjs.AppFolder to a folder or the program does it but itself?

    The NWjs.ReadFile(NWjs.AppFolder & "test.json") doesn't work but NWjs.ReadFile("hard-coded path" & "test.json") did worked. I also check for writing/reading permission but nothing to worry there. In the meantime of finding out a work-around, I've replaced the .appfolder expression by a global string variable that indicate my folder in case I change my project location. It works but I still have to declare a hard-coded path to my variable.

  • NWjs.AppFolder should be automatically set to your apps folder.

    Just output it to a text object or browser console (Browser Log NWjs.AppFolder) to see which folder it refers to on your pc.

  • Thanks for the ip dop2000 ! The folder wasn't my project one but in program files. Is there anyway to modify that?

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