[Solved] Why does paySkillCost work different in battle?

[Solved]
I tried to make a script for a specific effect for skills with a specific notetag (I still don't know how to name it).
I tested, and if I activate the skill on the map, it works just fine, but in battle, I get "Cannot read property 'length' of null" error in the "if (match.lenght>1)" line.
Code:
    Game_BattlerBase.prototype.paySkillCost = function(skill)
    {
        this._mp -= this.skillMpCost(skill);
        this._tp -= this.skillTpCost(skill);

        var match = skill.note.match(/<test:\s*([0-9]+)\s*([0-9]+)\s*(\D*)s*>/i);
       if (match.length>1)

What is wrong there?
Let's break this down into parts:

"Cannot read property length of null" is like doing this:
Code:
if (null.length > 1)

null doesn't have a property called length because, well, it's null, it has no properties.

So then you would know that your variable called "match" has a value of null. Then you'll look to where that variable gets its value, and then look up the match function documentation.

From reading the documentation, you will see that the match function returns a value of null when no matches are found. So now you know that the notetag doesn't match the regular expression you wrote.

If you'd like us to help further, you'll have to share what's in the notetag.
Are you sure it works outside of battle? In a new project, I tried overwriting paySkillCost with your function and I get "Cannot read property length of null" both in the menu and in battle - as I would expect, for the reasons Aloe Guvner just explained.
If you'd like us to help further, you'll have to share what's in the notetag.
As stated before, the same skill in the map works just fine, so it's not about what's in the notetag.
The notetag is <test: 5 6 true>, I'll update the regex in the main post.

The regex simplified is: /<test: (\d+) (\d+) (\w*)>/i

Are you sure it works outside of battle? In a new project, I tried overwriting paySkillCost with your function and I get "Cannot read property length of null" both in the menu and in battle - as I would expect, for the reasons Aloe Guvner just explained.
It does work in the map just fine, but it doesn't in battle.
I'm testing on a brand new project too.
Are you trying on the actual function? I'm on a plugin script (I doubt this shows any different results, but who knows...).

[EDIT] I screenshoted the notetag
X6WgqKb.png

In a project I made this morning, I changed the actor limit to 35 and skills to 68. I picked a random empty skill by the middle, added that notetag and assigned it to a character. Then, I began to make the plugin script and eventually I found this issue.
It will throw the error whenever you use a skill that doesn't have a matching notetag. For example, if I give the heal skill a note of <test: 5 6 true>, then when I use heal from the menu, there's a match, so there's no error. However, if I go into battle, and have someone use heal, but everyone else guard, then the error will occur when someone uses guard, because the note for guard is: "Skill #2 will be used when you select↵the Guard command."

Since that does not produce a match, match is null, then you get null.length. You'll need to add a null check on match before doing any further processing.
It will throw the error whenever you use a skill that doesn't have a matching notetag. For example, if I give the heal skill a note of <test: 5 6 true>, then when I use heal from the menu, there's a match, so there's no error. However, if I go into battle, and have someone use heal, but everyone else guard, then the error will occur when someone uses guard, because the note for guard is: "Skill #2 will be used when you select↵the Guard command."

Since that does not produce a match, match is null, then you get null.length. You'll need to add a null check on match before doing any further processing.
OMG! There's a huge chance that's the issue!

[EDIT]Yes! That was the issue!
I had to write "match!=undefined&&match!=null&&match.length>1" instead!
Such a mistake...!
I had to write "match!=undefined&&match!=null&&match.length>1" instead!

You could shorten it to:
Code:
if (match && match.length > 1)

Although match is, itself, not a boolean, JavaScript will do type coercion and evaluates most values that aren't 0 to true. Undefined and null ultimately evaluate to false, so if match is undefined or null, it will evaluate to false, and if match is an Array, it will evaluate to true.

If you decide to keep your original way, I would add a second equals sign.
Code:
if (match !== undefined && match !== null && match.length > 1)
The reason being that JavaScript uses "===" and "!==" for strict equality, but "==" and "!=" for loose equality - that is, the double-equals does a lot of type coercion. Strict equality checks that both type and value are the same, but loose equality won't. For example:

Code:
5 == '5'; // true
5 === '5'; // false

Not that using != undefined and != null won't do what you want, but it's generally good practice to use strict equality.

Original thread: https://forums.rpgmakerweb.com/threads/solved-why-does-payskillcost-work-different-in-battle.105713/