Script Organization & Subfolders
Break large scripts down to component files and keep things more organized
Fair warning, this isn't a "plug & play" plugin; this is more instruction than explicit file.
About
I come to RPGM from a lot of web development in PHP and other various odd languages and experiences. As a matter of personal preference, I like to break larger projects down into smaller files with relevant functions and elements in each. It makes it easy to manage the code, find what I need when I need it, etc. RPGM does this with the core files, for instance. Most languages make things easy with simple include commands, allowing you to store a bunch of common functions in another file, and then load those, or to store relevant files in an organized subfolder and call those via an include with the subdirectory.
Getting into RPGM that isn't exactly the case for plugins, however, and as some script projects have grown keeping those organized in one file has proven to be...frustrating. I'm doing a lot of extensions to the Sanshiro MapGenerator script, for instance, and it was getting kind of annoying to have my plugin list "cluttered" with all of these extra MapGenerator files. I'd want to consolidate them, have a set of files that would be in a subfolder and then "included" in any other language, hence the project here.
After rooting around a little bit, I sorted out a solution to my own issue, and I thought I'd share. It may be something other folks have already sorted out, but I've asked a lot of Qs and found a lot of great scripts here so I thought I'd share the approach.
Honestly, its a lot more technique than it is dedicated code; the core bit to it all comes straight from the RPGMaker core in how it loads the plugins and attaches it to the game.
Process
I'll walk you through the process of setting up a script to behave this way. I'm sure there's more ways to do this, and do more with it, but this is where I am now with it all. For our example, I'm going to take all of my extension files for the MapGenerator script, drop them in a subfolder, and "include" them.
Step 1: The Script!
We setup a script file for our purposes. I refer to these as a "collector" script, because this script collects all of my parts together for use. Name the script according to what it is you're doing. I use my screen name abbreviation and we're handling my MapGenerator extensions, so I name my file as "dmn_MapGeneratorSuite.js".
Into this, I paste a version of the plugin loading code from the RPG Maker MV Core:
JavaScript:
- // LoadScript function; load a specified script...
- loadScript = function(path,name) {
- var url = path + name + ".js";
- var script = document.createElement('script');
- script.type = 'text/javascript';
- script.src = url;
- script.async = false;
- script._url = url;
- document.body.appendChild(script);
- };
复制代码
Simple function to load scripts, easy peasy
Step 2: Subfolders!
Next, I setup my sub folder in my "js/plugins" folder, which will hold all of my component files; since this is my map generator extensions, I'll name it "MapGenerator". All of my files I want to include are put in here.
In my collector script, I'll add the following line to establish our file path.
JavaScript:
- var path = 'js/plugins/MapGenerator/';
复制代码
We include the full path from the base directory, so "js/plugins/" as well as YourFolderName.
Step 3: Includes!
In my case, as noted, I wrote some extensions for the MapGenerator, to render caves and tunnels; I put all of the relevant code for those in the files "dmn_MapGenerator_Caves.js" and "dmn_MapGenerator_Tunnels.js" as well as a set of some extension functions to change up behaviors I wanted different.
So, I put those files in the "MapGenerator" folder.
In my collector script, "dmn_MapGeneratorSuite.js", we start adding lines for each of our files in the sub folder:
JavaScript:
- loadScript(path,'dmn_MapGenerator_Extensions');
- loadScript(path,'dmn_MapGenerator_Caves');
- loadScript(path,'dmn_MapGenerator_Tunnels');
复制代码
Note that we exclude the ".js" in the call; the loadScript function already applies that to the file name to load.
Step 4: Parameters!
This one is a little bit of back and forth if you're breaking down existing files, but maybe a little easier if you're doing things from scratch.
So the first step is ensuring the included files can access parameters. Because of where they are now, these won't get loaded by the Plugin Manager so you won't be able to directly set the parameters for the individual files. "dmn_ManGenerator_Caves" would load in kind of blind.
So step 1 here (step 4a?) is to ensure the included files can access any parameters. Top of each of those files, add a line to access the parameters of your collector plugin, in this case:
JavaScript:
- var parameters = PluginManager.parameters('dmn_MapGeneratorSuite');
复制代码
This one is going to be highly tailored to your needs; the variable can be named whatever you want, you just need to load the parameters from your collector script, in my case "dmn_MapGeneratorSuite." Load your parameters as normal.
Step 2 (step 4b?) is to include all of your parameters this little subfile wants defined in your collector script, NOT the script in the subfolder. This will allow you to set those in the Plugin Manager.
I'll usually set mine up with a parent for the subfolder file, and make each parameter a child of that for ease of organization. Thus, in my "dmn_MapGeneratorSuite.js" I'll have...
JavaScript:
- * @param MapGenerator Tunnels
- *
- * @param windyness
- * @desc How "windy" is the tunnel?
- * @default 3
- * @parent MapGenerator Tunnels
- *
- *
- * @param MapGenerator Caves
- *
- * @param loops
- * @desc How many loops to perform?
- * @default 5
- * @parent MapGenerator Caves
复制代码
In the Plugin Manager then, all of my subfile parameters are neatly organized.
Step 5: The End?
Yup, that's about it.
Your final file will have your parameters, the loadScript function, the "path" variable definition, and the loadScript calls to your files in the subfolder. Your files in the subfolder will just need the call to load the parameters for your collector file, but no parameter definitions of their own.
Use Cases
So where is this useful? Not every script will benefit from the breakdown, but there are some that will.
Per the example, I'm doing a number of things with the Sanshiro MapGenerator script to generate new kinds of maps. This resulted in a lot of items in my Plugin Manager and made it a little messy. The MapGenerator itself, as well, is a bit of a large file and has a fairly complete second "object" for "RoomsAndPass". Breaking the script down like this improves my management and readability, and makes it easier to access specific parts of the script without digging through the larger file.
Another element I'm working on involves adapting d20/D&D style elements to RPG Maker. Using this process, I have been able to break down the affected portions of the game to their own files, "gameActor" for the functions interacting with the Game_Actor objects, "gameEnemy" for the Game_Enemy objects, etc.
All of these would or could normally be included in one file under normal circumstances. There's no need to separate their loading for script stacking/interacting reasons, so they can all be loaded at once. The scripts ARE loaded in the order you set the loadScript calls, so if order matters for your own purposes you can control that within the collector script.
Where this doesn't work well is cases where you WANT to split the loading order up. Some plugins interact with the same things so the load order is important. You may need to sandwich your own scripts between two other plugins, for instance, and load one at the very end. This process/example would load all of those items at once, as one file essentially, so it doesn't play nicely there.
Can't I just download this?
No, this is a pretty custom deal for some more advanced script writing and management. I suppose I (or someone else) could make a more generalized version and define a "subfolder" parameter and a list of files, but there's still the matter of ensuring the parameter hooks are in that main collector file, and the parameter access is valid in all of those sub files. AND, building each one like this as opposed to a straight "plug & play" plugin means you can do this for multiple large scripts; I run this for my MapGenerator extensions, the d20/D&D code, and a few other scripts that have some heft to them. It proves quite useful in loading all sorts of those little scripts; I maintain a "dmn_Tools" copy of this and keep a folder of all of those little one-line or small function "plugins" we tend to accumulate. This lets me keep them each in their own file for quick and easy maintenance but keep them collected so that my Plugin Manager isn't a list of a dozen petty plugins or one file of pure randomness.
Potential Improvements?
As mentioned above, this could be built into a general plugin in various ways, not that I'm a big advocate for it. A downloadable template maybe, but I highly recommend building what you need instead of relying on a generic plugin.
If you wanted to get creative, you can probably find ways using fs to just read the sub directory and load each file as found~ That could make life and maintenance easy as each time you add new files those are automatically included without needing to update the main collector script. This is just my easy version, and with the setup to load each file deliberately, and in a given order, I get to retain some of that control of loading things in order in the Plugin Manager.
License & Use
I'm not staking any special claim to this. The loading code comes straight from RPG Maker's core files, I just generalized the plugin and outlined an idea/technique. As such, this would fall under the normal clauses for use of RPG Maker's code.
If you WANT to toss a shoutout in the credits as a matter of gratitude or politeness, "With thanks to Nate Petersen (daMoose_Neo)" and a message here would be welcome, its nice to know I was of assistance to someone.
本贴来自国际rpgmaker官方论坛作者:daMooseNeo处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:
https://forums.rpgmakerweb.com/threads/script-organization-subfolders.164786/