[SOLVED] Why isn't this common event playing before menu opens (JavaScript issue)?

(I wasn't sure if this topic belongs in this category or in JavaScript/Plugin Support or Learning JavaScript, since all of this code is specific to RMMV - sorry if I got it wrong)



I'm writing a plugin that can assign particular common events to run whenever certain in-game "triggers" occur. One of these "triggers" is the player opening the menu (the assigned common event being supposed to run immediately before the menu opens - i.e. I'm not trying to run the common event while the menu is open).

I've tried aliasing different functions involved in calling the menu, but I haven't been able to get the common event to run first and then proceed to open the menu after. It always either turns out that the menu opens first and then the common event runs after, or the common event runs and then the menu never opens at all.



These are some things I've tried (where $.parameter.menuOpen is the ID of the common event the developer assigned to the menu-opening "trigger" in the plugin parameters):

***These result in the menu opening first, then the common event running after closing it:
Spoiler: Code
JavaScript:
$.Scene_Map_callMenu = Scene_Map.prototype.callMenu;
Scene_Map.prototype.callMenu = function() {
    if ($.parameter.menuOpen > 0) {
        $gameTemp.reserveCommonEvent($.parameter.menuOpen);
    }
    $.Scene_Map_callMenu.call(this);
};
JavaScript:
$.Scene_Menu_initialize = Scene_Menu.prototype.initialize;
Scene_Menu.prototype.initialize = function() {
    if ($.parameter.menuOpen > 0) {
        $gameTemp.reserveCommonEvent($.parameter.menuOpen);
    }
    $.Scene_Menu_initialize.call(this);
};

***These result in the common event running, but the menu never opening:
Spoiler: Code
JavaScript:
$.Scene_Map_updateCallMenu = Scene_Map.prototype.updateCallMenu;
Scene_Map.prototype.updateCallMenu = function() {
    if (this.isMenuEnabled()) {
        if (this.isMenuCalled()) {
            if ($.parameter.menuOpen > 0) {
                $gameTemp.reserveCommonEvent($.parameter.menuOpen);
            }
        }
    }
    $.Scene_Map_updateCallMenu.call(this);
};
JavaScript:
$.Scene_Map_updateScene = Scene_Map.prototype.updateScene;
Scene_Map.prototype.updateScene = function() {
    if (!SceneManager.isSceneChanging()) {
        this.updatePreMenuEvent();
    }
    $.Scene_Map_updateScene.call(this);
};

Scene_Map.prototype.updatePreMenuEvent = function() {
    if (this.isMenuEnabled()) {
        if (this.isMenuCalled()) {
            if ($.parameter.menuOpen > 0) {
                $gameTemp.reserveCommonEvent($.parameter.menuOpen);
            }
        }
    }
};



Does anybody have any idea why none of these are working, and/or how I could get it working instead? I'm just trying to figure out where to place the script call to call the common event, where the system knows the menu is about to be opened, but before the scene actually changes so it's not too late to run the event - but then still actually open the menu afterward.



As for reasoning, for those curious: This is just a general purpose plugin (expanded from a specific use I started writing it for for myself, which did not involve the menu opening/closing). Therefore, I don't personally have a specific reason to run a common event before the menu opens, but I can think of some reasons other people might want to do this (generally involving checking values that might affect how they want the menu to appear or behave), so I want to provide that option. As one example, someone might want saving to be disabled when the player is standing in a certain region; so instead of constantly running a parallel event to continuously check what region they're in and enable/disable saving accordingly, they could just use this plugin to run that check and enable/disable saving only when the player opens the menu.

All that said, it isn't the end of the world if I can't figure this out. I could just omit this "trigger" from the plugin - although it doesn't feel complete without it.



Thanks in advance for all help/advice! 👋
Hi there!

This is happening due to the nature of single-thread javascript. Although there are "parallel" events, nothing is executed parallel on javascript code in the RPG Maker context. Everything is executed on line, in order.

Spoiler: Explanation
To be more specific, the thing is when you reserve the common event to be called, it is not called/executed right away. It is reserved.
There is a proper function that will check if there is any common event reserved, then if it does, it will call/execute that common event.

That function is probably inside the Scene_Map.prototype.updateMain.

