Parse Note To JSON On Startup v1.0
By kabuto202
As the title describes, this script parses DBs that within their schema has the "note" key. This will be useless you're writing code. The purpose of this was to make it easier for my designer to pass meta data to myself (the programmer). Given that regex parsing is bad and needs to be phased out completely, and that the popular "tag" styles are incapable of transmitting complex data, I figured that given we're using JS now, the JSON format would be optimal. Especially given the large amount of JSON editors out there for those who don't want to validate their JSON manually.
How To Use
Nothing special. Script runs on start-up. If you're also planning on using it on start-up, just check to see if the expected field value is present, since it has to wait for the DBs to actually be loaded before it can.
The data can be accessed by looking for the "noteObj" key in the same place in the hierarchy as "note" is.
Example:
Your ID 1 actor may have a note field that looks like:
{
"favoriteFood": "Potato",
"canTransformIntoMonsterIDs": [3,7,8],
"age": 22,
"skills": {
"acrobatics": 20,
"cooking": 30
}
}
Now in your JS code you can reference those values like this:
console.log($dataActors[1].noteObj.favoriteFood) //prints Potato
console.log($dataActors[1].noteObj.age) //prints 22
console.log($dataActors[1].noteObj.canTransformIntoMonsterIDs[0]) //prints 3
console.log($dataActors[1].noteObj.cooking) //prints 30
Code
Spoiler//=============================================================================
// ParseNoteOnRun.js
//=============================================================================
/*:
* @plugindesc Parses every table/DB who's schema contains the note tag. Data will be visible under dbSelection.nodeObj.
* Useful for testing but avoid usage in a production environment. I recommend materializing the data from the DBs
* ahead of time as part of your deployment process.
*
*
* @help MIT License
https://opensource.org/licenses/MIT
* @author kabuto202
*/
(function() {
var parameters = PluginManager.parameters('ParseNoteOnRun');
function parseNoteFromDbSelection(selection){
try{
if(!selection || !selection.note) {
return false;
}
return JSON.parse(selection.note);
}
catch (e) {
return false;
}
};
function runFunctionWhenDbsAreReady(next){
if(DataManager && DataManager.isDatabaseLoaded()){
next();
return;
}
setTimeout(runFunctionWhenDbsAreReady, 2000, next);
}
function parseNoteFromDbs(){
var dbsToParseFrom = [$dataActors, $dataClasses, $dataSkills, $dataItems, $dataWeapons, $dataArmors, $dataEnemies, $dataStates, $dataTilesets]; //Remove DBs from here if you wish to avoid parsing something
for(var i = 0; i < dbsToParseFrom.length; i++){
var db = dbsToParseFrom
;
if(!db) continue;
for (var j = 0; j < db.length; j++){
var obj = parseNoteFromDbSelection(db[j]);
if(obj){
db[j]["noteObj"] = obj;
}
}
}
};
runFunctionWhenDbsAreReady(parseNoteFromDbs);
})();
License
My code on this site is licensed under MIT https://opensource.org/licenses/MIT. Credits or letting me know that you're using my stuff in a commercial game would be cool tho.
本贴来自国际rpgmaker官方论坛作者:kabuto202处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:
https://forums.rpgmakerweb.com/threads/parse-note-to-json-on-startup.56663/