How do I get text from a txt file?

  • I'm trying to build a quiz game, and I want to have to have the questions in a separate txt document, and have a system that chooses the question based on, for example, a number at the start of each question. Either way, I'm trying to figure out how to get text from a file either local on the computer, of from a server, but not sure how?

  • There's a fairly nice example of the Json object in the templates.

    Might as well learn how to use those right?

  • Use Project Files. Text files aren't really a good format - an array, dictionary or JSON would probably be more suitable.

  • Use Project Files. Text files aren't really a good format - an array, dictionary or JSON would probably be more suitable.

    But I need to be able to edit the file outside of the contstruct editor. Like, I wanna export a project that points to a file in the same folder as the project.

  • Project files work exactly like that.

    You can edit a Json file in any text editor, just so long as it doesn't do any extra formatting, and you save as the same type.

    Of course you will have to re-import files you edit externally.

  • I fear that you're both talking of different things - and to be honest, I don't think you've solved Xeeko's problem. I also think it is basically the same as my own, so let me try and clarify my issue:

    I need to use local .txt files in my projects that keep changing by other apps accessing them.

    Simply put, I need to access local .txt files that are not embedded in the project whilst in preview mode. How would you do that?

    -Would real-time importing a file to the project be a solution? How do you do that?

    I had a couple of projects in Construct 2 where I was able to load the contents of a TXT file in preview, using NWjs or AJAX.

    This was easy and functional in Construct 2 - you had several ways of importing a file and read its contents. Now because of the way Construct 3 works (especially on preview that runs on "https://preview.construct.net", instead of localhost), it seems to fire up CORS policies - and it obviously won't access any files on the "construct.net" "folder".

    I also tried exporting the project but the embedded project txt files don't show up in the export folder at all. How would I edit them with an external editor?

    Please disregard security concerns or efficiency - this is for local usage in my own computer where no one else has access to - and my disks are fast enough for what I need.

    Please help!

    Thank you for your support!

  • You can host text files on a different web server, and configure CORS to allow it to serve them to your project in preview mode. The AJAX manual entry covers this.

  • Thank you for your reply - I understand what you're saying, but what about files on my hard disk? What would be the best way to access them without using embedded project files?

    How could I build an offline standalone NWjs game with savefiles on my local hard drive, that I can edit or share with my friends?

  • vila4480 You can access files from a fixed location on your hard drive with NWJS.ReadFile() expression. You'll have to use the Desktop app to preview your project if you need to use NWJS features.

  • vila4480 You can access files from a fixed location on your hard drive with NWJS.ReadFile() expression. You'll have to use the Desktop app to preview your project if you need to use NWJS features.

    Thank you for your reply dop2000 - I am indeed using the desktop app and NWjs.Readfile(), but I'm getting a non-disclosed access error while in preview. I've tried exporting (which takes a long time between versions) as NWjs and it does work - but the preview mode just doesn't seem to be able to access local files at all.

    Agreeing with Ashley, I'm thinking that would likely be a CORS issue, since the preview runs under "https://preview.construct.net" instead of localhost (like construct 2 used to run).

    So my question is how should I set it up so that I can access my local files in preview mode without having to run a private server with special CORS policies set.

    I'm sorry if it feels like I'm insisting on this topic, but so far I haven't seen any answers that solve the issue of working with local files in preview - and since this wasn't an issue in Construct 2, I'm having a hard time accepting that it isn't possible in Construct 3. There's got to be a way ..right?

  • I doubt it has anything to do with CORS. Maybe NWJS has no access to the folder you are using. Try "My Documents" or something similar. Also, have you checked error messages in the console log?

    This very simple code works fine for me:

  • I doubt it has anything to do with CORS. Maybe NWJS has no access to the folder you are using. Try "My Documents" or something similar. Also, have you checked error messages in the console log?

    This very simple code works fine for me:

    Thank you dop200 That's very useful!

    I made a very similar example code and it doesn't work for me at all. I've tried using paths with no particular user access rights and no spaces in the path. I wonder if there's something wrong with my desktop app permissions then...

    I'll have to look into this in a few hours and I'll report back.

    Thank you dop2000 and Ashley!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you run a local web server, it will serve files from your local hard disk. So that solution already covers the case of serving local files. You just have to move them in to the folder that the web server serves.

  • I doubt it has anything to do with CORS. Maybe NWJS has no access to the folder you are using. Try "My Documents" or something similar. Also, have you checked error messages in the console log?

    This very simple code works fine for me:

    This is what I'm getting... I have no idea what it means though...

    Thank you Ashley, I will try to learn a bit more on how to run a local webserver + C3.

    Unfortunately I'll be away from keyboard for this week, but I'll pick this up again once I come back.

    -Thanks guys I really appreciate your help!

  • It looks like you are running Construct in Chrome browser. You need to use the Desktop App:

    construct.net/en/forum/construct-3/general-discussion-7/construct-nw-js-desktop-app-131317

    NWJS actions will not work in a browser.

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