Parse Note To JSON On Startup

kabuto202

Villager
Member
Joined
Dec 20, 2012
Messages
6
Reaction score
1
First Language
English
Primarily Uses
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

//=============================================================================
// 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. 
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,770
Reaction score
12,203
First Language
English
Primarily Uses
RMMV
I do not understand either the requirement/purpose for this script, or how it's meant to be used.  Please rephrase your post to make it clear what this script does, why it's needed, and how it's meant to be used.  Maybe giving examples of what might be set up in the data, and then how it would be used during the game, might make things more clear.  


I don't really see that this does anything different to what MV already does (the note object is already available for use after the database has been loaded).
 

kabuto202

Villager
Member
Joined
Dec 20, 2012
Messages
6
Reaction score
1
First Language
English
Primarily Uses
I don't really see that this does anything different to what MV already does (the note object is already available for use after the database has been loaded).
Note is not an object. It is a string.


The purpose of this script is front-load the parsing for ease of data access, so you don't have to manually run JSON.parse() every time you're trying to get a json from the note field at game-time. 
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
859
First Language
German
Primarily Uses
Given that regex parsing is bad and needs to be phased out completely


I don't see this. Tags are easier to use for non-scripters and allow to easily differentiate between data used by different plugins, while JSON would need to be like an array of objects, each containing an "id" key to identify them.
 

kabuto202

Villager
Member
Joined
Dec 20, 2012
Messages
6
Reaction score
1
First Language
English
Primarily Uses
I don't see this. Tags are easier to use for non-scripters and allow to easily differentiate between data used by different plugins, while JSON would need to be like an array of objects, each containing an "id" key to identify them.
Regex is bad. 


If your non-programmer is having difficulty with writing in object notation they can use http://jsoneditoronline.org/ to generate it from them through a nice user-friendly UI. As for data used by different plug-ins, there is no inherit increase chance of overlap with a JSON, and you can even protect yourself by simply putting your data in under a nested key instead of using generic keys.


Also, in-case you haven't looked at the code. The meta command that you use to pull tags runs the regex every single time without any caching or storing, and given how slow regex is to begin with, you're running performance risks if you're planning on deploying to weaker hardware and/or mobile.


Tags are a product from a bygone age of being stuck with Ruby for RMVX. Embrace JS, forget Ruby. 


But ultimately, it's up to you whether to use it or not. I come from a software engineering background that has it's prejudices, and I'm writing my code how I feel is the "correct" way to do it. I know my non-programmer fellas are smart enough to write in a simple notation, which at the end of the day help us convey complex data that's not possible to do cleanly with regex. 
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
859
First Language
German
Primarily Uses
I don't see how your linked thread provides any arguments.


Besides, it's not "my" non-programmer, it might be any user on these forums, and if i expect each of them to learn how to write JSON, i shouldn't even start writing that plugin. Some of my more complex ones (localization, achievements) need an external JSON file and are used considerably less.


Besides, tags are not "a product from a bygone age". They are markup, like HTML, that's there to express something. And since their is no other way to parse tags, the arguments stated in your thread don't apply.
 

kabuto202

Villager
Member
Joined
Dec 20, 2012
Messages
6
Reaction score
1
First Language
English
Primarily Uses
I don't see how your linked thread provides any arguments.
1) Don't use Regex when a parser is available because a parser will always be faster and more accurate. 2) Regex can't do data validation. 3) Regex is slow.

Besides, it's not "my" non-programmer, it might be any user on these forums, and if i expect each of them to learn how to write JSON, i shouldn't even start writing that plugin. Some of my more complex ones (localization, achievements) need an external JSON file and are used considerably less.
Like I said, writing a JSON is not hard. If it is hard, there are WYSIWYG tools available for it. To clarify, this is simply a by-product of the work I'm doing. Given that this plug-in is basically purposed for programmers working with people, I doubt it will have a high usability rate.

Besides, tags are not "a product from a bygone age". They are markup, like HTML, that's there to express something. And since their is no other way to parse tags, the arguments stated in your thread don't apply.
No. They are not markup. They are regex. In Ruby there was no other way to parse strings. In JS we have an alternative, we have JSON. It is a standard for a reason. In fact, it's such a standard, that the entire freaking language is written around it. So much so, that you actually reserialize the DB files, so you don't have to parse anything for the production build, and dramatically improve performance. 
 

Victor Sant

Veteran
Veteran
Joined
Mar 17, 2012
Messages
1,694
Reaction score
1,442
First Language
Portuguese
Primarily Uses
"regex is bad" this type of thing is pure.... i think people will know what i will say here.


regex is bad when it's used poorly. Generally the maker developers use simple regex and as long they're not on updates they cause no performance loss.


Regex is a tool to be used, and saying that we should never use it is simply wrong. There are times it should be used, there are times it shouldn't.


It's quite fun that the link you posted, Shows some examples of when regex shouldn't be used (the reasonable answers never say "never uses regex no matter what") and guess what? None of the scenarios apply to the maker users.


And you're totally wrong about meta, DataManager.extractMetadata parses the notes when the file is loaded and store it, and unless the plugin developer uses regex to parse value stored, no regex is executed by the maker itself after that.


Also working with json is not easy for non-programmers (wich are the targets of plugins, people with programming knowledge do their own plugins) as you are assuming. I know that by experience, on the times of rmxp when there was no notebox, people had to structure setup on the script editor itself in a similar way as a json do (arrays with arbitrary ids and data) and the setup was done directly on the editor. A lot of people had a hard time grasping how things were done. And a single wrong comma, and the setup is screwed and non-programmers will not have any idea of what is wrong.


To finish, the way we uses notetags are also widely spread through developers and non-programmers maker users, and reinventing the wheel now won't do any benefit.


The way you suggest people doing things are just for the sake of making things "the way you belive is right", but in programming there is not a single "right way" to do something, there are many right ways to the same things and you just saying that the way everyone else is wrong and your is right.
 
Last edited by a moderator:

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

Latest Threads

Latest Posts

Latest Profile Posts

buinguyenhoangtho wrote on fizzly's profile.
Hello @fizzly, i would like to use your asset/ resource for my project ( is maybe commercial ). May i have your permission? Thanks you !
I have 470+ hours on RMMV but have only made a demo game, is this normal? :kaoswt: (I lose motivation a lot plus I use a 32gb laptop, so..)
I like Iron Maiden.
Benku wrote on Magnus0808's profile.
Hey man you helped me out a long time ago with a plugin i was wondering if you can help me out one more time since its literally the last thing i need help with if not is cool but id figured id ask you one more soild.
Stream will be live shortly with a guest stream! Tonight, a new guest joins~ Feel free to drop by!

Forum statistics

Threads
95,425
Messages
929,067
Members
125,660
Latest member
AphoticAmaranth
Top