MZ Need help converting a set of events into a script

I'm currently trying to convert the tutorial Letting The Player Choose Their Character into a script as it's an unreadable mess and a pain to update as events only, however, I've ran into the issue of not knowing how to handle recursive choice selections. I saw the following threads Question about Nested Choices via JS and Message window doesn't show when called more than once but the first one isn't actually answered and the second one isn't script only. I'm not really sure on how to handle this. I'm sure there's a more complex character creation plugin, but I see no point in using one for something this simple.

I also saw this but it doesn't go into this either.

The code as-is:
JavaScript:
const characters = [
  { name: 'Kasey', class: 2, event_id: 26, image_id: 5, battle_image_id: "Actor1_5" },
  { name: 'Eliot', class: 3, event_id: 27, image_id: 6, battle_image_id: "Actor1_6" },
  { name: 'Roza', class: 3, event_id: 28, image_id: 7, battle_image_id: "Actor1_7" }
];

$gameMessage.add("Welcome Hero! It's time to face your destiny.\nNow, who are you?");
$gameMessage.setChoices(['Kasey', 'Eliot', 'Roza'], 1, -1);
$gameMessage.setChoiceCallback(index => {
  character = characters[index]
  $gameMap.event(character.event_id).moveStraight(2);

  // TODO: Show selected character's actor image. Part of the Yes/No choice selection process

  // TODO: Move this behind a separate yes/no choice selection
  // If no, the character event_id moves back once and goes back to the character selection menu
  $gameParty.members()[0].setName(character.name)
  $gameParty.members()[0].changeClass(2, true);
  $gameSwitches.setValue(2, true);

  // Plays animation
  const char = this.character(this._characterId = -1);
  $gameTemp.requestAnimation([char], 117);
  this.setWaitMode('animation');

  // Changes the player's character
  var actor = $gameParty.members()[0];
  actor.setCharacterImage('Actor1', character.image_id);
  actor.setFaceImage('Actor1', character.image_id);
  actor.setBattlerImage(character.battle_image_id);
  $gamePlayer.refresh();
  // TODO: Have the event send a message saying the character was selected
});

this.setWaitMode('message');
Bump.
Did more looking in the mentioned google doc link, definitely didn't find an example for this.
Oh boy. I hope you're not planning any fancy cutscenes for your game. 😅

Trying to reproduce event commands with script calls is less straightforward, readable, maintainable, efficient, and concise. It's also more likely to break plugins. What I'm saying is: it's possible, but generally not the best idea.

You're having trouble because you're trying to use the event interpreter's timings (e.g. "do this after a choice has been selected") while largely avoiding use of the interpreter. Normally the interpreter works with a list of command objects, e.g. the core Show Choices callback simply jumps to the corresponding branch in the command list, and continues processing commands from there. Similarly, you'd need external structure to "go back to the character selection menu". Why reinvent the wheel?

If you're using this in a Script command in an event, you can just put the follow-up in another Script command after the first, e.g.
JavaScript:
// $gameMessage stuff...
$gameMessage.setChoiceCallback(index => {
  // store selected index in variable ID 123 for later reference
  $gameVariables.setValue(123, index);
});
JavaScript:
// Retrieve stored index.
const index = $gameVariables.value(123);
// more stuff~
The this.setWaitMode("message") line should prevent the interpreter from processing any further commands until the message is dismissed, which in this case involves selecting a choice. 👋


I can't think of a good reason for it, but if you really needed to replace a series of event commands with scripting, the best approach might be to:
  1. Build the list of new commands to insert;
  2. (Deep-)clone the current command list;
  3. Splice the new commands in at the current index; then
  4. Assign the modified clone as the new command list.
Step 1 would involve writing commands in their p-code as detailed on the "Event Code" sheet of the Script Call Reference, a sister document to the Script Call List that you linked:
This p-code can also be seen in a project's map files, e.g. data/Map001.json. There's an example on the Script Call List: search for Insert Event Commands (currently row 579).
Oh boy. I hope you're not planning any fancy cutscenes for your game. 😅
Not for this, no. I could also leave it as just event calls, but as a programmer it bugs me because of how quickly it becomes unreadable and unmaintainable (also because of how much copy-pasta has to be done for something that's quite simple)
What little animation there is though is already covered via "$gameSwitches.setValue(2, true);" and events 🙂
I could also leave it as just event calls, but as a programmer it bugs me because of how quickly it becomes unreadable and unmaintainable
If you think JS is more readable and maintainable than event commands, then I recommend you check out the script files directly, in your project's js subdirectory. E.g. rmmz_objects.js defines Game_Interpreter, the controller responsible for timing and executing event commands~
Adding some detail to the "avoiding the interpreter" point, one issue with your opening code is that the default message implementation does not allow you to line up another message before the previous one is fully completed and cleared (which has technically not yet happened when the choice is recorded). Similarly, the "setWaitMode" function does not immediately interrupt your script command. The command will be fully executed, but the interpreter will not start executing another before the wait is completed.

As a result, the easiest solution would probably be to split your script command into multiple parts, adding a break whenever you want the event to wait for the previous step to complete.
(You may have to save some data from your local variables in a way that they are accessible by the later script parts)

Of course, that is if you want to stick to the scripted event approach.
If you think JS is more readable and maintainable than event commands, then I recommend you check out the script files directly, in your project's js subdirectory. E.g. rmmz_objects.js defines Game_Interpreter, the controller responsible for timing and executing event commands~
That's certainly an.... interesting approach. I've done cleaner implementations of some of this stuff myself in Java and C++ lol

Original thread: https://forums.rpgmakerweb.com/threads/need-help-converting-a-set-of-events-into-a-script.176649/