MZ How to call Common Event within choices in JS

I cant seem to call a common Event inside the choices and i dont know why. I tested it with Command117 and with reserve common Event and neither did work. Im sure its something stupid that i overlook.

Code:
const PictureTestPluginName = "PictureTest";
PluginManager.registerCommand(PictureTestPluginName, "Choices", function () {
  let choices = ["Do1", "Do2", "Do3"];
  $gameMessage.setChoices(choices, 0, -1);
  $gameMessage.setChoiceBackground(2);
  $gameMessage.setChoicePositionType(2);
  $gameMessage.setChoiceCallback(function (responseIndex) {
    switch (responseIndex) {
      case 0:
        $gameMap._interpreter.command117(1998);
        break;
      case 1:
        $gameMap._interpreter.command117(1999);
        break;
      case 2:
        $gameMap._interpreter.command117(2000);
        break;
      default:
        break;
    }
  });
});

And this is the Event
Spoiler: Event
1690478375830.png

For now the Common Event only runs the "Choices" Code, but i want to loop over it multiple times in the future
Why aren't you just eventing this? You're not passing anything into the plugin command that is getting used as anything variable, it's all just defined in the code. You could do the same thing using event commands and it would just work.

As for why your code doesn't work, you're supplying the arguments incorrectly. It expects an array, not a single value. See this post:
So okay i got it working now. The Solution was to add [] brackets around the id like this $gameTemp.reserveCommonEvent([2000]);

Why aren't you just eventing this? You're not passing anything into the plugin command that is getting used as anything variable, it's all just defined in the code. You could do the same thing using event commands and it would just work.
Because its only a piece that i need for the entire project. When i dont know how something works, i try to isolate the problem by creating projects like these, where i dont have unnecessary code that hinders me.

Sadly this doent work in a loop yet and i am not sure why exactly.

Edit: Apparently it works by using this:
$gameMap._interpreter.command117([1998]);
When i first tried it out it didnt work, i swear.
The reserveCommonEvent() method does not want an array, it expects a value. The command117() method is the one that expects an array, as I linked above.

Good luck.
As Turan says, there is nothing here that cannot be done more easily with event commands. That's why the event command system exists in the first place: to make things easier!
Spoiler: re your plugin code
As I mentioned in your previous thread, there is no need for a global interpreter reference...you can just reference the calling interpreter in the choice callback. E.g. using Function#bind:
JavaScript:
  $gameMessage.setChoiceCallback(function (responseIndex) {
    if (responseIndex >= 0)
      this.command117([1998 + responseIndex]);
  }.bind(this));
Also, [2000] is an array containing just 1 value: 2000.
As Turan says, there is nothing here that cannot be done more easily with event commands. That's why the event command system exists in the first place: to make things easier!
Spoiler: re your plugin code
As I mentioned in your previous thread, there is no need for a global interpreter reference...you can just reference the calling interpreter in the choice callback. E.g. using Function#bind:
JavaScript:
  $gameMessage.setChoiceCallback(function (responseIndex) {
    if (responseIndex >= 0)
      this.command117([1998 + responseIndex]);
  }.bind(this));
Also, [2000] is an array containing just 1 value: 2000.
Okay maybe i wasnt clear earlier. The larger project has more code and requires me to do it in javascript. Well i already did it in plain Event system, but i came to the conclusion, that its not dynamic enough and requires lots of duplicated code.

But thanks for the help!

Edit: Also somehow this $gameMap._interpreter.command117([1998]) worked even if it shouldnt, i dont know.

Edit: i meant $gameTemp.reserveCommonEvent([2000]);
Edit: Also somehow this $gameMap._interpreter.command117([1998]) worked even if it shouldnt, i dont know.
you're supplying the arguments incorrectly. It expects an array, not a single value.
The command117() method is the one that expects an array, as I linked above.
Also, [2000] is an array containing just 1 value: 2000.
caethyril said:
Code:
// MZ, anywhere: insert common event 5 into the map interpreter
$gameMap._interpreter.command117([5]);
I don't understand the confusion. That is what you've been told multiple times is the correct syntax.
Yeah i know that it is the correct syntax. I was just pointing out, that for some reason it worked...or my eyes werent working properly, which could definetely be the case.

I think i know what happened. This worked $gameTemp.reserveCommonEvent([2000]); even though it shouldnt $gameMap._interpreter.command117([1998]) and this is correct. I confused them in the last post
Your posts are very unclear - are you saying "For some reason" in regards to the $gameMap._interpreter part?

caethyril never said that doesn't work, she's saying it might not always be the correct interpreter, and there's no reason to do it that way instead of using this to reference the object calling the function.
The problem with using this
$gameMap._interpreter.command117([number]); is, that it breaks my loop.

I think this is supposed to happen, but is there anyway around it?

The loop sits in a Common Event and calls the new Event from inside, but by doing this, it doesnt keep going with the main loop.

