MV How to add "stats" to the Actor?

I want to add stats like the lockpick skill to my actor via script. Here is what I have already:

Spoiler: Code
JavaScript:
/*:
@param lockpick
@type number

*/

var params = PluginManager.parameters("Custom_Skill");

var lockPickSkill = params.lockpick;

Window_Status.prototype.drawParameters = function(x, y) {
drawParams.call(this, x, y);
var lineHeight = this.lineHeight();
for (var i = 6; i < 7; i++) {
        var paramId = i + 2;
        var y2 = y + lineHeight * i;
        this.changeTextColor(this.systemColor());
        this.drawText("Lockpick", x, y2, 160);
        this.resetTextColor();
        this.drawText(lockPickSkill, x + 160, y2, 60, 'right');}
};


Window_Status.prototype.refresh = function() {
    this.contents.clear();
    if (this._actor) {
        var lineHeight = this.lineHeight();
        this.drawBlock1(lineHeight * 0);
        this.drawHorzLine(lineHeight * 1);
        this.drawBlock2(lineHeight * 2);
        this.drawHorzLine(lineHeight * 6);
        this.drawBlock3(lineHeight * 7);
        this.drawHorzLine(lineHeight * 15);
        this.drawBlock4(lineHeight * 14);
    }
};

Here, you input the number for lockpick and this code adds the text and the value to the Status Window and redraws the line below it. I have even called it via script in the conditional where if you have a high enough lockpick value you can open the chest and it says your success, and tells you you have failed if not.

I want it to be an official "stat" like a skill in the menu.

Oh! Maybe I can add it to the InitMembers function here: Game_Actor.prototype.initMembers ? Oh, and then how would you increase this skill? How about each level it increases? Or you can give out stat points?

Please let me know guys!!
First of all,
JavaScript:
Window_Status.prototype.drawParameters = function(x, y) {
drawParams.call(this, x, y);
// rest omitted
};
will crash your game. You're trying to call drawParams(), but you never defined it. You probably forgot to add:
JavaScript:
var drawParams = Window_Status.prototype.drawParameters;
which you should do BEFORE you overwrite it with your own code, otherwise you'll get caught in an endless loop when drawParams() calls your function, which calls drawParams(), which calls your function, which calls drawParams(), which...

Next,
Code:
/*:
@param lockpick
@type number

*/
probably won't do what you expect it to do. This declares a plugin-wide parameter, used to fine-tune the plugin (e.g. if you want to share the plugin with other people who might need different values).

It doesn't really make sense to put your characters' lockpick skill in there. Instead, you should put in things like:
Code:
/*:
@param minLockpickSkill
@type number
@default 0

@param maxLockpickSkill
@type number
@default 100
*/
to ensure your characters' lockpick skill can't go beyond 0-100 (for example) when affected by a buff or debuff.

Since you want to put the lockpick skill on your actors (or maybe a class instead?), you might want to use note tags instead, where you can specify the starting lockpick skill for each actor, as well as other parameters, like the maximum lockpick skill (for instance, a character with maximum lockpick skill of 0 can't pick locks at all).

In the lower right corner of the RPG Maker database, there's a "Note" field. You can add notetags like this:
Code:
<initialLockpickSkill:10>
<maxLockpickSkill:100>
to define a character (or class) that starts with a lockpick skill of 10, and can go up to 100.

Here's an example of how you'd use those notetags (assuming lockpicking is an actor skill, not a class skill):
JavaScript:
var Game_Actor_initSkills = Game_Actor.prototype.initSkills;
Game_Actor.prototype.initSkills = function() {
    Game_Actor_initSkills.call(this);
    this.lockpickSkill = Number($dataActors[this._actorId].meta.initialLockpickSkill);
}
which will automatically initialize your actor with the lockpicking skill defined in the notetag.

Inside Window_Status you can access the skill with this._actor.lockpickSkill.

To access the lockpick skill from within an event:
Code:
Control Variables : #0001 = $gameActors[1].lockpickSkill
will save the lockpick value of actor #1 in variable #0001.

How to increase your lockpick skill depends on how you want it to be increased. If lockpicking is tied to your actor's level:
JavaScript:
var Game_Actor_levelUp = Game_Actor.prototype.levelUp;
Game_Actor.prototype.levelUp = function() {
    Game_Actor_levelUp.call(this);
    this.lockpickSkill += 1;
}
which will increase lockpicking by 1 every time the actor gains a level. You could use notetags to define a different growth, if you want (that way, a thief could have a growth of 5 per level, while a knight would have a growth of 0). This is also the best place to check for maxLockpickSkill if you chose to implement it.

If lockpicking is tied to a different mechanic (e.g. how many times the actor has picked locks), you might want to use a plugin command instead, and call that plugin command from the event that handles picking locks.
How to implement plugin commands depends on whether you use RPG Maker MV or MZ.
@Nolonar thanks sir/ma'am! You gave me a lot. Actually the first problem shouldn't be there. It is here in my script. IDK why it is not there on my post. Maybe a C&P problem. But really good work. How do I learn more about how to script using Notetags? I know what they are, I just don't know how to script with them. You gave me a number version of it. Please give me more, like a guide. Thanks again!
Notetags can be of one of two forms:
  1. <name>
  2. <name:value>
You can't have a closing pointy bracket > in either name or value, and you can't have a colon : in name, though you can have as many colons as you want in value.

There are many places where you can put notetags, such as:
  • Actors ($dataActors)
  • Classes ($dataClasses)
  • Skills ($dataSkills)
  • Items ($dataItems)
  • Weapons ($dataWeapons)
  • Armors ($dataArmors)
  • Enemies ($dataEnemies)
  • States ($dataStates)
  • Tilesets ($dataTilesets)
  • Maps ($dataMap)
  • Events ($dataMap.events)
Except for $dataMap, they're all arrays, so you need to access their notetag like this: $dataActors[1].meta
For the current map, you'd use $dataMap.meta instead.

The first form is used when you don't need a value in your notetag. You can easily check for the existence of a notetag using:
JavaScript:
if ("name" in $dataMap.meta) {
    // do something
}
or
JavaScript:
if ($dataMap.meta.name) {
    // do something
}
Of course, "name" must match whatever you put in for a name in the notetag. If the notetag is <disallowLockpicking>, then you need to do if ("disallowLockpicking" in $dataMap.meta) or if ($dataMap.meta.disallowLockpicking) instead.

By the way, the above code also works for notetags with values, though in this case I'd recommend using the solution with the in keyword (I'll explain why in a bit).

