MV Using 2 or more of the same of my own plugin... best practices?

Soooo mostly out of laziness and a bit out of just curiosity...

Anyone know of some best practices for coding a plugin to be added to the plugin manager multiple times like this:
1651630867450.png


and allow them to both have there own separate parameters used and function nicely together.. instead of the way it currently works for me, which is it just does what is setup with the top one.

lol Hope that makes sense...
I mean I of course know I could code the parameters into being an array in just one plugin and just add the plugin file once or code it differently.. but it just had piqued my curiosity.. on what the best way to code a plugin to allow it to do such would be or if possible.

A more in-depth look at how the plugin manager code works would suggest this isn't really even possible without recoding the PluginManager class?

..well hope I don't get bashed to hard here.. just a random thought I was having.

Hope this question at least makes sense though
Let me know if there are any "best practices" you know of for coding a plugin to be added to the plugin manager multiple times with different parameters to get it to still work nicely.

Thanks for reading 🙂

Edit:
It's not even possible to create a plugin to change the "PluginManager.setup" function is it? lol
that would just have to be hardcoded directly into the managers.js file wouldn't it? since the plugin isn't even added before that is called... since that is what adds the plugins themselves
lol dang... well guess even that is out of the question then

...wait maybe a plugin that reloads the "PluginManager.setup" function... lol hmm... that I suppose could work sort of..

Edit2:
Summary of what I've gathered to be "best practices"/ easiest to allow multiple used plugins of the same plugin:

Making a special trait in the plugin that is to become a multiple used/added plugin, such as a special parameter name

Make a new PluginManager setup that then loops the plugin list again and adds/loads the plugins that aren't in the PluginManager._scripts list and adds them with an index

When making the plugin that is to become a multiple used/added plugin make sure to get the index to be used for the parameters and function names.

I'm sure there is more to detail out but that's the basic gists of it for now.
This post contains the example plugins I made on this subject.
I don't see why you'd want to use the same plugin multiple times with different settings, and have no idea how you'd tell the engine which settings to use at any given point.

You're better off editing the plugin parameters and changing basically all you have there into a struct object and then allowing the end user to set different regions with different tints.

By setting the parameters to use an array of structs with the below parameters, you can effectively make infinite different tint regions.

tint1 = {regions: [x,y,z], opacity: 200, tone: [0,0,0], overlay: true}

Here's more info on struct objects in plugin parameters: link (ctrl+f struct)
I don't see why you'd want to use the same plugin multiple times with different settings, and have no idea how you'd tell the engine which settings to use at any given point.

You're better off editing the plugin parameters and changing basically all you have there into a struct object and then allowing the end user to set different regions with different tints.

By setting the parameters to use an array of structs with the below parameters, you can effectively make infinite different tint regions.

tint1 = {regions: [x,y,z], opacity: 200, tone: [0,0,0], overlay: true}

Here's more info on struct objects in plugin parameters: link (ctrl+f struct)
Thanks for your reply but...

I'm quite aware of struct objects and such lol

Yep, what you refer to is what I've done in many of my plugins, most notably the most practical one is my timer system

and I would imagine if I did allow the user to use the same plugin multiple times I would still have to gather all the data from each of them and act accordingly

This region tint was just an example... it's not the plugin necessarily that would utilize such a feature

The reason I made this topic was really more a less of a brain storm and to see what would be best practices for such an occurrence to happen...

Not that I'm not grateful for your reply but just because you don't see why one might want to use the same plugin multiple times doesn't mean there might not be a reason... actually though you kind of touched on the main subject of this post... best practice of telling the engine what to do is part of what I would hope to gain from this post.

A key example of using the same plugin multiple times is Orange HUD by HUDell found here:

Though in that plugin grouping the user is to copy and rename the same plugin file then change the parameters. That would be exactly one good reason for what I'm asking if any one knows best practices

As I did say above
I mean I of course know I could code the parameters into being an array in just one plugin and just add the plugin file once or code it differently.. but it just had piqued my curiosity.. on what the best way to code a plugin to allow it to do such would be or if possible.

