MV Why is the window selectable item list scrolling the wrong way?(SOLVED)

I have spent some time figuring out how the selectable item list is working. Kinda got it working at this point. But for some reason, it is scrolling in reverse... If I start going down from HP to Luck it works just fine.
1637226395382.png

If I press down once more... The entire thing moves downwards.
1637226297623.png

Note that HP goes away, and judging by the length of whatever is below Luck, it should be the next item in queue.
1637226512814.png

While writing this, I realize that there seems to be some issue with the list not moving up, considering HP gets hidden and what is below Luck gets shown.

So any idea what might be missing?

Best regards,
we cannot know, do you have a update/refresh button for this function?
and the bottom line can be fixed if you adjust the window a tiny bit
if you use fixed sizes, otherwise, you need to check the list lenghts
and return to 8 max to show or whatever number you set.

to know where you got it wrong or miss something, we need to know
the function in order to help you fix the issue or oversight you have.

there are many good coders that can pinpoint to it.
I'm guessing you made an override for drawAllItems and/or refresh and made a mistake somewhere. You should generally only write drawItem code for custom selectable windows: the inherited methods should handle redrawing the visible items.

Otherwise, like ShadowDragon said, we'd need to see your code.
we cannot know, do you have a update/refresh button for this function?
and the bottom line can be fixed if you adjust the window a tiny bit
if you use fixed sizes, otherwise, you need to check the list lenghts
and return to 8 max to show or whatever number you set.

to know where you got it wrong or miss something, we need to know
the function in order to help you fix the issue or oversight you have.

there are many good coders that can pinpoint to it.
I don't think there is an issue with the size of the window. If the bottom item just moved up one step, it would be visible 🙂
I'm guessing you made an override for drawAllItems and/or refresh and made a mistake somewhere. You should generally only write drawItem code for custom selectable windows: the inherited methods should handle redrawing the visible items.

Otherwise, like ShadowDragon said, we'd need to see your code.
I haven't overwritten either refresh or drawallitems. I can't see where the error lies :/

Feels a bit rude to post an entire plugin, especially since I don't know if it's very cleaned up(If it is a problem, please tell me and I shall clean it up more). But I will do it, as I am certain that I have some simple mistake, or that I've forgotten something.
JavaScript:
var BISH = BISH || {};

//Scene setup ######################################
Scene_Battle.prototype.createAllWindows = function() {
    this.createLogWindow();
    this.createStatusWindow();
    this.createPartyCommandWindow();
    this.createActorCommandWindow();
    this.createHelpWindow();
    this.createSkillWindow();
    this.createItemWindow();
    this.createActorWindow();
    this.createEnemyWindow();
    this.createMessageWindow();
    this.createScrollTextWindow();
};

(function(alias) {
    Scene_Battle.prototype.createAllWindows = function() {
        alias.apply(this, arguments);
        try {
            this.createEnemyStatusWindow();
            console.log($gameTroop._enemies.length);
            for (var i = 0; i < $gameTroop._enemies.length; i++) {
                $gameTroop._enemies[i].setStatRegistry();
            };
        } catch(e) {console.error(e)};
    }
}) (Scene_Battle.prototype.createAllWindows);

Scene_Battle.prototype.createEnemyStatusWindow = function() {
    var wy = this._helpWindow.y + this._helpWindow.height;
    var wh = this._statusWindow.y - wy;
    var ww = this._actorCommandWindow._width * 1.5;
    this._enemyStatusWindow = new Window_EnemyStatus(0, wy, ww, wh);
    this.addWindow(this._enemyStatusWindow);
};

//Window_EnemyStatus ######################################

function Window_EnemyStatus() {
    this.initialize.apply(this, arguments);
}
Window_EnemyStatus.prototype = Object.create(Window_Selectable.prototype);
Window_EnemyStatus.prototype.constructor = Window_EnemyStatus;

Window_EnemyStatus.prototype.initialize = function(x, y, width, height) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._touching = false;
    this._enemy = null;
};

