Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

[RESOLVED] How to Use $gameMessage.setChoiceCallback in Javascript to message from choice?

mnihas

Villager
Xy$
0.00
Hello,

I try to create plugin that shows questions from JSON file, and if the answer is correct, it gives someting to the player.

So far I managed to read data from JSON file and show question + choices, but when I process answer in $gameMessage.setChoiceCallback I can't show another message - it is closing. How can I prevent closing message or make plugin to wait for player action? I use the code below:

JavaScript:
$gameMap._interpreter.setWaitMode('message');
  $gameMessage.addText(this.list[index]._question);// add text to message
  _curCorrectAnswer =this.list[index]._correctAnswer;// store correct answer in global variable
  $gameMessage.setChoices(this.list[index]._answers,0,-1);// show choices

  $gameMessage.setChoiceCallback(function(choice){// choice processing callback
    if(choice == _curCorrectAnswer){// check if answer is correct
      $gameMessage.newPage();
      $gameMessage.addText("Correct");
      Input.isPressed('ok');// I try to wait for user action, but code doesn't stop here

      // setChoiceCallback ends and nothing is shown
    }
   });
I'm new to RPG Maker and it's scripting library. As I cannot find the answer I decided to ask more experienced RPG Maker MV users. I will be grateful for any hint on this topic.

Regards.
Michal
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
It's a bit hard to help you out with the little bit of information you've provided. How are you going about, is this bit of code in it's own function? When is this code being run, where is it being run?. You could try running another setWaitMode inside the callback function but I'm afraid I can't be of much help without seeing how all this is being accomplished, I've also never really looked at the Game_Message class so I don't know too much but I may be able to help if I know a bit more details, maybe more code.
 

mnihas

Villager
Xy$
0.00
It's a bit hard to help you out with the little bit of information you've provided. How are you going about, is this bit of code in it's own function? When is this code being run, where is it being run?
Thank you for a prompt response!

I'll describe it in more details.

  1. I created plugin (based on Random Dialogue by bluebooth), that is run from Event->Plugin command
  2. It reads list of questions from JSON file and puts them into array to randomly pick one
  3. I register new command whitch calls function to show message with a choice and based on answer shows another message with information if aswer is correct.
  4. Message shows for a moment and dissapers
The code is shown below:

JavaScript:
//=============================================================================
// Game_Interpreter
//=============================================================================
var MNH_RQ_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {

    if (command === 'RandomQuestions')
    {
        switch (args[0]) {
    case 'getRandomQuestion':
                    SceneManager._scene.getRandomQuestions(args);
                    break;
            };
        }
    else {
        MNH_RQ_Game_Interpreter_pluginCommand.call(this, command, args);
    }
};


Scene_Map.prototype.getRandomQuestions = function(args) {

        // find random question randomIndex
        ...

        // set wait mode - until message isBusy
        $gameMap._interpreter.setWaitMode('message');

        if (args.length > 3) {
            // set argumets for face image
            ...

            $gameMessage.setFaceImage(faceFile, faceNum);
        }

        // add message text - function from YEP_MessageCore - standard version works the same
        $gameMessage.addText(this.shortlist[randomIndex]._question);

        // remember correct answer in global variable - needed by callback function
        _curCorrectAnswer = this.shortlist[randomIndex]._correctAnswer;

        // set possible answers list
        $gameMessage.setChoices(this.shortlist[randomIndex]._answers, 0, -1);

        // set callback function when answer is chosen
        $gameMessage.setChoiceCallback(function (choice) {
            // answer correct - show new message
            if (choice == _curCorrectAnswer) {
                // I set wait mode again, but it doesn't work
                $gameMap._interpreter.setWaitMode('action');
                $gameMessage.setPositionType(1);

                // clear message content
                $gameMessage.newPage();

                // addd new text
                $gameMessage.add("Correct answer");

                // message shows for a while and terminates - app goes to
                // Game_Message.prototype.onChoice and resets choiceCallback
                // then callOkHandler calls this._messageWindow.terminateMessage();
                // Both functions from RPG Maker API are shown below
          }
        });
    };


Game_Message.prototype.onChoice = function(n) {
    if (this._choiceCallback) {
        this._choiceCallback(n);
        this._choiceCallback = null;
    }

Window_ChoiceList.prototype.callOkHandler = function() {
    $gameMessage.onChoice(this.index());
    this._messageWindow.terminateMessage();
    this.close();
};
I tried to set setWaitMode again, but it doesn't work with message and action wait mode.

Full source code is in http://pastebin.com/CSTERzxG

I wonder, if I should create new window or overload callOkHandler function?

If you have any clue for me it could be great.

Best regards,
Michal
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Okay, just give me a bit of time, I'll take a look at the full script in my free time today and report back. Do you mind if I include some helpful hints and or suggestions for the rest of your script? I may not get it working but tips and tricks are always nice lol. Anyway, I'll report back sometime today weather I looked at it or not.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Okay, so I got it working fine and dandy for me. The problem was that the window message will not load a new page or message until all windows are deactivated and not showing. So I used a simple native JS function setTimeout(); to wait 1 second before proceeding with the rest of the script. I attached the commented and fixed script to this post.

More about setTimeout();

As for hints and tips I left some comments, not much but enough to get an idea, if you'd like I can fix up the entire script to how I prefer and you can compare and possibly adopt some of the techniques I use. It won't be much of a rewrite just a few assurances in the plugin command side of things. As for readability, to me, it's hard to read and that is because you are putting your curly braces on the next line by itself, now this is okay for larger crammed scripts but with such a small script and not very large functions it's not really necessary. I only use curly braces on their own line if I feel the function could use better readability because it's so crammed, in your case your code would look cleaner like this.

JavaScript:
//You do it like this, which is not bad as long as you keep to this style and you find it easier to read.
if(isTrue)
{
//do something
}

// this is how I like doing it, unless of course the function is so crammed that it could use some extra space.
if(isTrue) {
//doSomething
}
There is more I could show you but that is a general idea, if I re-write the script you could see more of what I would do, it's up to you, I like to help, I know I could've used the tips and tricks when I first started making plugins.
 

Attachments

mnihas

Villager
Xy$
0.00
You know, I thought about giving a delay for completion of terminating previous windows, but didn'y know how. Refactoring hints are quite impotant - I'll do them by myself. Thank you!
 
Top