How to show a text in choiceCallback()?

● ARCHIVED · READ-ONLY
Started by kmph 3 posts View original ↗
  1. I've been struggling how to show the player a dynamically generated list of choices and I figured that using the Show Choices command would lead to lots of trouble here and it should be much easier to do this in a script.

    The sheet this post links to: https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mv-script-call-list.46456/ explains well how to actually show choices, but does not explain how to actually respond to the user maing a choice. For example, I would like to show the user a text that depends on their choice. How to do that?

    Having glanced at rpg_objects.js I figured that I should probably call $gameMessage.setChoiceCallback with a function tha handles the user's choice. So I thought I had to do something like this:

    Code:
    $gameMessage.setChoiceCallback(function(n) {
        switch(n) {
        case 0:
            $gameMessage.add('Affirmative!')
            break
        case 1:
            $gameMessage.add('Negative!')
            break
        }
    })
    
    $gameMessage.setChoices(["Yes", "No"], 0)

    The problem is that this is not working. The text is not being shown. The lines: $gameMessage.add('...') seem to be ignored.

    I thought that supplying setChoiceCallback with a custom function was the way to do this because this function is indeed being called whenever the user makes up his mind and the parameter does equal to the number of the user's choice. This, for example, works as expected:

    Code:
    $gameMessage.setChoiceCallback(function(n) {
        switch(n) {
        case 0:
            window.alert('Affirmative!')
            break
        case 1:
            window.alert('Negative!')
            break
        }
    })
    
    $gameMessage.setChoices(["Yes", "No"], 0)

    I also thought that I was correctly trying to display a text from a script, as a script containing only this line works as expected and does display the text:

    Code:
    $gameMessage.add('Some Text')

    So what am I doing wrong? How to display a text depending on the choice the user makes?
  2. did you solve the problem? cause im having the same one
  3. @polifack

    Yes.

    The problem is that setting choices and displaying text when the player chooses a choice mustn't be done in the same event command. There are also other things that must be done in different commands.

    So the simple way to counter this would be like this: (should hopefully work, but no time to test at the moment):

    To display choices in the first Script or Plugin command event command you write:

    Code:
    // default_choice can be -1 if no default
    // on_cancel: either choice or -2 if branch or -1 if disallow cancelling
    $gameMessage.setChoices(["Yes", "No"], default_choice, on_cancel)
    // background is 0 for Window, 1 for Dim and 2 for Transparent
    $gameMessage.setChoiceBackground(background)
    // position is 0 for Left, 1 for Middle or 2 for Right
    $gameMessage.setChoicePositionType(position)
    $gameMessage.setChoiceCallback(function(n) {
            this._branch[this._indent] = n
    }.bind(this))
    this.setWaitMode('message') // 'message' is a literal string

    and then in ANOTHER command:

    Code:
    // or whatever variable you've written to
    switch(this._branch[this._indent]) {
            case 0:
                    $gameMessage.setFaceImage(faceset, faceNo)
                    $gameMessage.setBackground(background)
                    // position: 0 Top 1 Middle 2 Bottom
                    $gameMessage.setPositionType(position)
                    // Each line = another call to add
                    // Max 4 lines
                    $gameMessage.add('Affirmative!')
                    this.setWaitMode('message')
                    break
            case 1:
                    $gameMessage.setFaceImage(faceset, faceNo)
                    $gameMessage.setBackground(background)
                    $gameMessage.setPositionType(position)
                    $gameMessage.add('Negative!')
                    this.setWaitMode('message')
                    break
    }

    Also note that if you want to dispaly more than one box of text each box must be in another event command.

    To overcome these issues I wrote this plugin: https://forums.rpgmakerweb.com/index.php?threads/events-as-scripts-with-a-use-case.84208/ You can use it if you find it convenient.