Window_EnemyStatus.prototype.update = function() {
    if (this._touching) {
        this.x = TouchInput._mouseOverX;
        this.y = TouchInput._mouseOverY;
    }
    if (this.getScene()._enemyWindow.enemy() && this._enemy != this.getScene()._enemyWindow.enemy()) {
        this._enemy = this.getScene()._enemyWindow.enemy();
        if (this._enemy.isEnemy()) {
            this._paramsLength = this._enemy.paramsName.length;
            this.refresh();
        }
    }
    Window_Selectable.prototype.update.call(this);
};

//Window_EnemyStatus Draw Enemy Status ######################################
Window_EnemyStatus.prototype.drawEnemyStatus = function() {
    var enemy = this.getScene()._enemyWindow.enemy();
    var paramsLength = enemy.paramsName.length;
    this.drawDarkRectEntries(paramsLength);
    for (var i = 0; i < paramsLength; i++) {
        this.drawEnemyData(enemy, i);
    }
};

Window_EnemyStatus.prototype.drawEnemyData = function(enemy, i) {
    var rect = this.getRectPosition(i);
    this.drawText(enemy.paramsName[i], rect.x, rect.y, rect.width, rect.height);
};

//Window_EnemyStatus Draw Dark Rects ######################################
Window_EnemyStatus.prototype.drawDarkRectEntries = function(numberOfRects) {
    var rect;
    for (var i = 0; i < numberOfRects; ++i) {
        var rect = this.getRectPosition(i);
        this.drawDarkRect(rect.x, rect.y, rect.width, rect.height, "left");
    }
};

Window_EnemyStatus.prototype.getRectPosition = function(index) {
    var rect = new Rectangle();
    rect.width = Math.floor(this.contents.width);
    rect.height = this.lineHeight();
    rect.x = /*index % 2 === 0 ? 0 : rect.width;*/ 0;
    rect.y = index *this.lineHeight();
    return rect;
};

//Items settup
Window_EnemyStatus.prototype.maxItems = function() {
    if (this._enemy) {
        return this._enemy.paramsName ? this._paramsLength : 1;
    } else {return 1;}
 
};

Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.getRectPosition(index);
    var statWidth = 96;
    rect.width -= this.textPadding();
    this.drawText(this._enemy.paramsName[index], rect.x + 4, rect.y, rect.width - statWidth);
    this.drawText(this._enemy.paramsValue[index], rect.x + rect.width - statWidth, rect.y, statWidth, 'right');
    this.changePaintOpacity(true);
};

//Window_EnemyStatus Touch Input ######################################

Window_EnemyStatus.prototype.processTouch = function() {
    if (this.isOpenAndActive()) {
        if (TouchInput.isTriggered() && this.isTouchedInsideFrame()) {
            this._touching = true;
            this.onTouch(true);
        } else if (TouchInput.isCancelled()) {
            if (this.isCancelEnabled()) {
                this.processCancel();
            }
        }
        if (this._touching) {
            if (TouchInput.isPressed()) {
                this.onTouch(false);
            } else {
                this._touching = false;
            }
        }
    } else {
        this._touching = false;
    }
};

Window_EnemyStatus.prototype.isTouchedInsideFrame = function() {
 
    var x = this.canvasToLocalX(TouchInput.x);
    var y = this.canvasToLocalY(TouchInput.y);
    console.log(x >= 0 && y >= 0 && x < this.width && y < this.height);
    return x >= 0 && y >= 0 && x < this.width && y < this.height;
};

Window_EnemyStatus.prototype.onTouch = function(triggered) {
    var lastIndex = this.index();
    var x = this.canvasToLocalX(TouchInput.x);
    var y = this.canvasToLocalY(TouchInput.y);
    var hitIndex = this.hitTest(x, y);
    if (hitIndex >= 0) {
        if (hitIndex === this.index()) {
            if (triggered && this.isTouchOkEnabled()) {
                this.processOk();
            }
        } else if (this.isCursorMovable()) {
            this.select(hitIndex);
        }
    } else if (this._stayCount >= 10) {
        if (y < this.padding) {
            this.cursorUp();
        } else if (y >= this.height - this.padding) {
            this.cursorDown();
        }
    }
    if (this.index() !== lastIndex) {
        SoundManager.playCursor();
    }
};

