MZ Syntax help for returning/checking party members class

I'm trying to set each party member's MP to 0, if they are a specific class. I put the below snippet in the Battle Event for one of my troops. If I remove the if statement logic is iterates through all party members and sets MP to 0.

I've tried assigning the current class to a variable but I can't seem to get it to return the class using:
$gameParty.members[n].currentClass() or
$gameParty.members[n].isClass(gameClass)

I'm pretty sure I'm setting up assigning it to a variable incorrectly. I'm also pretty sure I'm setting up the if statement incorrectly. Currently I'm using:
JavaScript:
var x = $gameParty.members().length;
for (i = 0; i < x; i++) {
  if $gameParty.members()[i].isClass(2) {
    $gameParty.members()[i].setMp(0);
  }
}

When I was trying to assign the current class to a variable I was using:
JavaScript:
$gameVariables.setValue(12, $gameParty.members()[i].currentClass);

Any help is appreciated.
actor.currentClass() is a function that returns the class obj
try
JavaScript:
$gameParty.members().forEach(function(actor){
  if(actor.currentClass().id == ID_OF_CLASS){
    actor.mp = 0
  }
}
I'm not entirely sure if you can set actor.mp to 0 the way done above, can't really test it right now, but if you can't you can always use actor.gainMp(n) where n is a negative integer
@Point08: you forgot to put brackets around your if condition, which probably means the script was running into an error and failing silently. Also note that isClass checks against a class object, e.g. $dataClasses[2], rather than a class ID. This would probably work:
JavaScript:
const x = $gameParty.members().length;
for (i = 0; i < x; i++) {
  const actor = $gameParty.members()[i];  // save yourself some typing
  if (actor.isClass($dataClasses[2])) {  // brackets & check class, not ID
    actor.setMp(0);
  }
}
I'd be more inclined to do something like Capitán suggests, though. 🙂

Storing current class in a variable should work mostly as you cited, except you need the brackets after currentClass to invoke it and get a result, otherwise you're storing the function itself. Also, you'd probably want to store only the class ID as opposed to the full class object, so something like this may be more appropriate:
JavaScript:
$gameVariables.setValue(12, $gameParty.members()[i].currentClass().id);


@Capitán: yep, mp is read-only, but setMp is the recommended way to set it to a different value~
@Capitán I'm getting an error when I try to use your code. I get a syntax error for missing ) after argument list. I thought it was maybe it was in the fist line after actor:
Code:
$gameParty.members().forEach(function(actor){
so I changed it to:
Code:
$gameParty.members().forEach(function(actor)){
and then I get an unexpected token ')' error.

Oh, I should mention I did change the actor.mp = 0 to actor.setMp(0)

Thanks for the detail about actor.currentClass() is a function that returns the class obj, that explains why I was getting an output value when displaying it, of object Object.

@caethyril The code you provided works. I was getting an unexpected token error at first on yours as well. I must have had a typo somewhere though, because I cleared it and pasted it in again and it worked. Thank you also for the correct syntax for storing a value in a variable. I was, in fact, storing the function itself.
Oh! Capitán had a typo in their example, it should work if you add a ) at the end, e.g.
JavaScript:
$gameParty.members().forEach(function(actor) {
  if (actor.currentClass().id == 2) actor.setMp(0);
});
Good to hear you got it working; happy RPG Making! 😂
Ooops! yes, I was typing on my phone hard to write good code on those things, glad you got it sorted out
Rather than start a new thread, I'm going to post this here as it is closely related.

I've gotten the above working, and added a piece that checks the Region ID and adds additional MP based on that, and then adds MP each turn based on a Region ID calculation.

I've included a similar calculation in a magic skill damage formula. All spells will have include this, so I'd like to get it out of the damage formula and into a common event. However I can't seem to get the party position based on actor ID, and I can't get gainMp to work using actor.

The damage formula is:
Code:
dmg = damage_formula; a.gainMp(Math.round(($dataSkills[99].mpCost * 0.4) + ($gameMap.regionId($gamePlayer.x, $gamePlayer.y) * 0.3))); dmg;

I was using $gameVariables.setValue(14, a.actorId()) in the damage formula, and then trying to use that in a common event. I tried things like $gameActors.actor($gameVariables[14]) and let x = $gameParty.members().actor($gameVariables[14]).

I tried a few other things I can't remember. Basically I can't figure out how to pass into the common event, the party member or actor who used the skill, so I manipulate their MP.
@Point08 where exactly are you using the common event?
I was using it in the skill, where you can set other effects or call an event. I couldn't get it to work so I put the code into the damage formula as above. It works, but would be a pain to make changes to across multiple skills.
I tried a few other things I can't remember. Basically I can't figure out how to pass into the common event, the party member or actor who used the skill, so I manipulate their MP.
To reference the value of game variable 14 in script, use: $gameVariables.value(14). 🙂

That said, RMMZ has a new feature that's very useful for this sort of thing! Check out Control Variables > Set > Game Data > Last: it offers "Last Actor to Act" and "Last Used Skill ID". You could put those together in a common event and just have your damage formula do the damage, e.g.
Spoiler: Event example
Code:
◆Comment:Get actor who last acted
◆Control Variables:#0010 actor ID = Last Actor ID to Act
◆Comment:Get base MP-restore value as 0.3 * regionId
◆Get Location Info:restore value, Region ID, Player
◆Control Variables:#0011 restore value *= 3
◆Control Variables:#0011 restore value /= 10
◆Comment:Add value from skill cost
◆Control Variables:#0012 skill ID = Last Used Skill ID
◆If:skill ID = 20
  ◆Control Variables:#0011 restore value += 50
  ◆
:Else
  ◆If:skill ID = 21
    ◆Control Variables:#0011 restore value += 20
    ◆
  :Else
    ◆Comment:etc
    ◆
  :End
  ◆
:End
◆Comment:Gain MP!
◆Change MP:{actor ID}, + {restore value}
Alternatively for the "skill MP cost" part you could try Control Variables > Add > Script:
JavaScript:
$dataSkills[$gameTemp.lastActionData(0)].mpCost
You can include a * 0.4 or something in there if you like, too. 😮‍💨
First, thank you so much for the help!
It works...ish.

My problem now is pulling in the region isn't working. If I use ◆Get Location Info:RegionID, Region ID, Player I get an error.

I solved it by setting the Region ID to Variable 12 in a parallel process event on the map. Is this because once battle starts I'm not on the map? I also tried setting it in the Troop Event that runs on Turn 0, but that also errors out.

Here's the event:
Code:
◆Control Variables:#0014 actorUsedSkill = Last Actor ID to Act
◆Control Variables:#0013 skillUsedId = Last Used Skill ID
◆Control Variables:#0015 skillUsedMpCost = $dataSkills[$gameVariables.value(13)].mpCost
◆Control Variables:#0016 gainMp = Math.ceil(($gameVariables.value(15) * 0.85) + $gameVariables.value(12) + 1)
◆Text:None, None, Window, Bottom
:    :Gain: \V[16]  Cost: \V[15]
:    :Actor: \V[14]  Skill: \V[13]  Region: \V[12]
◆Change MP:{actorUsedSkill}, + {gainMp}
Oh wow. 😅 OK, I wasn't previously aware, but it looks like any event command referencing map characters intentionally fails in battle (supporting code excerpt below). I'm guessing there's a reason for it...
Spoiler: Game_Interpreter > character (rmmz_objects.js v1.1.0)
JavaScript:
Game_Interpreter.prototype.character = function(param) {
    if ($gameParty.inBattle()) {
        return null;
    } else if (param < 0) {
        return $gamePlayer;
    } else if (this.isOnCurrentMap()) {
        return $gameMap.event(param > 0 ? param : this._eventId);
    } else {
        return null;
    }
};
Also, it will obviously fail during battle test because the map doesn't get loaded in that case. 😴

You don't actually have to use a variable, though: you can get the player's current region ID with this script:
JavaScript:
$gameMap.regionId($gamePlayer.x, $gamePlayer.y)
So you could replace $gameVariables.value(12) with that and save yourself the parallel event. 😤

Original thread: https://forums.rpgmakerweb.com/threads/syntax-help-for-returning-checking-party-members-class.129380/