I'm quite aware of what I could do to make one plugin take multiple of it's same parameters by setting up an array of structs/objects but I just feel like someone might also have some good insight on a "best" way to code in the case of using the same plugin, even if it had to be copied and renamed like in Hudell's Orange HUD.

Idk it's probably just a wasted thread... cuz as you did also state... there really isn't much reason to do what I'm asking most of the time since it could always be coded into just one plugin

...but just because I'm curious I'd like to know if anyone has any input on making a plugin that can basically refer to itself to see if it had been used and if so then add data to itself perhaps. idk

Gawd I sound snide in this post, I really don't mean for it to come across like that and value what you wrote... it's just that yes I'm aware of all that though... but I'm basically just trying to see if and what is best if I did want to ever have the end user be able to use multiple of a same plugin for some reason down the line.

Again thanks for the reply though 🙂
Adding more variations of the same plugin isn't impossible. You could reprogram your pluginmanager's loading function. However, it is stupid. You're still going to be left with just one working plugin, because Javascript will overwrite the code from plugin #1 by code from plugin #2. If you wanted them to "work nicely together", you'd essentially have to alias your own functions. And you'd be aliasing any "not your own" functions twice, creating an unnecessary overhead and unlocking immense potential for breaking plugins, because there isn't anything like conditional function definition in Javascript.

In particular aliasing one function twice will result in the function calling itself in an infinite loop.
Adding more variations of the same plugin isn't impossible. You could reprogram your pluginmanager's loading function. However, it is stupid. You're still going to be left with just one working plugin, because Javascript will overwrite the code from plugin #1 by code from plugin #2. If you wanted them to "work nicely together", you'd essentially have to alias your own functions. And you'd be aliasing any "not your own" functions twice, creating an unnecessary overhead and unlocking immense potential for breaking plugins, because there isn't anything like conditional function definition in Javascript.

In particular aliasing one function twice will result in the function calling itself in an infinite loop.
Now were gettin' somewhere 😀

Yes exactly what I was lookin' for is an answer like this.
Stupid may be.. but that's exactly why I actually created this thread is to get around it being "stupid"

Yes indeed exactly as you stated it would have to alias most likely my own functions.. but what to do to prevent them from breaking... I know there is a good way to do this. That's where the "best practices" part comes into this

I would imagine still upon loading them all then I would still have to created a master structure/object that they would then all use to prevent from becoming an infinite loop or memory leak, etc.

Basically I'm gonna try this mostly just because.. not ever saying it will be useful though... but mostly just for my own knowledge
...but who know's maybe someday the knowledge will be helpful for some future plugin idk

Sidenote:
The only plugin/set of plugins I have ever seen that such was I'm asking about and is actually done well and has a practical use is definitely OrangeHud by @Hudell
Though even that did have to have the user copy and paste and rename the files in order to get around the plugin manager limits