//Window_EnemyStatus Move Window ######################################
It's your own code, right? And you're asking for help with it? Are you saying it's rude for us to ask to see the code? I don't get it. 😅

Anyway, I see the following sections in your code:
  1. Scene setup​

    • Remember to remove the createAllWindows override right at the top.
  2. Window_EnemyStatus​

    • I'm not sure what you're trying to do with your update code. It's like: if the user is hovering this window, then move this window to the cursor's location (typically game windows are anchored by their top-left corner). But if this window is some kind of tooltip etc for another window, then the repositioning code should be on the parent object, not the tooltip. 😴
  3. Draw Enemy Status​

    • Try removing this section? Item drawing can be handled by drawItem, as you've done later.

    • I'm guessing this is what is messing things up, because:
      • It conflicts with the existing drawItem structure, and
      • It draws the data without reference to the window's topIndex.
        (See Window_Selectable.prototype.drawAllItems for details.)
  4. Draw Dark Rects​

    • Consider drawing the dark rectangles on a per-item basis, in the drawItem method. This is particularly relevant for the inherited redrawItem method, otherwise a single-item redraw will erase that item's dark rect. RMMV's core scripts do not have a drawDarkRect method so I'm not sure of the specifics there...if it's drawing the dark rects on a separate background layer then it should be OK as-is.

    • Like I said in my previous post, you can get the item rectangle using this.itemRect(index), so you might not need a custom getRectPosition method? If that method is only for the background/dark rectangles then consider renaming it to getDarkRect or something so its purpose is clearer.
  5. Items settup​

    • Looks OK! 👋
  6. Touch Input​

    • Remove this section as well? I'm guessing you just had it there for testing/logging because these methods should all be inherited from Window_Selectable.
It's your own code, right? And you're asking for help with it? Are you saying it's rude for us to ask to see the code? I don't get it. 😅
I think he meant he feels rude taking up so much space in the thread by posting the entire plugin. But it doesn't take up that much space, as the code tag adds a scroll bar.
@ATT_Turan is correct, I feel like it's a bit rude to post an entire thing like that and ask someone to go over it. I don't know why really, I wouldn't think it's rude if someone else would do it tbh 😛 It's commendable to ask for it though, so you're not rude at all, you're real nice for doing it 😀


It's your own code, right? And you're asking for help with it? Are you saying it's rude for us to ask to see the code? I don't get it. 😅

Anyway, I see the following sections in your code:
  1. Scene setup​

    • Remember to remove the createAllWindows override right at the top.
  2. Window_EnemyStatus​

    • I'm not sure what you're trying to do with your update code. It's like: if the user is hovering this window, then move this window to the cursor's location (typically game windows are anchored by their top-left corner). But if this window is some kind of tooltip etc for another window, then the repositioning code should be on the parent object, not the tooltip. 😴
  3. Draw Enemy Status​

    • Try removing this section? Item drawing can be handled by drawItem, as you've done later.

    • I'm guessing this is what is messing things up, because:
      • It conflicts with the existing drawItem structure, and
      • It draws the data without reference to the window's topIndex.
        (See Window_Selectable.prototype.drawAllItems for details.)
  4. Draw Dark Rects​

    • Consider drawing the dark rectangles on a per-item basis, in the drawItem method. This is particularly relevant for the inherited redrawItem method, otherwise a single-item redraw will erase that item's dark rect. RMMV's core scripts do not have a drawDarkRect method so I'm not sure of the specifics there...if it's drawing the dark rects on a separate background layer then it should be OK as-is.

    • Like I said in my previous post, you can get the item rectangle using this.itemRect(index), so you might not need a custom getRectPosition method? If that method is only for the background/dark rectangles then consider renaming it to getDarkRect or something so its purpose is clearer.
  5. Items settup​

    • Looks OK! 👋
  6. Touch Input​

    • Remove this section as well? I'm guessing you just had it there for testing/logging because these methods should all be inherited from Window_Selectable.
Thanks!

First off, thanks, I would've forgotten to remove the createAllWindows() function 🙂

