0

I'm using RPG Maker MV which is a game creator that uses JavaScript to create plugins. I have a plugin in JavaScript already, however I'm trying to edit a part of the plugin so that it basically checks if a certain string exists in a character in the game and if it does, then sets specific variables to numbers within that string.

for (var i = 0; i < page.list.length; i++) {
    if (page.list[i].code == 108 && page.list[i].parameters[0].contains("<post:" + (n) + "," + (n) + ">")) {
        var post = page.list[i].parameters[0];
        var array = post.split(',');
        this._origMovement.x = Number(array[1]);
        this._origMovement.y = Number(array[1]);
        break;
    };
};

So I know the first 2 lines work and contains works when I only put a specific string. However I can't figure out how to check for 2 numbers that are separated by a comma and wrapped in '<>' tags, without knowing what the numbers would be.

Then it needs to extract those numbers and assign one to this._origMovement.x and the other to this._origMovement.y.

Any help would be greatly appreciated.

2 Answers 2

1

This is one of those rare cases where I'd use a regular expression. If you haven't come across regular expressions before I suggest reading an introduction to them, such as this one: https://regexone.com/

In your case, you probable want something like this:

var myRegex = /<post:(\d+),(\d+)>/;
var matches = myParameter.match(myRegex);
this._origMovement.x = matches[1];   //the first number
this._origMovement.y = matches[2];   //the second number

The myRegex variable is a regular expression that looks for the pattern you describe, and has 2 capture groups which look for a string of one or more digits (\d+ means "one or more digits"). The result of the .match() call gives you an array containing the entire match and the results of the capture groups.

If you want to allow for decimal numbers, you'll need to use a different capture group that allows for a decimal point, such as ([\d\.]+), which means "a sequence of one or more digits and decimal points", or more sophisticated, (\d+\.?\d*), which is "a sequence of one or more digits, following by an optional decimal point, followed by zero or more digits).

There are lots of good tutorials around to help you write good regular expressions, and sites that will help you live-test your expressions to make sure they work correctly. They're a powerful tool, but be careful not to over-use them!

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help! For some reason the character is not returning to the correct position specified as <post:45,11>.
I can't help with the rest of your code, but I've tested the regular expression on a regex testing site and it correctly extracts the numbers "45" and "11". One possible thing to try - convert them to numbers like this this._origMovement.x = parseFloat( myRegex[1] ); - it may be treating them as strings which could be weird for your code.
Ah ok, still no luck. I've got this as the first line if (page.list[i].code == 108 && page.list[i].parameters[0].contains(myRegex)). Is this correct? I notice you've got myParameters which is undefined. What would this be set to?
No, you don't want to use contains(). Do var match = page.list[i].parameters[0].match( myRegex ); and then check whether match is defined: if ( page.list[i].code == 108 && match ) {
Sorry, I made an error in my answer (now fixed) - you need to do this._origMovement.x = matches[1], not =myRegex[1].
|
0

Got it to work. For anyone who may ever be interested, the code is below.

    for (var i = 0; i < page.list.length; i++) {
        if (page.list[i].code == 108 && page.list[i].parameters[0].contains("<post:")) {
            var myRegex = /<post:(\d+),(\d+)>/;
            var matches = page.list[i].parameters[0].match(myRegex);
            this._origMovement.x = matches[1];   //the first number
            this._origMovement.y = matches[2];   //the second number
            break;
        }
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.