JS test which choice was made

How do i know which choice the player made?

$gameMessage.setChoices(["yes", "No"]);

Dont i need something like "var x = $gameMessage.awaitChoice();"?
In eventing the usual way is to enter a command to create a flag inside each branch of the show choice command.

In javascript that depends on what exactly you are doing - theoretically you would do the same and create the branches that are triggered by the different choices, but that is not as easy as it sounds.

So what exactly are you trying to do and where are you placing those commands?
In eventing the usual way is to enter a command to create a flag inside each branch of the show choice command.

In javascript that depends on what exactly you are doing - theoretically you would do the same and create the branches that are triggered by the different choices, but that is not as easy as it sounds.

So what exactly are you trying to do and where are you placing those commands?
I want to make a switch case out of that

switch (condition) {
case 0: //code
case 1: //code
case 2: //code
case 3: //code
}

but i dont know the condition, because i dont have a variable for that. Or is there another way on how to do that?
I want to make a switch case out of that

switch (condition) {
case 0: //code
case 1: //code
case 2: //code
case 3: //code
}

but i dont know the condition, because i dont have a variable for that. Or is there another way on how to do that?
The way choices work in the default engine is that when the choice list is created, it also sets a choice callback in $gameMessage which takes n as a parameter (where n is the index of the selected choice) and sets the interpreter's branch at its current indent to be n, which causes it to start executing event commands from that branch position.
Can you explain it a little bit more. I dont really understand
I dont really understand
You sound as if you have only limited experience with javascript programming, and what you want to do does require some intermediate knowledge at minimum.
So I suggest taking a step back and answering the other question I had above, the one you didn't answer:

Where are you planning to use that code, and for what purpose? Are you writing a plugin? Are you trying to use it in an event script box?
Why aren't you using the event command itself?

Javascript is not a heal-all wonder solution - quite the contrary. When using javascript to do things from events, you are basically removing all built-in security measures and are required to handle all details for yourself to make the code work.
Because of that it is often easier to use the event commands as is depending on what you really want to achieve.

And we cannot really point you to a solution until you tell us what you want to do - as opposed to how you want to do something unknown, because the answer about the case struct is only a how your want to do something, while the what and the purpose are still unknown.
Not 100% sure this is what you're looking for, but I googled this for you:

$gameMap._interpreter.setupChoices([['Yes', 'No'], 1]); $gameMessage.setChoiceCallback(function(responseIndex) { if (responseIndex === 0) { //YOUR CODE HERE } if (responseIndex === 1) { //YOUR CODE HERE } });
You sound as if you have only limited experience with javascript programming, and what you want to do does require some intermediate knowledge at minimum.
So I suggest taking a step back and answering the other question I had above, the one you didn't answer:

Where are you planning to use that code, and for what purpose? Are you writing a plugin? Are you trying to use it in an event script box?
Why aren't you using the event command itself?

Javascript is not a heal-all wonder solution - quite the contrary. When using javascript to do things from events, you are basically removing all built-in security measures and are required to handle all details for yourself to make the code work.
Because of that it is often easier to use the event commands as is depending on what you really want to achieve.

And we cannot really point you to a solution until you tell us what you want to do - as opposed to how you want to do something unknown, because the answer about the case struct is only a how your want to do something, while the what and the purpose are still unknown.
I have actually decent experience with Javascript. Well more like Typescript and preact.
The thing i want to do is actually very simple. I want to use the show choices and based on what choice the player made i want to show a diffrent text for example.
Not 100% sure this is what you're looking for, but I googled this for you:

$gameMap._interpreter.setupChoices([['Yes', 'No'], 1]); $gameMessage.setChoiceCallback(function(responseIndex) { if (responseIndex === 0) { //YOUR CODE HERE } if (responseIndex === 1) { //YOUR CODE HERE } });
Yeah thats what i think how it should look like and thats what i want to do. However i dont know where you are getting the responseIndex from.

Also what is the diffrence between $gamemessage.setChoices and $gameMap._interpreter.setupChoices?
I want to use the show choices and based on what choice the player made i want to show a diffrent text for example.
that still doesn't answer the main question I had:

WHERE do you want to use that code?
Are you writing a plugin and if yes what is that plugin supposed to do?
Do you want to use that code in a script box? If so why not use the event command directly?

Or are you experimenting with code without any use for it at all?


EDIT:
And perhaps it will help if I explain the background in more detail.

"Show Choices" is an event command. As such it can only be used inside an event interpreter.
However there are two different interpreters in the engine, and a lot of code sections where both interpreters are disabled and cannot be used at all.

Add to that your question above about what the _interpreter is...

To give you an answer how to use that event command we need to know where in the engine - in which interpreter - you want to use it, because the event command CANNOT be used everywhere. For example no event command will ever work inside the menu, because the main menu has no interpreter.

So please answer the question:
Where and how do you want to use that command? What is the specific question you want to ask the player there?
that still doesn't answer the main question I had:

WHERE do you want to use that code?
Are you writing a plugin and if yes what is that plugin supposed to do?
Do you want to use that code in a script box? If so why not use the event command directly?

Or are you experimenting with code without any use for it at all?


EDIT:
And perhaps it will help if I explain the background in more detail.

"Show Choices" is an event command. As such it can only be used inside an event interpreter.
However there are two different interpreters in the engine, and a lot of code sections where both interpreters are disabled and cannot be used at all.

Add to that your question above about what the _interpreter is...

To give you an answer how to use that event command we need to know where in the engine - in which interpreter - you want to use it, because the event command CANNOT be used everywhere. For example no event command will ever work inside the menu, because the main menu has no interpreter.

So please answer the question:
Where and how do you want to use that command? What is the specific question you want to ask the player there?
Well Okay i am trying to write a plugin. And it should handle fights. So i go to a random Npc and then a fight should start, where 3 options appear. And then i need to handle the options. But i dont know which option was picked.
But i dont think its relevant actually.
Also i tested out this code and it didnt work.
Not 100% sure this is what you're looking for, but I googled this for you:

$gameMap._interpreter.setupChoices([['Yes', 'No'], 1]); $gameMessage.setChoiceCallback(function(responseIndex) { if (responseIndex === 0) { //YOUR CODE HERE } if (responseIndex === 1) { //YOUR CODE HERE } });
And this is exactly the code i need with the exception that i need to know where the responseIndex is coming from or if i have to define it.


Edit: Ok i was dumb the code is working now. I ran a diffrent event so i wasnt testing the code i wrote.
Anyways thanks for your help

Original thread: https://forums.rpgmakerweb.com/threads/js-test-which-choice-was-made.158871/