So, when you reserve the common event, the game needs to get to the point where it will check for the reserved common events and play them.

The problem with your approach is that the code responsible to play the common event is before the code that calls the menu. Something like that:
  1. check if there is any common event reserved:
  2. if yes, then execute it.
  3. if no, then proceed.
  4. reserve the common event
  5. executes the aliased call Menu function
This is run on the update loop. but when you call the menu, the scene changes, breaking the loop. And the loop will only continue again when you enter the scene map again.

To solve this, you can do the same approach the main code does for the menu:

JavaScript:
const Scene_Map_start = Scene_Map.prototype.start // Alias
Scene_Map.prototype.start = function() {
    Scene_Map_start.call(this)
    this.needMenuCommonEvent = true // New prop to check the need of a common event before the menu opens.
}

const Scene_Map_callMenu = Scene_Map.prototype.callMenu // Alias
Scene_Map.prototype.callMenu = function() {
    if(this.needMenuCommonEvent){ // If needs the common event, reserve it.
        $gameTemp.reserveCommonEvent(1)
        this.needMenuCommonEvent = false // The common event is already reserved, so we can set this to false.
    }else{
        Scene_Map_callMenu.call(this)
    }
}
Hi there!

This is happening due to the nature of single-thread javascript. Although there are "parallel" events, nothing is executed parallel on javascript code in the RPG Maker context. Everything is executed on line, in order.

Spoiler: Explanation
To be more specific, the thing is when you reserve the common event to be called, it is not called/executed right away. It is reserved.
There is a proper function that will check if there is any common event reserved, then if it does, it will call/execute that common event.

That function is probably inside the Scene_Map.prototype.updateMain.

So, when you reserve the common event, the game needs to get to the point where it will check for the reserved common events and play them.

The problem with your approach is that the code responsible to play the common event is before the code that calls the menu. Something like that:
  1. check if there is any common event reserved:
  2. if yes, then execute it.
  3. if no, then proceed.
  4. reserve the common event
  5. executes the aliased call Menu function
This is run on the update loop. but when you call the menu, the scene changes, breaking the loop. And the loop will only continue again when you enter the scene map again.

To solve this, you can do the same approach the main code does for the menu:

JavaScript:
const Scene_Map_start = Scene_Map.prototype.start // Alias
Scene_Map.prototype.start = function() {
    Scene_Map_start.call(this)
    this.needMenuCommonEvent = true // New prop to check the need of a common event before the menu opens.
}

const Scene_Map_callMenu = Scene_Map.prototype.callMenu // Alias
Scene_Map.prototype.callMenu = function() {
    if(this.needMenuCommonEvent){ // If needs the common event, reserve it.
        $gameTemp.reserveCommonEvent(1)
        this.needMenuCommonEvent = false // The common event is already reserved, so we can set this to false.
    }else{
        Scene_Map_callMenu.call(this)
    }
}
Thank you SO MUCH! This explanation makes sense and helped me figure it out! I had to tweak things a bit to get it working but I still used your general method:

JavaScript:
$.Scene_Map_start = Scene_Map.prototype.start;
Scene_Map.prototype.start = function() {
    $.Scene_Map_start.call(this);
    if ($.parameter.menuOpen > 0) {
        this.needPreMenuEvent = true;
    }
};

$.Scene_Map_updateMain = Scene_Map.prototype.updateMain;
Scene_Map.prototype.updateMain = function() {
    $.Scene_Map_updateMain.call(this);
    if (this.needCallMenu) {
        Scene_Map.prototype.callMenu.call(this);
    }
};

$.Scene_Map_callMenu = Scene_Map.prototype.callMenu;
Scene_Map.prototype.callMenu = function() {
    if (this.needPreMenuEvent){
        $gameTemp.reserveCommonEvent($.parameter.menuOpen);
        this.needCallMenu = true;
        this.needPreMenuEvent = false;
    } else {
        if (!$gameMessage.isBusy()) {
            this.needCallMenu = false;
            $.Scene_Map_callMenu.call(this);
            this.needPreMenuEvent = true;
        }
    }
};

Thank you again, this issue is solved now 😍

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-why-isnt-this-common-event-playing-before-menu-opens-javascript-issue.149662/