So yes, I guess this is what I meant with cleanup :/
For example, draw enemy status and the dark rects aren't being used at all. They were from before I decided to make it into a selectable rather than a base window. I kept them, since I felt that perhaps, somehow, they would be interacting with the selectable. So it was mostly for safety, and I should have added a comment, clarifying that it is not being used at the moment.

This goes double for the marked part as it is just me playing around, trying to learn how to utilize the TouchInput. It's not really relevant 🙁
1637670239968.png

This window is not related to my previous topic about tooltip, it is relevant in such a way that I am just experimenting with the mouse input. But, this specific part only allows me to move the window by dragging it. It also changes the origin point to be the center of the window.

Secondly, this specific window won't be visible to a player, assuming I don't create some kind of "check enemy status" skill or item. It is currently there because I've added the Yanfly level plugin, and I want to have an easy way to see what stats enemies have. And since I am creating every single stat an enemy can have, around 40, I decided to use a selectable window.

So the problem I see, is that since my draw enemy status functions aren't being used, I assume the issue lies somewhere else. And rather than a mistake in my code, the problem comes from a potentially missing function that other selectable windows use?

I have now cleaned up the plugin, ensured that it works(With the same problem though), but it should be more clear what is actually a part of the plugin. I have added some comments. I have also, removed the part of the plugin that creates the parameter items to be displayed, as they should not be relevant to the actual list(?). It is, however, included under the spoiler after the code below.
JavaScript:
//Scene setup ######################################
(function(alias) {
    Scene_Battle.prototype.createAllWindows = function() {
        alias.apply(this, arguments);
        try {
            this.createEnemyStatusWindow();
            console.log($gameTroop._enemies.length);
            for (var i = 0; i < $gameTroop._enemies.length; i++) {
                $gameTroop._enemies[i].setStatRegistry();
            };
        } catch(e) {console.error(e)};
    }
}) (Scene_Battle.prototype.createAllWindows);

Scene_Battle.prototype.createEnemyStatusWindow = function() {
    var wy = this._helpWindow.y + this._helpWindow.height;
    var wh = this._statusWindow.y - wy;
    var ww = this._actorCommandWindow._width * 1.5;
    this._enemyStatusWindow = new Window_EnemyStatus(0, wy, ww, wh);
    this.addWindow(this._enemyStatusWindow);
};

//Window_EnemyStatus ######################################

function Window_EnemyStatus() {
    this.initialize.apply(this, arguments);
}
Window_EnemyStatus.prototype = Object.create(Window_Selectable.prototype);
Window_EnemyStatus.prototype.constructor = Window_EnemyStatus;

Window_EnemyStatus.prototype.initialize = function(x, y, width, height) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._touching = false;
    this._enemy = null;
};

Window_EnemyStatus.prototype.update = function() {
   
    if (this.getScene()._enemyWindow.enemy() && this._enemy != this.getScene()._enemyWindow.enemy()) {
        this._enemy = this.getScene()._enemyWindow.enemy();
        if (this._enemy.isEnemy()) { //Ensures that the selected member is an enemy and not an actor.
            //Get the amount of parameters of the enemy.
            this._paramsLength = this._enemy.paramsName.length;
            this.refresh();
        }
    }
    Window_Selectable.prototype.update.call(this);
};

//Function is related to the draw dark rectangles.
//As far as I can understand, this should not interfere as it only parses the coordinates for item placement.
Window_EnemyStatus.prototype.getRectPosition = function(index) {
    var rect = new Rectangle();
    rect.width = Math.floor(this.contents.width);
    rect.height = this.lineHeight();
    rect.x = /*index % 2 === 0 ? 0 : rect.width;*/ 0; //Set it to 0, preserved original within the comment.
    rect.y = index * this.lineHeight();
    return rect;
};

//Items setup
Window_EnemyStatus.prototype.maxItems = function() { //Sets the max amount of items in the list.
    if (this._enemy) {
        return this._enemy.paramsName ? this._paramsLength : 1;
    } else {return 1;}
};

Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.getRectPosition(index); //Gets the position for where to place the text.
    var statWidth = 96;
    rect.width -= this.textPadding();
    //Draws stat name.
    this.drawText(this._enemy.paramsName[index], rect.x + 4, rect.y, rect.width - statWidth);
    //Draws stat value.
    this.drawText(this._enemy.paramsValue[index], rect.x + rect.width - statWidth, rect.y, statWidth, 'right');
    this.changePaintOpacity(true);
};

