设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 90|回复: 0

[转载发布] Script Organization & Subfolders

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    7959

    主题

    864

    回帖

    3万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    29925
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    38778

    灌水之王

    发表于 3 天前 | 显示全部楼层 |阅读模式
    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:       
    1.     // LoadScript function; load a specified script...
    2.     loadScript = function(path,name) {
    3.         var url = path + name + ".js";
    4.         var script = document.createElement('script');
    5.         script.type = 'text/javascript';
    6.         script.src = url;
    7.         script.async = false;
    8.         script._url = url;
    9.         document.body.appendChild(script);
    10.     };
    复制代码


    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:       
    1.     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:       
    1.     loadScript(path,'dmn_MapGenerator_Extensions');
    2.     loadScript(path,'dmn_MapGenerator_Caves');
    3.     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:       
    1.   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:       
    1. * @param MapGenerator Tunnels
    2. *
    3. * @param windyness
    4. * @desc How "windy" is the tunnel?
    5. * @default 3
    6. * @parent MapGenerator Tunnels
    7. *
    8. *
    9. * @param MapGenerator Caves
    10. *
    11. * @param loops
    12. * @desc How many loops to perform?
    13. * @default 5
    14. * @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/

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-1 02:37 , Processed in 0.103916 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表