You can put anything as a value, as long as it doesn't contain a closing pointy bracket >. The value will be a string, so if you expect a number, you should wrap the meta in Number(), for example: Number($dataActors[1].meta.initialLockpickSkill). This avoids bugs that are extremely hard to track, such as:
JavaScript:
var A = 1;
var B = $dataActors[1].meta.initialLockpickSkill; // "10"
var C = A + B; // "110"
You expected 11, but got "110" instead. That's because notetag values are strings (text), and when you add a number to a string, the number will be converted to a string and both will be combined like how you'd combine two words together to form a single word. This is known as "string concatenation".

If instead of a number, you expect a boolean (which is either true or false), you can't simply do Boolean($dataMap.isRaining), because unlike Number(), which parses a string to extract a number, Boolean() does not parse strings.
The following result in false:
  • Boolean()
  • Boolean(0)
  • Boolean(-0)
  • Boolean(0.0) and other variations of 0
  • Boolean("")
  • Boolean(undefined)
  • Boolean(null)
  • Boolean(NaN)
  • Boolean(false)
Everything else will return true instead. This includes Boolean("true") and Boolean("false").

So if you expect a boolean, you should do $dataMap.isRaining === "true" or $dataMap.isRaining !== "false" instead. Which one you choose depends on whether you want true or false to be the default value (in case the notetag is missing or contains an unexpected value).

Finally, a notetag without a value (e.g. <isRaining>) will have the value true. This is a boolean, not a string.

A notetag with a missing value (e.g. <isRaining:>) will have the value "", which will be evaluated as false. That's the reason why I recommended using if ("disallowLockpicking" in $dataMap.meta) instead of if ($dataMap.meta.disallowLockpicking) to check for the existence of a notetag.
Granted, it's a very unlikely case, but if it happens, you'll be happy not to have to waste hours trying to figure out why your plugin doesn't behave the way you expect.

Finally, you can go as far as to pack entire objects (in JSON) in a notetag's value, such as: <object:{name: "Bob", strength: 10}>

That's where notetags start getting really complex. In order to retrieve the object from the notetag, you'll need to do this:
JavaScript:
var obj = JSON.parse($dataMap.meta.object);

So far, it's still relatively simple. The nightmare starts when you begin chaining this stuff, like: <objects:[{name: "Bob", skill: {name: "lockpicking", value: "over 9000"}}]>

In this case, you have an object (skill) within an object (something with name: "Bob") within an object (Array). Which means you'll need to use JSON.parse() multiple times:
JavaScript:
var objects = JSON.parse($dataMap.meta.objects);
for (var i = 0; i < objects.length; i++) {
    objects[i] = JSON.parse(objects[i]);
    objects[i].skill = JSON.parse(objects[i].skill);
}
or alternatively:
JavaScript:
var objects = JSON.parse($dataMap.meta.objects).map(function(o) {
    var result = JSON.parse(o);
    result.skill = JSON.parse(result.skill);
    return result;
});