The code for enemy stat setup. - It has dependencies on my superclass.
Spoiler: Enemy status code
JavaScript:
    Game_Enemy.prototype.setStatRegistry = function() {
        var i = 0;
        var n = 0;
        var code = 13;
        var elementsName = $dataSystem.elements;
        this.paramsName = [];
        this.paramsValue = [];
        this.paramsName[i] = "HP";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "MP";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "Attack";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "Def";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "M. Attack";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "M. Def";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "Agility";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);
        this.paramsName[i] = "Luck";
        this.paramsValue[i] = this.paramBase(i++).toFixed(0);

        n = 0;
        code = 11;
        for (let n = 0; n < this.traits(code).length; n++) {
            dataIndex = this.traits(code)[n].dataId;
            if (BISH.elementOrder.contains(dataIndex)) {
                this.paramsName[i] = elementsName[dataIndex] + " Res %"; console.log(this.paramsName[i]);
                this.setTrait(i++, this.traits(code)[n], true, "100-");
            };
           
        };
    };
   
    Game_Enemy.prototype.hasTrait = function() {
        console.log(this.traits.length);
        return this.traits.length > 0 ? true : false;
    };
   
    Game_Enemy.prototype.setTrait = function(index, trait, multiplier, modifier) {
            value = trait.value;
            if (multiplier) value *= 100;
            if (modifier) modifier == "100-" ? value = 100 - value : value =  value - 100;
            this.paramsValue[index] = value.toFixed(0);
            return false;
    };

    Game_Enemy.prototype.setAttackElement = function() {
        for (var i = 0; i < item.traits.length; i++) {
            if (item.traits[i].code == 31) {
                var attackElement = $dataSystem.elements[item.traits[i].dataId];
                return attackElement;
            }
        }
        return 0;
    };

Thanks! 🙂
OK, I'm pretty sure getRectPosition is the problem. Try replacing this part:
JavaScript:
Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.getRectPosition(index); //Gets the position for where to place the text.
...with this:
JavaScript:
Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.itemRect(index);


Explanation: drawAllItems draws the visible items in the list:
Spoiler: Window_Selectable#drawAllItems (rpg_windows.js)
JavaScript:
Window_Selectable.prototype.drawAllItems = function() {
    var topIndex = this.topIndex();
    for (var i = 0; i < this.maxPageItems(); i++) {
        var index = topIndex + i;
        if (index < this.maxItems()) {
            this.drawItem(index);
        }
    }
};
itemRect accounts for the current scroll position, a.k.a. "top index", but getRectPosition does not, which means items will always paint at the same positions regardless of scroll. 😮‍💨
OK, I'm pretty sure getRectPosition is the problem. Try replacing this part:
JavaScript:
Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.getRectPosition(index); //Gets the position for where to place the text.
...with this:
JavaScript:
Window_EnemyStatus.prototype.drawItem = function(index) {
    var rect = this.itemRect(index);


Explanation: drawAllItems draws the visible items in the list:
Spoiler: Window_Selectable#drawAllItems (rpg_windows.js)
JavaScript:
Window_Selectable.prototype.drawAllItems = function() {
    var topIndex = this.topIndex();
    for (var i = 0; i < this.maxPageItems(); i++) {
        var index = topIndex + i;
        if (index < this.maxItems()) {
            this.drawItem(index);
        }
    }
};
itemRect accounts for the current scroll position, a.k.a. "top index", but getRectPosition does not, which means items will always paint at the same positions regardless of scroll. 😮‍💨
Yeah, that was the issue, now it is working. How funny that the problem was the function I specifically didn't think was the issue.
Thread closed


Thread closed


(I'm closing it twice, 'cause you reported it twice 😉 )

Original thread: https://forums.rpgmakerweb.com/threads/why-is-the-window-selectable-item-list-scrolling-the-wrong-way-solved.142250/