Loop:
Plugin Command: DoStuff()
:repeat above
and my js calls the other event instantly
@IareaGuys
please take a dozen steps back and tell us what you want to achieve, not how you want to do something unknown.
A lot of the confusion here and in your other topics because you always remove the context, and never tell us what result you want but only ask for tiny steps that are probably already completely wrong.

So what is the purpose of your work after everything is finished? What should the player see or do with that result?

But from what I can see your use of javascript is completely wrong - javascript CANNOT replace eventing, and a lot of things HAVE TO BE DONE by eventing, never by javascript.
But to tell yo what tools to use, we need to know what you want to0 do, not how you want to do this.
Well maybe im doing it wrong, but i think i am very close and i think i have already found a workaround for my problem, but if there is a better option i would use it instead because it would feel cleaner.

I dont know how far i can go into detail, with what i want to do, because i dont know if it would be against the forum rules and i am afraid that you wouldnt want to help when i tell everything.

The Obvious thing is i want to make a fight system, which presents you with multiple choices. Each choice has random outcomes which can be good for you or bad. There are many mechanics i thought of like a Momentum system, where if something good happens the possibility for another good thing increases for the next round and so on.

In order to achieve this i need of course all the logic, but also update the screen to show multiple pictures, choices, texts and play sounds. Other than Sounds and pictures i dont have any dependencies on rpgmaker, i think.

Please ask if anything is unclear, where you need more clarification and where i should go into more detail. I really appreciate your help.
OK, that is helpfull - the only question remains is the one you have been asked before:
why are you trying to use javascript for that?

From your description it would be much easier to do this battlesystem by eventing alone.

Plugins are NOT automatically the best solution, it always depends on what you want to do.
If you want to transport 10 tons of granite to the next town, you'll use a truck and not a Jaguar two-seater. Of course the Jaguar is much faster and much better looking and costs more - but it is still extremely unsuited to transport ten tons of stone...
I am using Javascript, because the code should be very dynamic. I want to have one Event per Enemy, which should only configure the fight...Give them a name, their strength, Skills etc etc and the same for the player somethimes. So i need a lot to account for. I have tried it with pure Eventing, but it bloats heavily, becuase either it results in massively duplicated code, or infinite Events. I have calculated that for 10 enemies i would need up to 200 Events + ~20-30 Events Logic. I mean i could obvously combine these Events, but that would result in very long common Events.
I dont know how far i can go into detail, with what i want to do, because i dont know if it would be against the forum rules and i am afraid that you wouldnt want to help when i tell everything.

The Obvious thing is i want to make a fight system, which presents you with multiple choices. Each choice has random outcomes which can be good for you or bad.
This sounds like you want to make some kind of gambling/gacha/lootbox system
still the wrong approach.

For cases like these, the entire battle mechanic should be in events - and only two or three events max.
then you should use script commands (NOT plugins) to read the additional data out of the notetags from the selected enemies - check the help file for the META script command, it is somewhere in the plugin sections of the help but should be used as a script function for your case.
I can assure you its not. And while most things are random i try to balance it out, with the choices and mechanics
still the wrong approach.

For cases like these, the entire battle mechanic should be in events - and only two or three events max.
then you should use script commands (NOT plugins) to read the additional data out of the notetags from the selected enemies - check the help file for the META script command, it is somewhere in the plugin sections of the help but should be used as a script function for your case.
Im not sure if its clear, but i dont use the regular battle System. I am building it completely from scratch.
I mean i could try it but im not sure if its actually a good approach. I mean i am mostly using the plugin to do all the logic, because i can actually calculate that and then i want to use the eventing system to Show Choices pictures and text and playing audio
@IareaGuys
That was clear - and that is exactly the reason why I suggested the eventing approach.

You need to condense your asking mechanic into one mechanical event that will not contain any text or question, but placeholder variables for each case.
THEN you need to fill those placeholders with texts read from the notetags of the current enemy (or whatever) by using the meta command I mentioned above.

The tricky part is to have eachg enemy contain the texts for multiple questions and answers in these notetags, and that might be easier to do by plugin.
But the texts need to be outside the common event or the plugin, and you'll need a logic function to decide when to load what.

ANY try to use fixed responses in either event or plugin is doomed to failure (unless you are extremely experienced in javascript, and you wouldn't ask if you were).
Your best option is to have the questions and answers stored outside your logic structure.
That could either be external text files to be loaded, json files or as said above in the notetags of the enemies - and the engine has build-in support for using notetags that way.
external texts or json would require more programming from you.
Hmm my desing is that when the logic is ready it calls a conditional unique Event, that shows their texts. I dont think i need to get rid of my plugin overall, because a plugin command can be the same thing that i want to do with a script call inside the event.
Also i have some special things like numbers that can only contain variables in a specified range, and in eventing i need to call another common event for that or write an if else statement.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-call-common-event-within-choices-in-js.159767/