This should be more or less everything you need to know about notetags, unless I've forgotten something.
@Nolonar you did so much work here, thanks so much!! I will study this.

But just one more thing. What about the tags with the name followed by a certain body and ending with a closed tag? Like this:

<tag>
Statement 1: 15
Statement 2: hello world (etc.)
</tag>

Can you tell me how to do this?
To access the lockpick skill from within an event:
Code:
Control Variables : #0001 = $gameActors[1].lockpickSkill
will save the lockpick value of actor #1 in variable #0001.
You shouldn't do this. It's possible that $gameActors[1] hasn't yet been initialized and is null, in which case this code will crash. Instead, you should write $gameActors.actor(1), which does the same thing but also makes sure the actor has been initialized first, so it will never be null. (You do have to be careful not to do this in any code that will run when the actor is initialized, but in an event you don't need to worry about this.)
So if you expect a boolean, you should do $dataMap.isRaining === "true" or $dataMap.isRaining !== "false" instead. Which one you choose depends on whether you want true or false to be the default value (in case the notetag is missing or contains an unexpected value).
My preferred way of converting a boolean value is JSON.parse($dataMap.meta.isRaining. It'll crash if it's not a valid boolean, though… but you just need to make sure it is a valid boolean, right? It'll also convert other types of values, such as numbers or strings or null, or the complex values mentioned later in Nolonar's post. For example, if you wrote <isRaining:45> in the note box, it would return the number 45, and if you wrote <isRaining:"tomorrow">, it would return the string "tomorrow" (without the quotes). But you're the one writing the note tag, so if this is undesirable, then just don't write that kind of thing in the note tag. (This is assuming you don't plan to distribute your code as a plugin, of course.)
@Nolonar you did so much work here, thanks so much!! I will study this.

But just one more thing. What about the tags with the name followed by a certain body and ending with a closed tag? Like this:

<tag>
Statement 1: 15
Statement 2: hello world (etc.)
</tag>

Can you tell me how to do this?
The engine doesn't have any built-in way to parse these. I recommend using the in syntax to detect whether a tag even exists (ie, if('tag' in $dataSkills[4].meta)), then parsing it yourself from the raw note (ie, $dataSkills[4].note; for example, using regular expressions).
You shouldn't do this. It's possible that $gameActors[1] hasn't yet been initialized and is null, in which case this code will crash.

It's possible $gameActors[1] hasn't yet been initialized, even though the game is already processing an event?

Just asking because I'm feeling way too lazy to go through the entire codebase to check myself.
Hey if those notes in the database things are arrays (like class[0]), then how do you check all of them at once? How about a loop?
It's possible $gameActors[1] hasn't yet been initialized, even though the game is already processing an event?

Just asking because I'm feeling way too lazy to go through the entire codebase to check myself.
$gameActors[1] wouldn't work whether the game is initialized or not, because the object is not itself an array and can't be accessed like one. You could do $gameActors._data[1], but $gameActors.actor() exists as a wrapper for that so that you don't need to refer to its internal variable (encapsulation and all that).

One other thing I did notice that I feel I should point out is that in your explanation of how to use meta, if you're running that code on a battler that doesn't have a lockpick notetag, it will return NaN because you're not checking that the property exists before trying to convert it to a number.

As for meta as an array, you could do

JSON.parse("[" + meta.whatever + "]")
It's possible $gameActors[1] hasn't yet been initialized, even though the game is already processing an event?

Just asking because I'm feeling way too lazy to go through the entire codebase to check myself.
Yes, even if the game is already processing an event, it's possible the actor has not been initialized. Typically, an actor will be initialized only once it joins your party, although if it's needed for something else (for example, a summoning plugin) it'll be initialized then.

Hey if those notes in the database things are arrays (like class[0]), then how do you check all of them at once? How about a loop?
Yes, a loop would be the normal way. There are also functions like some and every that could be useful, depending on what exactly you want.
$gameActors[1] wouldn't work whether the game is initialized or not, because the object is not itself an array and can't be accessed like one. You could do $gameActors._data[1], but $gameActors.actor() exists as a wrapper for that so that you don't need to refer to its internal variable (encapsulation and all that).
I checked, and you are absolutely correct.

I shouldn't have tried to write a technical answer at 2 AM. My bad.
Why did this thread of mine turn into a full discussion? Did anyone else benefit by learning more here besides me? I am grateful for this thread and everyone who has contributed to this topic/discussion.
Why did this thread of mine turn into a full discussion? Did anyone else benefit by learning more here besides me? I am grateful for this thread and everyone who has contributed to this topic/discussion.
I'm not quite sure what you mean. The only responses besides those directly answering your questions are posts correcting answers to your questions that weren't quite right. 😛

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-stats-to-the-actor.136990/