File name independency for Parameters

Mellye

Veteran
Veteran
Joined
Oct 24, 2015
Messages
347
Reaction score
279
First Language
Portuguese
As we know, to get our parameters from the PluginManager, we need to call

PluginManager.parameters(plugin_name)With the plugin_name being defined by the name of the file (sans extension).

For the sake of ease of sharing plugins, it would be fantastic if users didn't need to worry about saving plugins with the exact correct name.

So a good course of action would be if we just used some method inside our plugin to detect its own filename. There are a bunch of ways to do it in JavaScript, but they are all a bit "workaround-y".

I'm away from my computer for a while, so I can't test it right now. Is there a way to get our plugin own filename that would works properly, and wouldn't cause issues, in RMMV?
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
As we know, to get our parameters from the PluginManager, we need to call

PluginManager.parameters(plugin_name)With the plugin_name being defined by the name of the file (sans extension).

For the sake of ease of sharing plugins, it would be fantastic if users didn't need to worry about saving plugins with the exact correct name.

So a good course of action would be if we just used some method inside our plugin to detect its own filename. There are a bunch of ways to do it in JavaScript, but they are all a bit "workaround-y".

I'm away from my computer for a while, so I can't test it right now. Is there a way to get our plugin own filename that would works properly, and wouldn't cause issues, in RMMV?
you can do it, yes. lavra made a separate plugin to do this, but I'm working on getting a nice, automatic solution into the MVCommons. 
 

GaryCXJk

Veteran
Veteran
Joined
Dec 24, 2012
Messages
88
Reaction score
46
First Language
Dutch
Primarily Uses
Here's the post I made in another topic.

Okay, I've got an atrocity of a code right here that should do the ultimate trick or treat.

Essentially, what it does is, it will go through each method until it either finds the plugin parameters or it just gives up and uses the fallback.

_pluginName is the name of the plugin, or the standard plugin name. _propNames is an array containing all essential parameters (properties of the parameters object), and _defaultParams is an object containing the default values.

First, it just uses PluginManager. Next, it uses document.currentScript, only available on Webkit and Gecko based browsers, as well as Safari and Opera. Then, it uses the description of the plugins. If during that same loop there's nothing in the description for some reason, it checks all properties if they exist. That step could possibly be skipped, though, but still. Added the possibility. Finally, if all fails, it just says, meh, screw that, and uses _defaultParams.

It's not fool proof, but it's as close as it can get without having to resort to using AJAX to get the contents of the script.
 

var _pluginName = 'CXJ_Exit'; var _propNames = ['Text - Exit', 'Text - To Desktop', 'Add to title', 'Add to Game End']; var _defaultParams = { 'Text - Exit' : 'Exit', 'Text - To Desktop' : 'To Desktop', 'Add to title' : 'true', 'Add to Game End' : 'true' }; var _getParameters = function(pluginName, propNames, defaultParams) { /* Private function that checks plugin content */ var _checkPluginContent = function(parameters) { for(var prop in parameters) { if(parameters.hasOwnProperty(prop)) { return true; } } return false; } var parameters = PluginManager.parameters(pluginName); if(_checkPluginContent(parameters)) { return parameters; } var currentScript = document.currentScript; if(currentScript) { var scriptName = document.currentScript.src; scriptName = scriptName.substr(scriptName.indexOf('js/plugins/') + 11); scriptName = scriptName.substr(0, scriptName.lastIndexOf('.js')); parameters = PluginManager.parameters(scriptName); } if(_checkPluginContent(parameters)) { return parameters; } for(var idx = 0; idx < $plugins.length; idx++) { var plugin = $plugins[idx]; var params = plugin.parameters; if(plugin.description.indexOf('<' + pluginName + '>') > -1) { return params; } var hasFound = true; for(var idx = 0; idx < _propNames.length; idx++) { if(!params.hasOwnProperty(propNames[idx])) { hasFound = false; break; } } if(hasFound) { return params; } } return _defaultParams; } var parameters = _getParameters(_pluginName, _propNames, _defaultParams);By the way, code's CC0.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
First, it just uses PluginManager. Next, it uses document.currentScript, only available on Webkit and Gecko based browsers, as well as Safari and Opera. Then, it uses the description of the plugins. If during that same loop there's nothing in the description for some reason, it checks all properties if they exist. That step could possibly be skipped, though, but still. Added the possibility. Finally, if all fails, it just says, meh, screw that, and uses _defaultParams.
Why specify default parameters if it can't find the correct plugin?


As far as I'm concerned, if it can't find the plugin, it should not be considered installed and should either throw an error or make sure it isn't executed.


If you're falling back to defaults that are hardcoded in the code, the user is going to be confused why the plugin works, but their settings aren't being loaded.


I'd rather just throw an error if they don't name it properly.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Take a look at the topic linked in my signature. No need to include another plugin, anymore. The general idea is to include an id value inside the plugin description and iterate over the plugin array.


Since the array contains the plugin name (filename), description and parameters, we can use the description id to get our parameters.
 
Last edited by a moderator:

GaryCXJk

Veteran
Veteran
Joined
Dec 24, 2012
Messages
88
Reaction score
46
First Language
Dutch
Primarily Uses
Why specify default parameters if it can't find the correct plugin?

As far as I'm concerned, if it can't find the plugin, it should not be considered installed and should either throw an error or make sure it isn't executed.

If you're falling back to defaults that are hardcoded in the code, the user is going to be confused why the plugin works, but their settings aren't being loaded.

I'd rather just throw an error if they don't name it properly.
It's up to the plugin scripter to decide. Leaving out the parameter will just default to JavaScript returning undefined, after which you can throw an error.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,849
Messages
1,016,981
Members
137,563
Latest member
cexojow
Top