MZ How do I get my code called by game flow OR Maximum Call Stack Size in empty plugins

I begun to make a lot of plugin scripts, but I've noticed many of them are incompatible together, getting "Maximum Call Stack Size" error.
I usually overwrite the default function like this:
Code:
var _savetheoriginalFUNCTION = FUNCTION; //Assume the function I call is named "FUNCTION", and it has no params

FUNCTION = function()
{
    //do whatever I want before it does the normal work
    _savetheoriginalFUNCTION.call(this);
    //do whatever I want after it does the normal work
};

The thing is, after doing this twice in different plugins, even if I name the vars _saveFUCTION1 and _saveFUNCTION2 over the same original function, I get the error popping.

For example, I'm currently working on a time/date system for ingame, and I need to save it into the savefile.
In order to do this, I worked on a mid-plugin as a core to enable a simpler management of the savefile for the developer.

Save Plugin snippet:
Code:
var _saveTheOriginalCreateGameObjects = DataManager.createGameObjects;

DataManager.createGameObjects = function()
{
    _saveTheOriginalCreateGameObjects.call(this); //Throws the error
    //$me_CustomSave= new ME_CustomSave();
};

Clock Plugin snippet:
Code:
var _saveTheOriginalSetupNewGame = DataManager.setupNewGame;

DataManager.setupNewGame = function() {
    _saveTheOriginalSetupNewGame.call(this); //If you do this in two plugins, with the same or different variable names, Max Call Stack error.
};


Sometimes, it even happens with no other plugins involved:

Code:
var _saveTheOriginalSceneMenuCreate=Scene_Menu.prototype.create;
Scene_Menu.prototype.create = function() {
        _saveTheOriginalSceneMenuCreate.call(this);
        this.createCalendarWindow(); //Create a customized window for the default menu with sample values.
    };

What other ways can I get my code called by normal game flow?
either by adding your plugin name as the variable prefix or use some wrapper.

variable with plugin name prefix example:
Code:
var myPluginName_SceneMenuCreate = Scene_Menu.prototype.create;
Scene_Menu.prototype.create = function() {
        myPluginName_SceneMenuCreate.call(this);
        this.createCalendarWindow(); //Create a customized window for the default menu with sample values.
    };

wrapper example
Code:
var myPluginName = myPluginName || {};
(function($){
$.SceneMenuCreate = Scene_Menu.prototype.create;
Scene_Menu.prototype.create = function(){
        $.SceneMenuCreate.call(this);
        this.createCalendarWindow(); //Create a customized window for the default menu with sample values.
    };

})(myPluginName);
sorry if the code a little mess... typing on phone 😛.

hope this help

edit: wait... you said you already use diferent name with your alias... it should already do the trick then... how many times you alias the same function in your project to get the maximum stack?


usually it happen when you install the same plugin twice or different plugin have the same alias name.

or something called an infinite loop.
I've seen a lot of it, but never tried the wrapper approach in my plugins, your example is quite explanatory.
As you noticed in your edit, I already try naming them different in each plugin. More often than not, at the second alias of the same function in different plugins, gives me the max call stack size error. Sometimes even at the first, and it usually goes all the way down to tell me the issue is in pixi.js rather than my own code.

For example, many of my plugins need to start specifically after "New Game", or "Load Game" are run. It happens quite a lot in aliasing new game related functions, in clean projects with 2 plugins which only alias the same function once. (Plugins that exactly have the first example with different names).

I did browse a lot in this forum about it, that's why I did try some of the stuff. I find it odd, it rarely happened to me on MV.
Maybe you got into an infinite loop by yourself.
You should check if the functions you are calling inside the aliased functions do not call the aliased function themselves again.

A full script would be also helpful for a better analysis.
I've seen a lot of it, but never tried the wrapper approach in my plugins, your example is quite explanatory.
As you noticed in your edit, I already try naming them different in each plugin. More often than not, at the second alias of the same function in different plugins, gives me the max call stack size error. Sometimes even at the first, and it usually goes all the way down to tell me the issue is in pixi.js rather than my own code.

For example, many of my plugins need to start specifically after "New Game", or "Load Game" are run. It happens quite a lot in aliasing new game related functions, in clean projects with 2 plugins which only alias the same function once. (Plugins that exactly have the first example with different names).

I did browse a lot in this forum about it, that's why I did try some of the stuff. I find it odd, it rarely happened to me on MV.
can you try in blank project... then add those two plugins BUT only with the aliased function ONLY (delete the other part of the plugin). does it still throw error? if yes then the culprit is indeed multiple aliased function.... but if it work then something in your 'other' code is looping infinitely and not the alias that causing it. hope this help.
I tried that before starting the thread. I thought I had explained already, but I guess I'm no good at explaining when I'm too obfuscated on why something that used to work in MV I can't make it again on MZ...
For example, many of my plugins need to start specifically after "New Game", or "Load Game" are run. It happens quite a lot in aliasing new game related functions, in clean projects with 2 plugins which only alias the same function once. (Plugins that exactly have the first example with different names).
I tried to explain I already tried that. I was most likely too ambiguous.

Maybe you got into an infinite loop by yourself.
You should check if the functions you are calling inside the aliased functions do not call the aliased function themselves again.

A full script would be also helpful for a better analysis.
Code:
var _saveTheOriginalSetupNewGame = DataManager.setupNewGame;

DataManager.setupNewGame = function() {
    _saveTheOriginalSetupNewGame.call(this); //If you do this in two plugins, with the same or different variable names, Max Call Stack error.
};
A full reading would help too. Although it's my fault for being so confused and not explaining properly...

Somehow forum did not save my edit.

I'm sorry I can't explain the issue more clearly, I'm too confused about things that used to go well for me on MV don't always work just so fine on MZ for no apparent reason, and my mind is really blocked. I can't even format the main post so it's easier to understand. I thank you both a lot for trying to understand my mess and help me.
I tried that before starting the thread. I thought I had explained already, but I guess I'm no good at explaining when I'm too obfuscated on why something that used to work in MV I can't make it again on MZ...

I tried to explain I already tried that. I was most likely too ambiguous.



A full reading would help too. Although it's my fault for being so confused and not explaining properly...

Somehow forum did not save my edit.

I'm sorry I can't explain the issue more clearly, I'm too confused about things that used to go well for me on MV don't always work just so fine on MZ for no apparent reason, and my mind is really blocked. I can't even format the main post so it's easier to understand. I thank you both a lot for trying to understand my mess and help me.
how about:
1. create blank project
2. add only 2 plugins conflicting
(only with the function aliased inside. delete the rest of the code and only leave the aliased function).
3. playtest to see if error happen
4. if the error replicated 100% all the time... upload the project in google drive or something
5. share the link so we can check it.

hope this help

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-get-my-code-called-by-game-flow-or-maximum-call-stack-size-in-empty-plugins.128366/