[Solved] Why does paySkillCost work different in battle?

myenemy

Veteran
Veteran
Joined
Jan 17, 2014
Messages
67
Reaction score
30
First Language
Spanish
Primarily Uses
RMMV
[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?
 
Last edited:

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
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.
 

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
640
Reaction score
353
First Language
English
Primarily Uses
RMMV
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.
 

myenemy

Veteran
Veteran
Joined
Jan 17, 2014
Messages
67
Reaction score
30
First Language
Spanish
Primarily Uses
RMMV
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

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.
 
Last edited:

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
640
Reaction score
353
First Language
English
Primarily Uses
RMMV
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.
 

myenemy

Veteran
Veteran
Joined
Jan 17, 2014
Messages
67
Reaction score
30
First Language
Spanish
Primarily Uses
RMMV
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...!
 
Last edited:

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
640
Reaction score
353
First Language
English
Primarily Uses
RMMV
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.
 

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

Latest Threads

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,454
Members
137,821
Latest member
Capterson
Top