[SOLVED] Converting Plugin Parameter to Function

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I want to ask something simple, probably. This is about plugin parameters. I know I can just look at a coder's plugin to see how it is done, but I want to do it the right way as I also know what it actually does. I've seen Yanfly use Graphics.height or Graphics.boxHeight as a plugin parameter. Some coders also use Graphics.boxHeight / 2, or something like: $gameActors.actor(1)._name.


These are in the plugin parameters so I know they are strings. What code should I do to convert them directly as functions? So when I do Graphics.height on the plugin parameter, it gets converted into a Number property, that values as the height of my screen?


TIA
 
Last edited by a moderator:

sagebrushfire

Villager
Member
Joined
Jun 12, 2016
Messages
25
Reaction score
12
First Language
English
Primarily Uses
I'm not 100% sure I get the question but I'll take a stab at it: 


It's in the JS file for the plugin. Plugin Parameters are special entries in the JavaScript comments section near the top. They look like this: 


 * @param Screen Height
 * @desc Adjusts the height of the screen.
 * Default: 624
 * @default 624


That's just a comment in JavaScript but in RMMV it shows up as a parameter in the Plugin Manager called "Screen Height". The @param part tells RMMV to make a new parameter. The @desc part tells RMMV to put the following text as the description. The third line has no @ symbol and does nothing in RMMV, it's just a note Yanfly added, probably just for developers so they know what the normal default is in case they change it. The fourth line @default tells RMMV what the default value for that parameter should be. 


To access this parameter from JavaScript, you have to use the built-in Plugin Manager function that's a part of the core scripts. Most plugin writers make new javascript variables and store these values in them for use later. Here's how Yanfly does it in the Core Engine: 


Yanfly.Parameters = PluginManager.parameters('YEP_CoreEngine');
Yanfly.Param = Yanfly.Param || {};
Yanfly.Icon = Yanfly.Icon || {}; // Ignore this line, it's not part of our lesson.

Yanfly.Param.ScaleBattleback = String(Yanfly.Parameters['Scale Battlebacks']);


That first line is the most important one. Yanfly uses RMMV's PluginManager.parameters() function and then inserts the name of the plugin. It will return a list of all the parameters that were defined in the comments section of that specific plugin. Yanfly then sets the value of a variable called Parameters to the result. The Parameters object is a property inside of the Yanfly object. 


Finally, for convenience, Yanfly creates a new object called Param and then proceeds to add a new property to it for each parameter, getting the parameter value with Yanfly.Parameters['parameter-name']. It may seem strange but the reason for the bracket notation instead of dot notation is because parameter names can have any sort of special character like a space or a dash. JavaScript property names are allowed to have just about any character you want but only phrases starting with an underscore or regular letters and only containing letters and numbers can be accessed via dot notation. If the property name has special characters, you have to put the name in quotation marks and access it with bracket notation. 


So these two are the exact same:


window.document; // The document Object which is a property of the window object.

window["document"]; // The document Object which is a property of the window object.

window.document.body; // The HTML <BODY> Tag

window["document"]["body"]; // The HTML<BODY> Tag.

window.document["body"]; // The HTML <BODY> Tag

// This demonstrates the 2 major ways of accessing object's properties in JavaScript.




The TL;DR Answer: 


Use the PluginManager.parameters('Your-Plugin-Name'); function, which is a part of the core RMMV scripts. That will return an object that contains all of your plugin parameters by name. Access them via bracket notation if they have any special characters like spaces or dashes. 
 
Last edited by a moderator:

DoubleX

Just a nameless weakling
Veteran
Joined
Jan 2, 2014
Messages
1,787
Reaction score
939
First Language
Chinese
Primarily Uses
N/A
How about something like this?


Class.config = new Function(parameters);


Then using parameters can be as simple as this:

Code:
Class.config();
this.config(); // Provided that this is Class itself or an instance of Class
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,100
Reaction score
13,705
First Language
English
Primarily Uses
RMMV
I just use eval.


var param = '$gameActors.actor(1)._name'


var actorName = eval(param)
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I just use eval.


var param = '$gameActors.actor(1)._name'


var actorName = eval(param)


this works properly, thank you. I'll mark this solved. :)
 

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

Latest Threads

Latest Profile Posts

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,882
Messages
1,017,230
Members
137,607
Latest member
Maddo
Top