MV Asking for help for my plugin (Show choices during Scene_Battle.prototype.onActorOk)

First of all, this is my first time posting something on the forum, please tell me if I did something wrong. I hope I am not posting my thread on the wrong forum...
Secondly, I only have limited knowledge of Javascript, and I do not have much experience in coding, so there are possibilities that I make some silly mistakes here and there.

So,basically I want to achieve something like this:

Capture.PNG

After players select themselves as target of item/skill, the code check if the certain skill/item is being used, then give player choices to proceed or to return.

I would like to achieve that when the player is using something on themselves, they can choose where they want to use the item for
for example, to use it on mouth, hand, leg, or to not use it, you get the idea.

For your information, I implement this plugin with YEP_BattleEngineCore plugin enables, but it should not affect much, as I have tried to run my plugin in new project, it seems to "work" just fine.
(By work I mean have the same problem lol)

Problems: Most of them work just fine, but problems appear as I am trying to code the red part. (I did not implement the skill part as I have no idea how to implement it, I only implement the item part)
I will post my code here, but for your convenience, please just go to my github to look at my code, it is basically the same thing, and I will also mention what I have tried there : My plugin in github

Spoiler: the ok part of my code
JavaScript:
var Rawang = Rawang || {};

//Make changes to onActorOk

Rawang.Scene_Battle_onActorOk = Scene_Battle.prototype.onActorOk;
Scene_Battle.prototype.onActorOk = function() {
    switch (this._actorCommandWindow.currentSymbol()) {
        //Check if used certain item
        case 'item':
        
            switch ($gameParty.lastItem().id){
                case 12:
                    this.checkConfirmation();
                break;
                
                default:
                Rawang.Scene_Battle_onActorOk.call(this);
            }
        break;
        
        //Default
        default:
        Rawang.Scene_Battle_onActorOk.call(this);
        alert("default");
    }
    
};

    
};
Spoiler: The problem(red) part of my code
JavaScript:
//If used certain item,show text & choices
Scene_Battle.prototype.checkConfirmation = function() {
    $gameMessage.add("Hello!");
    const choices = ["Continue", "Cancel"];
    $gameMessage.setChoices(choices, 0, 0);
    $gameMessage.setChoiceBackground(0);
    $gameMessage.setChoicePositionType(1);
    $gameMessage.setChoiceCallback(
    //I have no idea how this work
        function(choice){
            this.calresolution();
        });
    
//this is basically the Original code of Scene_Battle.prototype.onActorOk;
Scene_Battle.prototype.calresolution = function() {
    var action = BattleManager.inputtingAction();
    action.setTarget(this._actorWindow.index());
    this._actorWindow.hide();
    this._skillWindow.hide();
    this._itemWindow.hide();
    this.selectNextCommand();
};
The problem is,
I want to make that code will run after players make their choice, then decided whether procceed or return to the item page.I have no idea this is possible or not.

I think I am using the word "this" wrongly in the callback function, however, I have no idea how to make it work anymore. I tried instead using Scene_Battle.prototype.calresolution.call/bind/apply, or simply just put Scene_Battle.prototype.calresolution(). None of those works, they either show "xxx is not a function", "cannot read index of undefined" , or they will simply cause the game to freeze.

I tried to save the "this" keyword from Scene_Battle.prototype.checkConfirmation to a gameVariable: $gameVariables.setValue(52, this);
and save it in another variable in the next function (Scene_Battle.prototype.calresolution)

var calobj = $gameVariables.value(52);

then change all "this" keyword in calresolution function to calobj

eg. this._actorWindow.hide(); => calobj._actorWindow.hide();

When I test run after making these changes, it simply freezes my game after the calresolution function is called.

I know, to achieve what I want to do, I would need to write code that return player to the item/skill page. Here is the code that will run successfully, if completely overwrites Scene_Battle.prototype.onActorOk with these code:

Spoiler: Code that work if overwrite
JavaScript:
Scene_Battle.prototype.onActorOk = function() {
    this._actorWindow.hide();
    this._helpWindow.clear();
    switch (this._actorCommandWindow.currentSymbol()) {
    case 'skill_type':
        this._helpWindow.hide();
        this._skillWindow.show();
        this._skillWindow.activate();
        break;
    case 'item':
        this._itemWindow.show();
        this._itemWindow.activate();
        break;
    }
    
};

Maybe this can be achieved using other way. If so, please tell me how to do it. I really hope I can implement this as it will play an important role in my game 🙁

I am not a native English speaker, so I hope I have not messed up my grammar too much 🙂 Have a nice day!
setChoiceCallback stores a function to call when a choice is selected. The function takes one input: the selected choice index (0 = choice 1, 1 = choice 2, etc).

By default the callback is invoked on Game_Message, i.e. this is not Scene_Battle. Like you say, that is probably why your callback isn't working.

To tell the callback to run in the scene context, you should be able to use bind:
JavaScript:
$gameMessage.setChoiceCallback(this.calresolution.bind(this));
Then include checks in your calresolution method for the choice index, e.g.
JavaScript:
Scene_Battle.prototype.calresolution = function(n) {
    switch (n) {   // check selected choice index
        case 0:    // "Continue"
            this.onActorOk();
            break;
        // Other cases (1, 2, etc) here if needed~
        default:   // Anything else, e.g. "Cancel"
            this.startActorCommandSelection();
            break;
    }
};
I haven't tested this, but hopefully it helps~ 👋
First of all, thank you so much for helping me caethyril! I have learnt so many things from you in other threads 🙂

Sadly, the code does not work. When I use other items, it works fine. But when I use that certain item, the game "freeze" itself. I say "freeze" because when I type code in the console, the code runs, and nothing happens afterward.

I confirm that these two items target the actor themself, hence the code will run when these two items are used.

Spoiler: Using other item(ID: 11). It runs successfully.
using item11.PNG
run successfully.PNG
Spoiler: Using certain item(ID:12). It "freeze" the game.
using item12.PNG
option menu.PNG
game freeze.PNG
Spoiler: The game return no error when "freezing". It can even run code. However nothing happened after the code finished running.
freeze with no error.PNG
able to run code when the game freeze.PNG
Spoiler: Data of the items
Capture.PNG
2.PNG

Do you have any ideas how I can fix this? Thank you for your help anyway!
OK, I just did some testing and I think I see what is happening. When $gameMessage opens, the actor command window (Attack, Guard, Skill, Item) and the party command window (Fight, Escape) both get closed. When the message ends, neither of these windows open again. This is usually OK, because by default the input phase does not run any events or show any messages. In your case it means you will have to re-open the actor command window when a choice is made.

Try this for your calresolution method:
Spoiler: Scene_Battle#calresolution
JavaScript:
Scene_Battle.prototype.calresolution = function(n) {

    // Restore actor command window (closed by $gameMessage)
    this._actorCommandWindow.setup(BattleManager.actor());
    this._actorCommandWindow.selectSymbol('item');
    this._actorCommandWindow.deactivate();
    
    // React to selected choice
    switch (n) {   // check selected choice index
        case 0:    // "Continue"
            Rawang.Scene_Battle_onActorOk.call(this);
            break;

        // Other cases (1, 2, etc) here if needed~

        default:   // Anything else, e.g. "Cancel"
            this._actorWindow.hide();
            this.commandItem();
            break;
    }

};
It seemed to work OK for me with no other plugins, but you might need to add some stuff in for compatibility (e.g. that _helpWindow part). 😮‍💨

(I'm not sure why it froze when choosing that item for the last actor in the party before this edit. I guess it's something to do with mismatched checks in the core scripts: sometimes it checks if a window is active, sometimes it checks if it's open and active.)
Yep it works! I have made it so that it will also work when certain skill is used. Thank you so much caethyril!

Original thread: https://forums.rpgmakerweb.com/threads/asking-for-help-for-my-plugin-show-choices-during-scene_battle-prototype-onactorok.138674/