Preserved forum archive. This topic stores the original first post and locally mirrored RPG Maker Web attachments when available. It is posted by the BMMPlay archive account, not by the original creator.
Original Source
- Original title: My first plugin (Macros), feedback required
- Original author: Thyzer
- Original date: January 29, 2016
- Source thread: https://forums.rpgmakerweb.com/threads/my-first-plugin-macros-feedback-required.55927/
- Source forum path: Game Development Engines > RPG Maker Javascript Plugins > Learning Javascript
Summary
Hey, I am currently learning Javascript and especially with making plugins there are many things new to me. For example saving a function, overwriting it with my own code and then execute the saved code... After about 3-4 hours I tinkered a plugin I call "Macros".
Archived First Post
Hey,
I am currently learning Javascript and especially with making plugins there are many things new to me.
For example saving a function, overwriting it with my own code and then execute the saved code...
After about 3-4 hours I tinkered a plugin I call "Macros".
Macros will basically replace text from noteboxes with other text you define in a common event.
I want feedback from you about my coding style. I personally think some things in my code are "dirty" and want to make it different, but dont know how.
Maybe it's not that bad or not possible another way...
How to use:
My Questions:
- When I want a macro with more than one parameter, and both parameters should contain a longer text (and thus containing comma)... how do I distinguish between "comma for the text" and "comma to seperate parameters".
- Is there another plugin like this, which might be more stable/useful/... which I can take a look at? I havent found anything myself.
- Is there a way, that this plugin doesnt require to be the first in plugin-load-order?
- Do you find it useful?
- Did you find any bugs/mistakes/messy code?
- Do you know how to solve something better then I did?
Source Code:
View attachment THY_Macros.js
I am currently learning Javascript and especially with making plugins there are many things new to me.
For example saving a function, overwriting it with my own code and then execute the saved code...
After about 3-4 hours I tinkered a plugin I call "Macros".
Macros will basically replace text from noteboxes with other text you define in a common event.
I want feedback from you about my coding style. I personally think some things in my code are "dirty" and want to make it different, but dont know how.
Maybe it's not that bad or not possible another way...
How to use:
- Install this plugin before anything that reads noteblocks!!!
- Make a Common Event and add a "Comment". In this comment you can now define a macro.
-
Protected download
- Adjust the Common Event ID in the plugin parameter.
-
Protected download
- Use the macro in any notebox
-
Protected download
Protected download
My Questions:
- When I want a macro with more than one parameter, and both parameters should contain a longer text (and thus containing comma)... how do I distinguish between "comma for the text" and "comma to seperate parameters".
- Is there another plugin like this, which might be more stable/useful/... which I can take a look at? I havent found anything myself.
- Is there a way, that this plugin doesnt require to be the first in plugin-load-order?
- Do you find it useful?
- Did you find any bugs/mistakes/messy code?
- Do you know how to solve something better then I did?
Source Code:
//=============================================================================
/*:
* @plugindesc v0.01 This plugin enables you to define macros for notetags.
* @author Thyzer
*
* @param Macro Defines
* @desc In which common event are your macros defined?
* @default 1
*
* @help
* ============================================================================
* Introduction
* ============================================================================
*
* Sometimes we want to use a lot of notetags in our notes. Often we need to
* copy and paste the same notes in other noteboxes. With this plugin you may
* define a macro, which will replace a word with your predefined notetags.
* This removes clutter in the notebox and makes it easier changing values when
* you want the same notetags in different places.
*
* ============================================================================
* Defining Macros
* ============================================================================
*
* Use one comment for each define. The define should look like this:
*
* <name>(<parameter1>, <parameter2>) = <anything> <parameter1> <anything> <parameter2> <anything>
*
* Name: The name can contain any letters a-z, A-Z and 0-9.
* - The name is case sensitive (intentional)
*
* Parameters: You can define as much parameters as you want, they are split with a ",".
*
* The result string, can consist of basically anything you want. If the name
* of a parameter occurs it will be replaced by the text you enter when using
* the macro.
*
* Examples:
*
* ESLOTS = <Equip Slot>
* Weapon
* Armor
* Accessory
* Accessory
* </Equip Slot>
*
* DESC(txt) = <Help Description>
* txt
* </Help Description>
*
* ============================================================================
* Using Macros
* ============================================================================
*
* Simply use the name of the macro in any notebox, it will be replaced
* and parameters will be inserted.
*
* Examples:
*
* ESLOTS
* ==>
* <Equip Slot>
* Weapon
* Armor
* Accessory
* Accessory
* </Equip Slot>
*
* DESC(A hunter in the dark.)
* ==>
* <Help Description>
* A hunter in the dark.
* </Help Description>
*
* ============================================================================
* Changelog
* ============================================================================
*
* Version 0.01:
* - Public release for feedback.
*/
//=============================================================================
var Thyzer = Thyzer || {};
Thyzer.Macros = Thyzer.Macros || {};
Thyzer.Macros.params = PluginManager.parameters("THY_Macros");
Thyzer.Macros.define_common_event = Number(Thyzer.Macros.params["Macro Defines"]);
Thyzer.Macros.regex = /([\w]+)(?:\((.+)\))?/g;
Thyzer.Macros.param_split_regex = /[\s]*\,[\s]*/
Thyzer.Macros.define_regex = /([\w]+)(?:\(((?:[\w]+[\,\s]*)*)\))?(?:[\s]+\=[\s]+)([\s\S]+)/;
Thyzer.Macros.defines = null;
Thyzer.Macros.processMacros = function(group) {
for (var n = 1; n < group.length; n++) {
var obj = group[n];
var notedata = obj.note;
for (var j = 0; j < this.defines.length; j++)
{
var macro = Thyzer.Macros.defines[j];
notedata = notedata.replace(this.regex, function replaceFunction(whole, title, params)
{
if (title === macro.title)
{
var repl_str = macro.replace_str;
if (params != null) {
var args = params.split(this.param_split_regex, macro.params.length);
for (var k = 0; k < macro.params.length; k++)
{
repl_str = repl_str.replace(macro.params[k], args[k]);
}
}
return repl_str;
}
return whole;
});
}
obj.note = notedata;
}
};
Thyzer.Macros.getMacros = function() {
var defines = [];
var ce = $dataCommonEvents[this.define_common_event];
for (var i = 0; i < ce.list.length; i++) {
var elem = ce.list;
if (elem.code === 108)
{
var str = elem.parameters[0];
while (ce.list[i+1].code === 408)
{
str = str.concat("\n" + ce.list[1+i++].parameters[0]);
}
var macr = this.getMacro(str);
if (macr != null) {
defines.push(macr);
}
}
}
return defines;
}
Thyzer.Macros.getMacro = function(comment) {
if (comment.match(this.define_regex)) {
var macr_prms = [];
var title = RegExp.$1;
var params = RegExp.$2;
var replace_str = RegExp.$3;
var prms = params.split(this.param_split_regex);
for (var i = 0; i < prms.length; i++) {
macr_prms.push(prms);
}
var macr = {title:title, params:macr_prms, replace_str:replace_str}
return macr;
}
return null;
}
Thyzer.Macros.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
if (!Thyzer.Macros.DataManager_isDatabaseLoaded.call(this)) return false;
Thyzer.Macros.defines = Thyzer.Macros.getMacros();
Thyzer.Macros.processMacros($dataActors);
Thyzer.Macros.processMacros($dataClasses);
Thyzer.Macros.processMacros($dataSkills);
Thyzer.Macros.processMacros($dataItems);
Thyzer.Macros.processMacros($dataWeapons);
Thyzer.Macros.processMacros($dataArmors);
Thyzer.Macros.processMacros($dataEnemies);
Thyzer.Macros.processMacros($dataStates);
Thyzer.Macros.processMacros($dataTilesets);
return true;
};
/*:
* @plugindesc v0.01 This plugin enables you to define macros for notetags.
* @author Thyzer
*
* @param Macro Defines
* @desc In which common event are your macros defined?
* @default 1
*
* @help
* ============================================================================
* Introduction
* ============================================================================
*
* Sometimes we want to use a lot of notetags in our notes. Often we need to
* copy and paste the same notes in other noteboxes. With this plugin you may
* define a macro, which will replace a word with your predefined notetags.
* This removes clutter in the notebox and makes it easier changing values when
* you want the same notetags in different places.
*
* ============================================================================
* Defining Macros
* ============================================================================
*
* Use one comment for each define. The define should look like this:
*
* <name>(<parameter1>, <parameter2>) = <anything> <parameter1> <anything> <parameter2> <anything>
*
* Name: The name can contain any letters a-z, A-Z and 0-9.
* - The name is case sensitive (intentional)
*
* Parameters: You can define as much parameters as you want, they are split with a ",".
*
* The result string, can consist of basically anything you want. If the name
* of a parameter occurs it will be replaced by the text you enter when using
* the macro.
*
* Examples:
*
* ESLOTS = <Equip Slot>
* Weapon
* Armor
* Accessory
* Accessory
* </Equip Slot>
*
* DESC(txt) = <Help Description>
* txt
* </Help Description>
*
* ============================================================================
* Using Macros
* ============================================================================
*
* Simply use the name of the macro in any notebox, it will be replaced
* and parameters will be inserted.
*
* Examples:
*
* ESLOTS
* ==>
* <Equip Slot>
* Weapon
* Armor
* Accessory
* Accessory
* </Equip Slot>
*
* DESC(A hunter in the dark.)
* ==>
* <Help Description>
* A hunter in the dark.
* </Help Description>
*
* ============================================================================
* Changelog
* ============================================================================
*
* Version 0.01:
* - Public release for feedback.
*/
//=============================================================================
var Thyzer = Thyzer || {};
Thyzer.Macros = Thyzer.Macros || {};
Thyzer.Macros.params = PluginManager.parameters("THY_Macros");
Thyzer.Macros.define_common_event = Number(Thyzer.Macros.params["Macro Defines"]);
Thyzer.Macros.regex = /([\w]+)(?:\((.+)\))?/g;
Thyzer.Macros.param_split_regex = /[\s]*\,[\s]*/
Thyzer.Macros.define_regex = /([\w]+)(?:\(((?:[\w]+[\,\s]*)*)\))?(?:[\s]+\=[\s]+)([\s\S]+)/;
Thyzer.Macros.defines = null;
Thyzer.Macros.processMacros = function(group) {
for (var n = 1; n < group.length; n++) {
var obj = group[n];
var notedata = obj.note;
for (var j = 0; j < this.defines.length; j++)
{
var macro = Thyzer.Macros.defines[j];
notedata = notedata.replace(this.regex, function replaceFunction(whole, title, params)
{
if (title === macro.title)
{
var repl_str = macro.replace_str;
if (params != null) {
var args = params.split(this.param_split_regex, macro.params.length);
for (var k = 0; k < macro.params.length; k++)
{
repl_str = repl_str.replace(macro.params[k], args[k]);
}
}
return repl_str;
}
return whole;
});
}
obj.note = notedata;
}
};
Thyzer.Macros.getMacros = function() {
var defines = [];
var ce = $dataCommonEvents[this.define_common_event];
for (var i = 0; i < ce.list.length; i++) {
var elem = ce.list;
if (elem.code === 108)
{
var str = elem.parameters[0];
while (ce.list[i+1].code === 408)
{
str = str.concat("\n" + ce.list[1+i++].parameters[0]);
}
var macr = this.getMacro(str);
if (macr != null) {
defines.push(macr);
}
}
}
return defines;
}
Thyzer.Macros.getMacro = function(comment) {
if (comment.match(this.define_regex)) {
var macr_prms = [];
var title = RegExp.$1;
var params = RegExp.$2;
var replace_str = RegExp.$3;
var prms = params.split(this.param_split_regex);
for (var i = 0; i < prms.length; i++) {
macr_prms.push(prms);
}
var macr = {title:title, params:macr_prms, replace_str:replace_str}
return macr;
}
return null;
}
Thyzer.Macros.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
if (!Thyzer.Macros.DataManager_isDatabaseLoaded.call(this)) return false;
Thyzer.Macros.defines = Thyzer.Macros.getMacros();
Thyzer.Macros.processMacros($dataActors);
Thyzer.Macros.processMacros($dataClasses);
Thyzer.Macros.processMacros($dataSkills);
Thyzer.Macros.processMacros($dataItems);
Thyzer.Macros.processMacros($dataWeapons);
Thyzer.Macros.processMacros($dataArmors);
Thyzer.Macros.processMacros($dataEnemies);
Thyzer.Macros.processMacros($dataStates);
Thyzer.Macros.processMacros($dataTilesets);
return true;
};
View attachment THY_Macros.js
Downloads / Referenced Files
Log in to download
Log in, then follow the RPG Maker Developers Group to see these download links.
Log in to downloadReferenced Images / Attachments
Creator Claims / Removal
If you are the original creator and want this listing reassigned, edited, or removed, join BMMPlay and contact the moderators with proof that matches the original RPG Maker Web profile, linked GitHub, itch.io page, or another public creator identity.
Replies (0)
No replies yet.
0
replies
1
view
Topic Summary
Loading summary...