Script to identify if a party slot is empty.

@caethyril or @Rlyeh or Anyone else who knows JS. I have been trying to understand exactly what is happening with the script outlined above and what I incorporated in my test event. I have been looking things up for JS and trying to balance that with how I think it is being used in MZ specifically. But there are some things that just aren't clicking.

Would anyone be willing to break this script down with an explanation of what is happening in plain language? If so, I would be eternally grateful!

Once again, here is the screenshot of the script part of my test event:

ChoicesScriptUpdated.png


So here is what I think I understand. However flawed it might be.

choices = [ ];

Is creating an empty array and choices is what MZ uses for the Show Choices command?

$gameParty.members().forEach(m => choices.push(m.name()));

Is using .forEach to cycle through all the party members and pushing (adding) the names to the choices array. m is referencing the members and m.name is referencing the member name? Not sure why an => is needed here.

choices.push("Nevermind");

Is pushing (adding) "Nevermind" to the choices array in the last position so that will be an option outside of the party member names.

$gameMessage.setChoices(choices, 0, choices.length-1);

I think is setting a variable Choices to the choices array, not sure what 0 is doing unless it is referencing the first position in the array, and then something about the last position in the array so something about "Nevermind". Maybe this is saying the variable Choices is set everything in the array from position 0 to the last position?

$gameMessage.setChoiceBackground(1);

Is setting the background for the choices message display which you would normally set through the Show Choices command in the event.

$gameMessage.setChoicePositionType(1);

Is setting the position for the choices message display which you would normally set through the Show Choices command in the event.

$gameMessage.setChoiceCallback(n => {

I read what a callback does, but I'm not really clear on what this is doing and the n is throwing me off too. I read that n could be a reference to a line break, but I don't think that is what it is doing here. Is n what is being selected as a choice?

$gameVariables.setValue(106, n!=choices.length-1?n:-1);

This is setting the variable designated as 106, but I am having trouble interpreting this too. I read that != means NOT equal so I think that is saying n is NOT equal to "Nevermind" because choices.length-1 is the value of the last position in the array which is "Nevermind". I read that : can mean size. But I also read that ?: can be a conditional statement like if then. Is this essentially setting the value of variable 106 to a number depending on the number of your party members like 0, 1, 2, 3, 4, -1?

});

I think this is just closing the callback.

this.setWaitMode('message');

I read about wait in JS and I think this is using the WaitMode option in MZ. I'm not sure what the 'message' part is all about unless that is just referring back to waiting for a choices selection to be made.

So as you can see, maybe I understand a little bit of what is going on or maybe not. And a lot is unclear. I am starting to work through an online JS class to start learning from the very beginning but that is a slow process.

Again, if anyone is willing to help me understand what is actually happening in this script, in plain language, that would be awesome. I really appreciate folks' time and patience with new users.

Thanks!
Note that there is some explanation of this kind of thing on the MV / MZ Script Call List.
choices = [ ];

Is creating an empty array and choices is what MZ uses for the Show Choices command?
Yes, choices is defined as an empty array and will be used later in the script call.

$gameParty.members().forEach(m => choices.push(m.name()));

Is using .forEach to cycle through all the party members and pushing (adding) the names to the choices array. m is referencing the members and m.name is referencing the member name? Not sure why an => is needed here.
Yes. m => m.name() is a single arrow function expression: it says "take the input m and output m.name()". forEach calls this function once per element in $gameParty.members(), with inputs based on the current iteration. Details:
Arrow functions have some limitations but otherwise work much the same as "normal" functions, transforming input value(s) into an output. They're also shorter to write! Details:

choices.push("Nevermind");

Is pushing (adding) "Nevermind" to the choices array in the last position so that will be an option outside of the party member names.
Correct. Specifically, Array#push adds to the end of the array, so "Nevermind" will always be the last option.

$gameMessage.setChoices(choices, 0, choices.length-1);

I think is setting a variable Choices to the choices array, not sure what 0 is doing unless it is referencing the first position in the array, and then something about the last position in the array so something about "Nevermind". Maybe this is saying the variable Choices is set everything in the array from position 0 to the last position?

$gameMessage.setChoiceBackground(1);

Is setting the background for the choices message display which you would normally set through the Show Choices command in the event.

$gameMessage.setChoicePositionType(1);

