Handling Show Choices in Javascript

I have a question that I've been researching for a while now. 


I'm very new to Javascript, but I'm not terrible at it. I am a bit overwhelmed by some of what I look up in the core JS scripts, however.


I've been trying to figure out how to use script calls to handle Show Choices. 


I know it can be done through the maker with events, I just want to know for my own reasons, which aren't important to the question itself. 


So I'll give a simple scenario. 


I want to show choices 'Yes' and 'No'


Then I would like to use the resulting choice in, say, an if statement. 


I've seen the very helpful script calls spreadsheet for MV, but it doesn't get me any closer to my answer. 


And Ive seen SumRndmDd's (sp) thread, but it still didn't help me. What is the actual call I would use/how would I use it.


Could someone show me a simple example Of showing choices 'Yes and 'No', and then how to use the resulting choice in a if statement to display text according to the choice?


Any help would be massively appreciated ^^
Check Game_Interpreter.command101 to see the code that is used to show choices:


this._index++;
this.setupChoices(this.currentCommand().parameters);


So you could do something like this to show your Yes/No choices:


$gameMap._interpreter.setupChoices([['Yes', 'No'], 1]);


And to handle the response with JS, you can use a callback function:

Code:
$gameMessage.setChoiceCallback(function(responseIndex) {
  if (responseIndex === 0) {
    // Player chose "Yes"
  } else {
    // Player chose "No"
  }
});
I tried that (copied and pasted both parts) in a script call, my game threw an error that said 


Cannot read property length of null


Edit: Also, I tried just putting the first call on the event, and it said the same error
I tried that...



