Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[MV] JSON from Commands snippet

Not a full Plugin, but a utility for Plugin developers that want to use JSON with MV plugin commands.

Plugin Commands are separated by spaces and do not take quotes into account, which means data structures aren't very compatible.

This snippet parses that array of space-separated command arguments and gives you something that's been sanitised to support JSON.

The way a JSON structure starts is a white-space followed by an open-brace character {
"abc{" will not work, but {{ will
There is no error detection or JSON validation, so if there is a missing closing brace the entire string will be appended on.

I have tried to cover every weird case to avoid bugs, but if you find a weird-case bug please say so.

Usage: var args = ParseCommandArgs( ["plugin", "command", "array"] );
Code:
var ParseCommandArgs = function( inArgs ) {

    var argStr = inArgs.join( ' ' ), args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0;

    for ( var ii = 0, argStrLen = argStr.length; ii < argStrLen; ii++ ) {

        switch ( argStr[ii] ) {

        case '<span style="color: #000099; font-weight: bold;">\'':

            if ( depth ) {

                apo = 1 - apo; // String disables JSON

            }

            break;

        case '"':

            if ( depth ) {

                quot = 1 - quot; // String disables JSON

            }

            break;

        case '{':

            if ( !nonWhiteSpace && !( quot + apo ) ) {

                depth++; // Entered JSON

            }

            break;

        case '}':

            if ( depth > 0 && !( quot + apo ) ) {

                depth--; // Left JSON

            }

            break;

        default:

            if ( !depth && <span style="color: #0066FF;">/\s/.test( argStr[ii] ) ) {

                args.push( argBuild ); // Add to args

                argBuild = ""; // Reset arg builder

                nonWhiteSpace = 0; // We found white space so next char could be JSON {        

                continue;

            }

            break;

        }

        argBuild += argStr[ii]; // Concat next character

        if ( !depth ) { 

            nonWhiteSpace++;

        }

    }

    if ( argBuild.length > 0 ) {

        args.push( argBuild ); // Add final string

    }

    return args;

};

An example command could be:
MyPlugin character { 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }
Everything in that JSON structure will be sanitised into a single string and all the arguments around it will be strings as usual.

Result : ["MyPlugin", "character", "{ 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }"]
Without this snippet : ["MyPlugin", "character", "{", "'name'", ":", "'xilef',", "'level'", ":", "0,", "'stats'", ":", "{", "'hp'", ":", "123", "}", "}"]

Here's an optimised version of the snippet:
Code:
var ParseCommandArgs = function( inArgs ) {

    inArgs = inArgs.join( " " );

    for ( var args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0, ii = 0, argStrLen = inArgs.length; ii < argStrLen; ii++ ) {

        switch ( inArgs[ii] ) {

        case "'":

            depth && ( apo = 1 - apo );

            break;

        case '"':

            depth && ( quot = 1 - quot );

            break;

        case "{":

            nonWhiteSpace || quot + apo || depth++;

            break;

        case "}":

            0 < depth && !( quot + apo ) && depth--;

            break;

        default:

            if ( !depth && <span style="color: #0066FF;">/\s/.test( inArgs[ii] ) ) {

                args.push( argBuild );

                argBuild = "";

                nonWhiteSpace = 0;

                continue;

            }

        }

        argBuild += inArgs[ii];

        depth || nonWhiteSpace++;

    }

    0 < argBuild.length && args.push( argBuild );

    return args;

};
 
Forgot to mention; this only sanitises the string. To get a Javascript object out of the JSON you need to do something like this;

Code:
args = ParseCommandArgs( args );

 

var data = JSON.parse( args[index_of_json] );

for ( var key in data ) {

    my_object[key] = data[key];

}

Princess Amy":3jfoizrm said:
Nice, it's always bugged me how limited plugin commands are.
This is part of an attempt to make them more useful.

On one hand it solves the problem of lack of API space in previous RPG Maker versions, on the other hand it's very difficult to get a sane usage scenario figured out for it - especially when the script option is just above and can do the same + more than the plugin command system.

Adding data structure support is a small way to alleviate that, at the expense of JSON's complexity being dragged into it (ugh).
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top