trying to make text go backwards problem

Hello!!

I think this is my first actual post in this forums so.....that's fun ^^:

Anyway, I have a problem with some javascript that I've been working on. I'm still a bit of a newbie when it comes to this language so please bear with me.

So I'm trying to find a way to make it so that when the player presses a certain button, they can 'reverse' the text and go back to previous text boxes. It's kinda like in Phoenix Wright where in the testimonies you can go either backwards and forwards in text. Anyway, the code I wrote for trying to do this is here:

Spoiler
var Text = new Array;
var Recycling = new Array;
var part;
function showText() {
var textObject;
for(i = 0; i < Text.length; i++){
if(!$gameMessage.isBusy()){
textObject = Text;
$gameMessage.setFaceImage(textObject.faceImage, textObject.faceImageNumber);
$gameMessage.setBackground(textObject.background);
$gameMessage.setPositionType(textObject.position);
$gameMessage.add(textObject._text);
$gameMessage.newPage();
Text.splice(i,1);
}
}
}
function addToMessegeArray(index,contents){
if(Text.length < index + 1){
Text.push(contents);
Recycling.push(contents);
part = index;
}
}

function Backwards() {
if (part != 0)
part--;
Text.push(Recycling[part]);
showText();
}
var DV_Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
DV_Scene_Map_update.call(this);
if (Text.length>0) {
showText();
}
};
var Nik_Message_Update = Window_Message.prototype.update;
Window_Message.prototype.update = function() {
Nik_Message_Update.call(this);
if (Input.isTriggered('shift')){
Backwards();
}
}




Basically what I'm trying to do with this is make a list of text inside the text window and adding it using AddToMessageArray to simulate text. You can see it here:
Spoiler
index.php

That part is working fine. The part that's really tripping me up is the trying to go backwards part. According to the code, it should go back whenever I press 'shift'. The problem that I'm having is that it's not really going back correctly.


Here's what happens when I press enter for the first time:
Spoiler
index.php

Here's the text when I do it the second time:

Spoiler
index.php


Third time pressing enter:

Spoiler
index.php

Here's what happens when I press "Shift" at this moment:

Spoiler
index.php
​



It goes back one and displays the text. However, when I press shift again, nothing happens. I have to press enter again and then press shift afterwards in order to go fully back to 'hi'. Is there anyway I can just press shift and it'll go straight to the previous text? Thank you in advance for anyone and if I wasn't clear on anything, please tell me. Again, first time. Probably missed something ^^;
​
Naturally it gets back one test before showing a text, because that's what you told it to do.
Code:
function Backwards() {
if (part != 0)
part--;
Text.push(Recycling[part]);
showText();
}
I would say that the part--should be one line lower. Btw. if also uses curly brackets with the code.
Also, it does it like this, because $gameMessage uses a Wait type "message" when it is active, which means it won't do anything until you hit enter. You need to use either a custom window or edit the core functiobality.
Naturally it gets back one test before showing a text, because that's what you told it to do.
Code:
function Backwards() {
if (part != 0)
part--;
Text.push(Recycling[part]);
showText();
}
I would say that the part--should be one line lower. Btw. if also uses curly brackets with the code.
Also, it does it like this, because $gameMessage uses a Wait type "message" when it is active, which means it won't do anything until you hit enter. You need to use either a custom window or edit the core functiobality.



thanks for the help!! With that I was able to get like 90% done with this!! There's only one thing I'm having a bit of trouble with:

Here's the code I wrote that makes a custom window and adds text:

Code:
var Text = ['Hello' , 'hi' , 'idk'];
var part = 0;
(function() {
    var _Scene_Map_start = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        _Scene_Map_start.call(this);
        this._myWindow = new myWindow(50,50);
        this.addWindow(this._myWindow);
    };
    var _Scene_Map_Update = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function() {
        _Scene_Map_Update.call(this);
        if (Input.isTriggered('right')){
            part++;
            if (part == Text.length){
                $gameScreen.startFadeOut(24);
                part = 0;
                Game_Interpreter.prototype.wait(60);
                $gameScreen.startFadeIn(30);
            }
        }
        else if (Input.isTriggered('left')) {
            part--;
            if (part < 0){
                part = 0;
            }
        }
    };
    function myWindow(){
        this.initialize.apply(this,arguments);
    };
    myWindow.prototype = Object.create(Window_Base.prototype);
    myWindow.prototype.constructor = myWindow;
    myWindow.prototype.initialize = function(x,y){
        Window_Base.prototype.initialize.call(this,x,y,this.windowWidth(),this.windowHeight());
        this._value = -1;
        this.refresh();
    };
    myWindow.prototype.refresh = function() {
        if (this._value != part) {
            this._value = part;
            this.contents.clear();
            this.drawText(Text[this._value],0,30,this.windowWidth(),'left');
        }
    };
    myWindow.prototype.windowWidth = function(){
        return 300;
    };
    myWindow.prototype.windowHeight = function(){
        return 200;
    };
})();

The two biggest problems is that it only works when the function is self-executing. I wanted to have a little more control on when I can call this function, but it doesn't seem to work. Any tips on making me able to turn this on/off when I need to call it? The other sorta linked problem is that the text array has to be in the actual code. And you can't add anything or subtract anything in game. Anyway to make that work? Thanks in advance :3.
Before you call addWindow on that window, set its visible to false.

If you want more control, you need the scene map update function to handle everything in regards to that window (except for creation), because that is the function that gets fired every frame. So now set in the window a condition, which will if true show the window and show the text you want to show, if false, nothing happens.
e.g.
if (switch 1 is on)
window.refresh()
Text.pop()
visible = true;

And then edit the window's refresh function. Instead of this weirdness
this.drawText(Text[this._value]
(weirdness because -1 is not a valid argument in js for arrays. In python yes, but not in js.)
use this.drawText(Text[Text.length() - 1]

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-make-text-go-backwards-problem.95254/