Eventing a way for the party to heal a state with choices?

I don't understand one thing... in the code up here ↑ there is a piece of code that says:
JavaScript:
$gameMessage.setChoiceCallback(function(index){
  target.removeState(choiceStates[index]);
});

I tried to figure out where this index variable is defined and searching in the core js of RPGMMV I've found that:
Game_Interpreter._index exist and its default value is zero... when I launch an event with only this script inside:
JavaScript:
var choices = ["yes", "no"];
b = this
$gameMessage.setChoices(choices, 0);
$gameMessage.setChoiceCallback(function(indice) {
  a = indice //comment - indice is another word for index
});
after I launch the game and interact with this event I can press F8/F12 (open console) and If I ask the value of a it says me it is 0 or 1 in according with my choice respectively of "yes" or "no".

but if I use this script in the event instead of that up here:
JavaScript:
var choices = ["yes", "no"];
b = this
$gameMessage.setChoices(choices, 0);
$gameMessage.setChoiceCallback(function(indice) {
  a = indice //comment - indice is another word for index
});

b = this._index //Game_Interpreter._index

And I make it run in game and open the console it says...

Cattura.JPG


So maybe this "index" and Game_Interpreter._index are not the same thing... someone could explain me where and when the $gameMessage.setChoiceCallback() function define/assign this index or let me know in which line it happens in wich file of the core?
Okay so this gets a little bit complicated, but index doesn't exist in that context until you're defining the callback function.

Code:
$gameMessage.setChoiceCallback(function(index){
  target.removeState(choiceStates[index]);
});

You are basically *creating* index as a callback variable to the function, and saying "whatever gets passed into this function when setChoiceCallback is called, we'll refer to it as "index" in this code block".

If you look at the code that handles setting the callback and selecting a choice:

Code:
Game_Message.prototype.setChoiceCallback = function(callback) {
    this._choiceCallback = callback;
};

Game_Message.prototype.onChoice = function(n) {
    if (this._choiceCallback) {
        this._choiceCallback(n);
        this._choiceCallback = null;
    }
};

So we can see what when setChoiceCallback is called, $gameMessage's _choiceCallback variable is set to the passed-in callback object, which is the function you supplied in the first bit of code. Since functions are objects just like everything else, right now "callback" is:

Code:
function(index){
  target.removeState(choiceStates[index]);
}

Now when onChoice is called, n is supplied as a parameter, which is whatever the index is of the choice you picked from the window. Then it calls _choiceCallback passing in n, so it's calling the function you created with the choice window's index as the "index" variable.

Does that make sense?
Yeah you made screenshots of the code I've watched trying to understand the workflow of the core... I'll edit this answer when I'll understand... give me some days to digest... ahahah
I'll cover the flow from start to finish and see if that makes understand it easier:

You present the player with four choices: "Poison", "Blind", "Rage" and "Silence". Internally, they have IDs of 0, 1, 2 and 3 respectively. The player chooses "Rage".

That calls onChoice, and passes in the chosen ID as n:

Code:
Game_Message.prototype.onChoice = function(2) {
    if (this._choiceCallback) {
        this._choiceCallback(2);
        this._choiceCallback = null;
    }
};

if (this._choiceCallback) is true, because of the assignment done when setChoiceCallback was called, so we go into the if statement.

this._choiceCallback(2); will call the function set as the callback, passing in 2 as the parameter:

Code:
function(2){
  target.removeState(choiceStates[2]);
}

So now removeState is called, passing in the third element of choiceStates (which due to the way we set up that array in the common event code, will be the ID of the Rage state)
You couldn't explain better than this way, thank you a lot. I understood the theory now, maybe a day of these I'll try to make some trivia events to get used to this "Callback" logic (call a function in another function to use it or its result like a variable for the caller function to run). As you can see from my first solution I'm not a coder at all, I just have fun in messing things up until the event works, using lots of tricks, global variable and switches and fake states (that in my opinion was the best idea I've ever had ahah). But, also if it is not my job at all, I love when a script works without using a plugin or a piece of code I don't understand. So thank you very much Trihan 🙂
As for Game_Interpreter._index, that's just which line of code the game is currently executing in your event. It was 7 in your example because the script call was 7 lines long.
Lol, I tought it was something about some other function using the same property ahahahah, thank you too Solar xD

Original thread: https://forums.rpgmakerweb.com/threads/eventing-a-way-for-the-party-to-heal-a-state-with-choices.123349/