I wanted to try this out, too, and it worked. In an Event, I created a Script command, into which I pasted the first part (the 'Show Choice' part. I tested this, and when I triggered the Event in Pest Play, the Choice was displayed.


I then created two Common Events, one for each Choice made by the Player. In the same Event as above, I created a second Script command, into which I pasted the second part (the Choice Handling...). I added a line of Script for each of the Choices, calling the appropriate Common Event. In Test Play, the Choices were displayed, and the correct Common Event called, depending on the Choice made.


Here's the final code from the Event...

Spoiler
◆Script:$gameMap._interpreter.setupChoices([['Yes', 'No'], 1]);
◆Script:$gameMessage.setChoiceCallback(function(responseIndex) {
:      :  if (responseIndex === 0) {
:      :    // Player chose "Yes"
:      :$gameTemp.reserveCommonEvent(28);
:      :  } else {
:      :    // Player chose "No"
:      :$gameTemp.reserveCommonEvent(29);
:      :  }
:      :});

...and the code from one of the Common Events, named 'Repl_Yes' (the other is almost identical, of course...)

Spoiler
◆Script:$gameMessage.setFaceImage('Actor1',0)     
:      :$gameMessage.setBackground(0)
:      :$gameMessage.setPositionType(2)                   
:      :$gameMessage.add(" Player chose 'Yes'")

Does this help at all..?
Ok, that works for me, too, but is it necessary to have a common event for every possible choice, or to even use


common events at all? Why couldn't I do what the common event is doing in place of the common event call? Or rather, 


'why' can't I, because when I try it without using a common event it doesn't work...


Also thank you both very much for your responses, Hudell your OrangeHud and it's components are part of my essentials,


and your response was super quick and obviously correct. 


Dad3353, thank you very much for testing that all out, too, and for responding. Would you be able to clarify that for me, 


though? I can see using one common event to handle all choices but I just want to know if using them at all is necessary.
... is it necessary to have a common event ...



No, there's no necessity; one may put any (legitimate...) js code in place of my Common Event call. I was simply testing it out; pasting the Message code in there would have worked, but for the inherent limitation of the Script Editor, which won't allow more than 20 lines (it's not a scrolling window just a fixed text entry box...). Using a call to a Common Event gave me more scope to alter whatever I wanted the Choices to do, that's all, and it's a programming 'habit' that I have. I can imagine a case where there could be many Choice Events, but would want one single place for maintaining the replies (translation, for instance..?).


Glad it's working for you now; I won't ask why you wanted to do it by Script, rather than in classic fashion, but it's taught me a thing or two, too, whatever its utility. I may try one with more than 6 choices; I have an application for that in one of my Projects, and 'Show Choices' is limited to 6. Hmm... Now let's see if that works..!
That's the thing, though... Using the same command from the common event in place of the common event call doesn't work. The common event call works, but the exact same code from that common event won't. Any thoughts?
Here is a Show Choice with 8 Choices..!

Spoiler
◆Script:$gameMap._interpreter.setupChoices([['Yes', 'No', 'Perhaps', 'Certainly', 'Never..!', 'What..?', 'Dunno', 'Hmm...'], 1]);
◆Script:$gameMessage.setChoiceCallback(function(responseIndex) {
:      :  if (responseIndex === 0) {$gameTemp.reserveCommonEvent(28);}
:      :  if (responseIndex === 1) {$gameTemp.reserveCommonEvent(29);}
:      :  if (responseIndex === 2) {$gameTemp.reserveCommonEvent(30);}
:      :  if (responseIndex === 3) {$gameTemp.reserveCommonEvent(31);}
:      :  if (responseIndex === 4) {$gameTemp.reserveCommonEvent(32);}
:      :  if (responseIndex === 5) {$gameTemp.reserveCommonEvent(33);}
:      :  if (responseIndex === 6) {$gameTemp.reserveCommonEvent(34);}
:      :  if (responseIndex === 7) {$gameTemp.reserveCommonEvent(35);}
:      :});


Spoiler
30867728310_9856e10116_o.png


Spoiler
31199588816_13f2069815_o.png



I'm trying to get code to work instead of the Common Event call. I don't do it that way, usually, but I'll keep trying. It's not easy with this tiny Script Editor Window (it's not made for that...), which is why I use Common Events..! I'll keep trying, though...
I've been hacking away at it, too. This thread has brought me so very close to the answer, I've been trying functions, different script calls, anything outside of common events doesn't seem to work. Honestly, though, I'm not overly upset with


having to use one or a few common events that I could use to interact with my scripts. 


I already scripted my own way to wait via scripts or script calls, but I had to use a common event for it, too. 


I might have to do the same with this, but I'm not starting to wonder if I can use Yanfly's Call Event or just


the script calls for it on the spreadsheet in place of the common events! 


Either way, please let me know if you come up with anything further in regards to the initial inquisition, and thanks a


ton for everything ^^ 
You may have something wrong on the script calls you're adding there, then.


Also, note that the number 1 at the end of that line means that the second options is the default one (change it to -1 if you don't want any default)
...


Either way, please let me know if you come up with anything further in regards to the initial inquisition...



Well, thanks to Hudell, and quite some trial and (mostly...) error, I finally got this to work...

Spoiler
◆Script:$gameMap._interpreter.setupChoices([['Yes', 'No', 'Perhaps', 'Certainly', 'Never..!', 'What..?', 'Dunno', 'Hmm...'], 6]);
◆Script:$gameMessage.setChoiceCallback(function(responseIndex) {
:      :  if (responseIndex === 0) {$gameTemp.reserveCommonEvent(28);}if (responseIndex === 1) {$gameTemp.reserveCommonEvent(29);}
:      :  if (responseIndex === 2) {$gameTemp.reserveCommonEvent(30);}if (responseIndex === 3) {$gameTemp.reserveCommonEvent(31);}
:      :  if (responseIndex === 4) {$gameTemp.reserveCommonEvent(32);}if (responseIndex === 5) {$gameTemp.reserveCommonEvent(33);}
:      :  if (responseIndex === 6) {$gameTemp.reserveCommonEvent(34);}
:      :  if (responseIndex === 7) {setTimeout(function(){
:      :$gameMessage.setFaceImage('Actor1',0);
:      :$gameMessage.setBackground(0);
:      :$gameMessage.setPositionType(2);
:      :$gameMessage.add(" Player chose 'Hmm...'");}, 1);}
:      :});

Much attention to detail, such as these blasted Brackets and Braces, and it's not guaranteed to be without drawback somewhere, but at least it works..! Yippee..!

Original thread: https://forums.rpgmakerweb.com/threads/handling-show-choices-in-javascript.71450/