Moving BattleEnemy window with index of highlighted battler (during selection)

Hi fellow Makers.

Is there a way to get the index of a highlighted battler ?
For example, $gameTroop.members()[0]._hp returns the current hp of the first enemy.
Which script would return the current hp of the currently highlighted battler ?

I'm trying to customize the BattleEnemy and the BattleActor windows so I need the index before the target has been selected.

If that index doesn't exist, how would you check the hp of the highlighted battler ?

I hope I'm clear enough.

Thx for your help
This should return the game enemy index of the battler that's currently being hovered over.

JavaScript:
let hoveredBattler = doStuff();
function doStuff(){
    for(let i = 0; i < $gameTroop._enemies.length; i++){
        if($gameTroop._enemies[i]._selected === true){
            return i;
        }
    };
};

The variable hoveredBattler would hold the value of the index.
Hi, Tea's Jams.
Thx for your help.
Your function does indeed return the good index, so I guess the topic can be closed.

Edit : If it's not too much I would like to make it so that if nobody is selected, it returns 0.
Huh, I wonder why? I just tested it again and it's working for me. :LZYhuh:

Edit***
Just a thought, this is returning the enemy index, are you trying to get the enemy Id? @thierrywtrmz
No it's really the index I want.
I said it didn't work but It was my mistake.

Could you add something to return 0 if nobody is selected ?
You can do this to return 0 if none are selected, but you may want to change 0 to something else, since it's also the index of your first enemy. So say you have a battle with one enemy, whether you are hovering or not, it will always return 0.

JavaScript:
let hoveredBattler = doStuff();
function doStuff(){
    let data = [];
    for(let i = 0; i < $gameTroop._enemies.length; i++){
        data.push($gameTroop._enemies[i]._selected)
    };
    let checker = data.every(v => v === false);
    if(checker === true){
        return 0;
    };
    for(let i = 0; i < $gameTroop._enemies.length; i++){
        if($gameTroop._enemies[i]._selected === true){
            return i;
        }
    };
};
Thanks again Tea's Jams.

I ran into an issue though, the dostuff function works great but in the game console, wether I select an enemy or not, the hoveredBattler command returns undefined.

I tried the code in an empty project and in an empty file.

(By the way, when I put the code at the begining of my script it kinda messes things up, but not when it is at the end. Any idea why ?)
If you want to see the value of hoveredBattler in the console, just add console.log(hoveredBattler); as the last line of the script.

Honestly I wouldn't know why it messes it up without seeing your script.
So it was a noob issue again, sorry about that.
Thanks again for your help.
You're very welcome! I'm glad you got it working :LZSjoy: :LZSjoy: :LZSjoy:
Hey it's me again.

I'm still stuck on a thing.
I'd like to move the BattleEnemy window to match the selected enemy coordinates.
With the previous function coded by Tea's Jam, I can easily get the target screenX and screenY
but I can't find the way make to make the window move.

I've tried using the refresh function but I guess it just refresh the content of the window.
Since the window is only created once,
how can I change its position mid-battle ?
Up !

My BattleEnemy window still won't move.
I'm currently trying to use a function similar to the Scene_Battle.prototype.updateWindowPositions function.

function MoveWindow(){ if (this._enemyWindow.x != something) { this._enemyWindow.x = something; } };

I don't understand why this._enemyWindow.x returns undefined when this._statusWindow.x works fine in the Scene_Battle.prototype.updateWindowPositions function.

Also, I'm not really sure of where I should run this function, I was thinking of adding a line a the end of
Window_BattleEnemy.prototype.select = function(index) { Window_Selectable.prototype.select.call(this, index); $gameTroop.select(this.enemy()); };

Please help me or I might start begging.
Hey, I'm Back.
In case someone is interested, I finally made my window move using
function MoveWindow(){ if (SceneManager._scene._enemyWindow.x != something) { SceneManager._scene._enemyWindow.x = something; } };

I also managed to add this function to the select function as mentioned previously and it's working, my window as I want.

I'm sure my script is a mess but I feel like I improved a lot for this.

Original thread: https://forums.rpgmakerweb.com/threads/moving-battleenemy-window-with-index-of-highlighted-battler-during-selection.139717/