MV Reading specific lines of text file.

Some time ago I wrote a plugin that allows me to load text from external text-file, but now I want to improve it a little more by reading specific range of lines, take a look at fragment of scenario file how it looks like, it's written in specific format:

ActorName:
Dialog to show, max 5 lines

Spoiler
Code:
/*** Interlude ***/

Tallanar:
Where have you been!? I've been calling for you for five
minutes and you not showed up!

Soulrender:
What's it been, five minutes? What'd you expect, you call
and get instant God?

Tallanar:
Well, something like that...

Soulrender:
Me not coming is going to happen a lot. My responsibilities
extend to all of this cursed island... Where is Telthos?

Tallanar:
He got up early and left. I don't know where he went or when
he'll be back. And frankly, I don't care.

Soulrender:
Okay... There's a story in here somewhere.

Tallanar:
He did something stupid...

Soulrender:
Great! I love it. Tell me.

Tallanar:
He was attacked by four men and instead of
calling for help, he almost gets killed.

Soulrender:
Almost? What a pity. Oh, nothing against Telthos, it's just that, um...
stories are always better when someone gets killed.

So I wanted to ask does node.js in default form - provided as is with MV can handle such request, or do I have to install some extra libraries?

This is code that loads entire text.
Spoiler
JavaScript:
Dialog.prototype.readFile = function(filename){
    var xhr = new XMLHttpRequest();
    xhr.async = false;  
    try{
        xhr.open("GET", filename, false);
        xhr.send(null);
        var fileContent = xhr.responseText;
        return fileContent;  
    }catch(e){
        console.log(e.stack);
    }

Dialog.prototype.getDialouge = function(sourceFile, index){
    var src     = "../data/proto/dialouges/map%1/%2_%3.txt".format($gameMap._mapId.padZero(4), sourceFile, index.padZero(4));
    var data     = this.readFile(src);
    return data;
}
}

Of course I could adjust line breaks within text file to number of rows in message window and use Batch Entry function in MV editor like this

Screenshot_1.png


But that will block me possibility to add other actions between line dialouges, making just static text exposition, witch is I want to avoid.


Ok so be more precise I want to load a range of lines from txt file
When dealing with compact data like text it's often practical to just load the entire file into memory and index the lines from there. 😮‍💨

Otherwise: the system accesses files based on disk/cluster location. If you want to read only part of a file, you'd need to know the byte offset from the start of the file. Even with that information (e.g. fixed/padded line lengths or header data), I think that by default XHR requests are limited to stuff like "file" or "header". You'd probably need a custom server setup (if the game is hosted online) and/or Node's local file access methods, e.g.

PS: it's spelled "dialogue", or "dialog" in American English. 🙏
5 mins and now I get it, you @caethyril would be great teacher.
Hello again, sorry I'm not very good at programming. But I thought, what if I saved 5 lines at a time in an Array?

Example:
JavaScript:
/*Read*/
textFile="line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nlin8";

/*Split to Array*/
lines=textFile.split("\n");

dialogs = [];
for(i=0;i<lines.length;i++){
   if((i%5) == 0){
      /*Clear variable temporary*/
      _msg="";
   }
   if((i%5)==4 || i==(lines.length-1)){
      /* Last line */
      _msg = _msg+lines[i];
      /* Record */
      dialogs.push(_msg);
   }
   else
   {
      _msg = _msg+lines[i]+"\n";
   }
}

dialogs[0] return:
Code:
line 1
line 2
line 3
line 4
line 5

dialogs[1] return:
Code:
line 6
line 5
line 8
@Dev_With_Coffee. This is quite similar and good idea I was exploring few months ago, it worked well, but it has a major flaws, not all tags from yanfly's message core and it's extensions will work when I "explode" string into array, wich I use them very often and lot and when text file grows to large sizes ( > 512kb) there are visible lags when text is loaded... But for some - unknown to me - reason, loading entire text into memory, like @caethyril said earlier, allows me to store in text file notetags and engine parses them smoothly without problems.

Anyway, my problem for now is solved with odd way, but it works 🙂
Just for the record, it doesn't make sense to write each message with 5 lines using breaks, because you would have to make them Array and then read, it's more practical to write each line inside a new Array:
JavaScript:
/*Read*/
textFile="line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nlin8";

/*Split to Array*/
lines=textFile.split("\n");

dialogs = [];
for(i=0;i<lines.length;i++){
   if((i%5) == 0){
      /*Clear variable temporary*/
      _msg=[];
   }
   /* Last line */
   _msg.push(lines[i]);
   if((i%5)==4 || i==(lines.length-1)){
      /* Record */
      dialogs.push(_msg);
   }
}

The only thing is that it's pretty boring to use Array vectors.

( > 512kb) there are visible lags when text is loaded...
Therefore, you will need to use functions from NW.js itself just to read the specific line file:

Original thread: https://forums.rpgmakerweb.com/threads/reading-specific-lines-of-text-file.138400/