How to bring up external .txt files

So, I am trying to break the 4th wall in my game. I have a .txt file that has a letter that correlates with the game but is talking to the player. Is there any way to bring that up mid game, maybe through an event or something?
If you have a text file named file.txt in the root folder, use this script call to read the file and store it in a variable:
var fs = require('fs');
$gameVariables.setValue(1, fs.readFileSync('file.txt','utf8'));

Then you can show it in a message using the variable escape code. \V[1]

1592035953554.png


1592035730683.png

1592035937028.png
If you have a text file named file.txt in the root folder, use this script call to read the file and store it in a variable:
var fs = require('fs');
$gameVariables.setValue(1, fs.readFileSync('file.txt','utf8'));

Then you can show it in a message using the variable escape code. \V[1]

View attachment 147515

View attachment 147510
View attachment 147514
Thank you so much!
Just to make you aware, the current solution will only work on desktop environments, playing your game in the browser will throw an error because var fs = require('fs'); will not work in the browser. An alternative solution would be to use fetch, or XHR.

JavaScript:
fetch(`../mytext.txt`)
  .then(response =>
    response.text().then(text => {
      $gameVariables.setValue(1, text)
    })
  )

  .catch(err => console.error("Unable to read text file", err));
Sandbox Link: https://codesandbox.io/s/fetch-text-6m93i
@LTN Games Didn't know that. Thanks!
Also note that if using the fetch method, you may need to add a Wait command for it to work correctly. Otherwise the message will show before the file has loaded and use the old value of the variable.

Or if you actually are deploying to web, you should probably loop until it's ready, like this (untested, uses the same script code LTN Games posted):

Code:
◆Control Variables:#0001 = ""
◆Script:fetch(`../mytext.txt`)
:Script:  .then(response =>
:Script:    response.text().then(text => {
:Script:      $gameVariables.setValue(1, text)
:Script:    })
:Script:  )
:Script:  .catch(err => console.error("Unable to read text file", err));
◆Loop
  ◆Wait:60 frames
  ◆If:Script:$gameVariables.value(1) != ""
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above
◆Text:None, Window, Bottom
:Text:\V[1]
Also note that if using the fetch method, you may need to add a Wait command for it to work correctly. Otherwise the message will show before the file has loaded and use the old value of the variable.

Or if you actually are deploying to web, you should probably loop until it's ready, like this (untested, uses the same script code LTN Games posted):

Code:
◆Control Variables:#0001 = ""
◆Script:fetch(`../mytext.txt`)
:Script:  .then(response =>
:Script:    response.text().then(text => {
:Script:      $gameVariables.setValue(1, text)
:Script:    })
:Script:  )
:Script:  .catch(err => console.error("Unable to read text file", err));
◆Loop
  ◆Wait:60 frames
  ◆If:Script:$gameVariables.value(1) != ""
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above
◆Text:None, Window, Bottom
:Text:\V[1]
I wasn't planning on making it for browser but thanks for the advice. I didn't know about this method
I have a few questions.
Is it possible to do that without the whole variables thing?
Where exactly do I put my text files?
Is it possible to display dialogues like that? If so, how would it look?
No you need variables. You need to store the contents of the file somewhere before you use it and a variable does that.
You can save the text file antwhere you want. You just change the file path in the script according to it's location.
You can do some dialogue like in my example but not all of them. It'll be too complex in that way.
It'll be too complex in that way.
Too bad 🙁

@KotoYama, please refrain from necro-posting in a thread. Necro-posting is posting in a thread that has not had posting activity in over 30 days. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-bring-up-external-txt-files.122799/