Is setting the position for the choices message display which you would normally set through the Show Choices command in the event.
These are all correspond directly to the Show Choices options in the editor:

showChoices.png

  • setChoices(choices, defaultType, cancelType)
    There are 3 inputs here, in order: Choices, Default, and Cancel.
    • Choices: just lists the text to display for each choice.
    • Default: -1 = None, 0 = first choice, 1 = second choice, etc.
    • Cancel: -2 = Branch, -1 = Disallow, 0 = first choice, etc.

  • Background: 0 (Window), 1 (Dim), 2 (Transparent).

  • Window Position: 0 (Left), 1 (Middle), 2 (Right).
So for your example:
  • Choices = all party member names + "Nevermind".
  • Default = first choice.
  • Cancel = last choice. It's choices.length - 1 because first choice is 0, not 1.
  • Background = Dim.
  • Window Position = Middle.

$gameMessage.setChoiceCallback(n => {

I read what a callback does, but I'm not really clear on what this is doing and the n is throwing me off too. I read that n could be a reference to a line break, but I don't think that is what it is doing here. Is n what is being selected as a choice?
This provides a function to run when a choice is selected (it's another arrow function). The game gives the index of the selected choice to this function, e.g. n = 0 for the first choice. Something like this is important, otherwise we wouldn't know what the player selected.

(The callback for the event command is different: it just tells the interpreter to skip to the appropriate "When" branch in the current Show Choices block.)

$gameVariables.setValue(106, n!=choices.length-1?n:-1);

This is setting the variable designated as 106, but I am having trouble interpreting this too. I read that != means NOT equal so I think that is saying n is NOT equal to "Nevermind" because choices.length-1 is the value of the last position in the array which is "Nevermind". I read that : can mean size. But I also read that ?: can be a conditional statement like if then. Is this essentially setting the value of variable 106 to a number depending on the number of your party members like 0, 1, 2, 3, 4, -1?
It sets the value of variable ID 106 equal to:
  • n (selected index) if it was not the last choice ("Nevermind"); or
  • -1 otherwise.
E.g. if you select the first choice, the variable will be set to 0. Remember this is in the choice callback function, which gets run when you select a choice.

condition ? if_true : if_false is the conditional operator. Like you say, it's basically an if...else. Details:

});

I think this is just closing the callback.
Yep! The corresponding opening brackets ( and { are on this line:

$gameMessage.setChoiceCallback(n => {

this.setWaitMode('message');

I read about wait in JS and I think this is using the WaitMode option in MZ. I'm not sure what the 'message' part is all about unless that is just referring back to waiting for a choices selection to be made.
In this context (standalone Script command) this refers to the instance of the interpreter that is processing the current event. RMMZ's event interpreters have a "wait mode" mechanic that is used for all kinds of things. It's most obvious on commands that have a "Wait for completion" option. Basically it tells the interpreter to wait until something is no longer busy. In this case, no more commands will be processed until the message is closed (by selecting a choice).
@caethyril Thank you so much for the explanation! I will add this to my own reference document that I can go back to.

This is incredibly helpful!
You're welcome~ 👋
$gameVariables.setValue(106, n!=choices.length-1?n:-1);

This is setting the variable designated as 106, but I am having trouble interpreting this too. I read that != means NOT equal so I think that is saying n is NOT equal to "Nevermind" because choices.length-1 is the value of the last position in the array which is "Nevermind". I read that : can mean size. But I also read that ?: can be a conditional statement like if then. Is this essentially setting the value of variable 106 to a number depending on the number of your party members like 0, 1, 2, 3, 4, -1?
?: is a conditional ternary operator, meaning it takes 3 inputs.
In the code it's checking that n is not equal to the last choice in which it returns n, otherwise it returns -1.
My reasoning is it's simpler to check for cancel by testing if var 106 is -1 rather than testing if 106 is party members + 1.
?: is a conditional ternary operator, meaning it takes 3 inputs.
In the code it's checking that n is not equal to the last choice in which it returns n, otherwise it returns -1.
My reasoning is it's simpler to check for cancel by testing if var 106 is -1 rather than testing if 106 is party members + 1.
@Rlyeh Thanks for that explanation! Every example and description helps me tremendously!

Original thread: https://forums.rpgmakerweb.com/threads/script-to-identify-if-a-party-slot-is-empty.153066/