Many thanks for both your replies ☺️
(I truly do mean that, even if the text doesn't read that way)
I think from a 'best practices' standpoint, your best bet would be to code a single plugin that defines the functions, and then a second one that can be imported multiple times to alias those functions. Of course, you'd still need to at least change the name of each additional import of the plugin, and figure your way around getting each copy of the plugin to actually get its own plugin parameters, there is a way to do this though, so it's not impossible.

But like I said, I'm not sure why you'd want to do this at all. As the person making the plugin, you already have the ability to change the plugin parameter to use struct objects, and know all about them, so there's no reason why you should build a plugin on the assumption that if the end user wants to use it in multiple ways, they should just import it multiple times. You have the ability to make the parameters as complex or as simple as you want, and it's not making anything easier for the end user to have to jump through weird hoops to import the same plugin with different names multiple times.
I think from a 'best practices' standpoint, your best bet would be to code a single plugin that defines the functions, and then a second one that can be imported multiple times to alias those functions. Of course, you'd still need to at least change the name of each additional import of the plugin, and figure your way around getting each copy of the plugin to actually get its own plugin parameters, there is a way to do this though, so it's not impossible.

But like I said, I'm not sure why you'd want to do this at all. As the person making the plugin, you already have the ability to change the plugin parameter to use struct objects, and know all about them, so there's no reason why you should build a plugin on the assumption that if the end user wants to use it in multiple ways, they should just import it multiple times. You have the ability to make the parameters as complex or as simple as you want, and it's not making anything easier for the end user to have to jump through weird hoops to import the same plugin with different names multiple times.
Well said ☺️

..it's more of a just because I'm curious as to how to do and what the "best practice" is.
I believe your right though... might be best to have a "parent" plugin and then all the "children"

Yeah that's my goal is to not have to make the end user have to rename or copy/paste to different names.
..but really looks like that is quite difficult since the way the plugin manager is setup to only chose the first on the list a plugin.... hmm...
I think I'm just really looking for a way to change the pluginmanager... and that's more of just because I want to see it done

yep I think for me this is just more of a challenge to attempt and figure it out then anything too practical.. mostly just because and no other reason.. but maybe I'll make use of it at some point idk

athough I think this is really starting to depend of the plugin, only certain times this could even be at all sort of useful... but yeaa... the more I even try to justify it to myself I also am like... it's not worth it lmao 🤣

even I'm like just create one plugin with a good parameter set/build
(that does quite obviously make the most sense of course)

idk lol... I guess I just wanted to see what everyone could think of and see if there is something I might have been missing
Well, aliasing your own function isn't difficult.
Code:
var x = x || functionName;
on global level is going to be valid code that will prevent the infinite loops. Perhaps it could even solve the issue of aliasing other peoples' functions twice, because this way your alias should never call itself. However, this will conflict with you wanting each instance to have unique parameters, forcing you to push them inside an array or sth anyway.

The only way where I see you could avoid placing the parameters in an array... would be to create an array of objects, have each plugin create a new object and define each function as a member of the object's member function. AKA.., Much worse implementation of class, because this approach is extremely inefficient. With 50 instances of the plugin you'd have 50 objects with the same member functions, the only difference being their parameters.

EDIT: There is one alternative that could completely remove a need for arrays. However, I am scared to say it.
Well, aliasing your own function isn't difficult.
Code:
var x = x || functionName;
on global level is going to be valid code that will prevent the infinite loops. Perhaps it could even solve the issue of aliasing other peoples' functions twice, because this way your alias should never call itself. However, this will conflict with you wanting each instance to have unique parameters, forcing you to push them inside an array or sth anyway.

The only way where I see you could avoid placing the parameters in an array... would be to create an array of objects, have each plugin create a new object and define each function as a member of the object's member function. AKA.., Much worse implementation of class, because this approach is extremely inefficient. With 50 instances of the plugin you'd have 50 objects with the same member functions, the only difference being their parameters.
Awesome, yep that's pretty good info for this ☺️
Sooo now what about breaking the limitation of the plugin manager?

What would you do if you wanted to get around the issue of not being able to load the same plugin twice?

Since the PluginManager calls "setup" in the main.js file first thing
Would it be next best to then make a new setup that would look through all the plugins again?
and if that is the case...
then PluginManager.setParameters would also need an index along with the plugin name given to it also I imagine

& would this be then called from Scene_Boot?

Edit:
EDIT: There is one alternative that could completely remove a need for arrays. However, I am scared to say it.
Oh??
Well, the pluginmanager isn't that much of an issue. Remove
Code:
 !this._scripts.contains(plugin.name)
from the manager's setup function and have a variable that tracks how many instances of the plugin with this name have been called. Then append this variable value at the end of the plugin name's parameters.
Well, the pluginmanager isn't that much of an issue. Remove
Code:
 !this._scripts.contains(plugin.name)
from the manager's setup function and have a variable that tracks how many instances of the plugin with this name have been called. Then append this variable value at the end of the plugin name's parameters.
Yep done that ☺️

The real part that get's tricky.. is doin' that from a plugin & not just editing the rpg_managers.js file Since the plugins get loaded right in main... but I'm attempting to reload them now I suppose on Scene_Boot using an alternative function that does exactly what you just described
Why not just load a custom javascript file through the index.html and override the pluginmanager's function like that if you want to keep the pluginmanager.js's integrity? Much easier. If you want to reload the plugins, you'd have to undefine every important variable and then I guess you could call the setup routine again, since it's already defined.
Why not just load a custom javascript file through the index.html and override the pluginmanager's function like that if you want to keep the pluginmanager.js's integrity? Much easier. If you want to reload the plugins, you'd have to undefine every important variable and then I guess you could call the setup routine again, since it's already defined.
Well just because I'm being difficult I suppose lmao
... I don't want the theoretical end-user to have to change any files... just add plugins and go like adding normal plugins

Though this will most likely still never need happen lol
...because I probably won't ever release anything that utilizes any of what is discussed in this topic but still just a test to see how to do it I guess

Edit:
Does this approach seem decent so far?
See attached files.
... I don't want the theoretical end-user to have to change any files... just add plugins and go like adding normal plugins
You won't be able to avoid that. You'll need to unload every plugin, which will require to revert the function definitions back to core scripts and delete every custom function and global variable defined by these plugin makers. Unless you're willing to program a convoluted plugin that cycles through every window._memberVariable and filter out the essentials defined by RM core engine and the browser, user input will be required. And fighting against custom functions is even more difficult, because they are not variables. Finally, there are IIFE's.

EDIT: Nevermind, I'm just dumb. It's not as hot as I made it seem and reverting to corescripts should be enough. However, IIFE's will pose a challenge.
You won't be able to avoid that. You'll need to unload every plugin, which will require to revert the function definitions back to core scripts and delete every custom function and global variable defined by these plugin makers. Unless you're willing to program a convoluted plugin that cycles through every window._memberVariable and filter out the essentials defined by RM core engine and the browser, user input will be required. And fighting against custom functions is even more difficult, because they are not variables. Finally, there are IIFE's.
Oh dang I see... hmm.. well in that case...
I guess now I need to know how to unload every plugin.. so that I can to reload them I guess?

Aside from editing the main.js or the index.html file... is Scene_Boot the next best to unload/reload them?

Edit:
What if I just unload mine? Then reload them but using the next setup function that makes use of indexing?
You will forcefully need to undefine every custom function. Which is easy in objects functions. You simply undefine the entire object. Scene_Boot can be undefined and redefined. Yanfly global object can be undefined and redefined.
Code:
window["Yanfly"] = undefined;
But it will require you to undefine the global variables, so you'd have to cycle through them. And pay extra care not to undefine any variables defined by the browser or other essential variables. Although since you can't guarantee people not tampering with Scene_Boot, SceneManager and other things, even these essentials need to go.

So no, Scene_Boot is not good for it. You will need a custom scene. If you're in Scene_Boot as your current scene and undefine Scene_Boot, the game would crash as SceneManager would try to call Scene_Boot.prototype.update, which is now undefined.

So call a custom "scene" (it shouldn't be a scene tied to rpg maker corescripts at all so it doesn't crash once you remove the definitions) from Scene_Boot. And there undefine everything not tied to the function and reload and redefine everything. It's going to be a real hassle. And what's worse, it doesn't take care of non-object global level function definitions and top level IIFE's.
You will forcefully need to undefine every custom function. Which is easy in objects functions. You simply undefine the entire object. Scene_Boot can be undefined and redefined. Yanfly global object can be undefined and redefined.
Code:
window["Yanfly"] = undefined;
But it will require you to undefine the global variables, so you'd have to cycle through them. And pay extra care not to undefine any variables defined by the browser or other essential variables. Although since you can't guarantee people not tampering with Scene_Boot, SceneManager and other things, even these essentials need to go.

So no, Scene_Boot is not good for it. You will need a custom scene. If you're in Scene_Boot as your current scene and undefine Scene_Boot, the game would crash as SceneManager would try to call Scene_Boot.prototype.update, which is now undefined.

So call a custom "scene" (it shouldn't be a scene tied to rpg maker corescripts at all so it doesn't crash once you remove the definitions) from Scene_Boot. And there undefine everything not tied to the function and reload and redefine everything. It's going to be a real hassle. And what's worse, it doesn't take care of non-object global level function definitions and top level IIFE's.
Thanks for all the info. ☺️

I went a somewhat simpler route...
Seems to be working great so far... though I could possibly run into some issues later but with the basic testing it's working quite smooth ☺️
Pretty much I ran a new Plugin Manager Setup Function from Scene_Boot, didn't unload anything... then just loaded ones that were already on the list and had a "special" parameter name in the plugin file. (in this example that is *test*)

Check it out... so far it just turns on switches when starting a map just to test:
Spoiler: Plugin Manager List & Plugin Settings
1651652658774.png
1651652629548.png

Code:
Spoiler: PluginManager Extended
JavaScript:
var CTB = CTB || {}; CTB.PluginManagerEx  = CTB.PluginManagerEx || {};
var Imported = Imported || {}; Imported["CT_Bolt PluginManagerEx"] = 1.00;

"use strict";
(function ($_$) {
    function getPluginParameters() {var a = document.currentScript || (function() { var b = document.getElementsByTagName('script'); return b[b.length - 1]; })(); return PluginManager.parameters(a.src.substring((a.src.lastIndexOf('/') + 1), a.src.indexOf('.js')));}
    $_$.params = getPluginParameters();
      
    PluginManager.setupEx = function(plugins) {      
        var index = 1;
        plugins.forEach(function(plugin) {
            if (plugin.status && this._scripts.contains(plugin.name)) {
                if (plugin.parameters.hasOwnProperty("*test*")){
                    this.setParametersEx(plugin.name, plugin.parameters, index);
                    this.loadScript(plugin.name + '.js');
                    this._scripts.push(plugin.name + ' ' + index);
                    index++;
                };
            }
        }, this);
    };
  
    PluginManager.setParametersEx = function(name, parameters, index) {
        this._parameters[name.toLowerCase() + ' ' + index] = parameters;
    };
  
    $_$['Scene_Boot.prototype.initialize'] = Scene_Boot.prototype.initialize;
    Scene_Boot.prototype.initialize = function() {
        $_$['Scene_Boot.prototype.initialize'].apply(this, arguments);
        PluginManager.setupEx($plugins);
    };
      
})(CTB.PluginManagerEx, this);
Spoiler: Plugin Manager Test Demo Plugin
JavaScript:
var CTB = CTB || {}; CTB.PluginManagerTest  = CTB.PluginManagerTest || {};
var Imported = Imported || {}; Imported["CT_Bolt PluginManagerTest"] = 1.00;
//=============================================================================//

/*:
 * @plugindesc [RPG Maker MV] [Tier 1] [Version 1.00] [CT_Bolt - Plugin Manager Test]
 * @author CT_Bolt
 *
 * @param *test*
 * @desc *test*
 * @default
 *
 * @param Turn On Switch When Map Starts
 * @desc Switch to Turn On When Map Starts
 * @default
 *
 * @help
 * CT_Bolt's PluginManagerTest
 * Version 1.00
 * CT_Bolt
 *
 *
 */
//=============================================================================
//=============================================================================

"use strict";
(function ($_$) {
    $_$.data = $_$.data || [];
    $_$.data.push({params:getPluginParameters()});
    var index = $_$.data.length-1;
  
    function getPluginParameters() {
        var a = document.currentScript || (function() {
            var b = document.getElementsByTagName('script'); return b[b.length - 1]; }
        )();
        var id = $_$.data.length > 0 ? ' ' + $_$.data.length : '';
        return PluginManager.parameters(a.src.substring((a.src.lastIndexOf('/') + 1), a.src.indexOf('.js'))+id);
    }

    if (index > 0){
        $_$['Scene_Map.prototype.createSpriteset ' + index] = Scene_Map.prototype.createSpriteset;
        Scene_Map.prototype.createSpriteset = function() {
            $_$['Scene_Map.prototype.createSpriteset ' + index].apply(this, arguments);
            // DO STUFF
        };
      
        $_$['Scene_Map.prototype.start ' + index] = Scene_Map.prototype.start;
        Scene_Map.prototype.start = function() {
            $_$['Scene_Map.prototype.start ' + index].apply(this, arguments);
            var switchId = $_$.data[index].params['Turn On Switch When Map Starts'];
            $gameSwitches.setValue(switchId, true);
        };      
    };
  
})(CTB.PluginManagerTest, this);
Attached are the plugin files.

Original thread: https://forums.rpgmakerweb.com/threads/using-2-or-more-of-the-same-of-my-own-plugin-best-practices.147305/