How locate actor with a certain class in party?

Using an event, how can I get the actor Id of an actor with a particular class in my party?


For example, I would like to locate the Mage in my party and match her magic skill to counter a spell or something?


How would I do it for all actors?


Thanks!
Wait, is only one character the mage? If so, just check them.


Or can they change classes and you want to check if there is a mage and, if so, which is the strongest?
Assume that classes are randomized and player can change the party whenever, so at the worst all 4 members could be mages. Which is a good point, in this case, how do I locate the actor id of one with the say, highest mat?
Well, there's a lot of ways to do that. If you have a predetermined and low number of actors in your party you can just use conditionnals.


You'll have to update the event each time you change the number of possible actors though, and it can get quite complicated, especially if they can change classes.


What the editor lacks for a fully evented automated way is:

  • A way to get an actor class Id with only the actor's own id  (script call: $gameActors.actor(actorId)._classId)
  • A way to get an actor parameter with only the actor's own id (script call: $gameActors.actor(actorId).atk for example)

You can do an automated evented way using a loop and the script calls above, probably.


Or, you can go full javascript. 


I know you said an evented way, but it piqued my interest so I wrote a javascript solution, in spoilers below, you're free to use it if you want.

Spoiler
var ar = []; var car = [];var maxIndex = 0; var mageClassId = 1;//set mageClassId to your liking
var vid1 = 1; var vid2 = 2; //variable vid1 will contain the actor id, variable vid2 will contain the parameter value
for (var i = 0; i < $gameParty.members().length; i++) {
if ($gameParty.members()._classId === mageClassId) {
ar.push($gameParty.members()._actorId)
car.push($gameParty.members().mat) //change the parameter here to the one you want
}
}
var maxValue = car[0]
for (var i = 1; i < car.length; i++) {
if (car > maxValue) {maxIndex = i; maxValue = car}
};
$gameVariables.setValue(vid1, ar[maxIndex]); $gameVariables.setValue(vid2,maxValue)


What this does is:

  • Run a loop through all party members and add them to an array if their class Id is equal to mageClassId, 
  • Add the specified parameter to another array.
  • Check the second array and get the highest number index and value
  • Get the actor Id of the highest parameter by cross matching between the two.
  • Assign the actor Id of the actor with the highest parameter to variable vid1, and the value to variable vid2;

Change vid1 from 1 to the number of the game variable you want to use, same for vid2.

...so I wrote a javascript solution, in spoilers below, you're free to use it if you want...



Snitched, for my ever-growing collection of snippets. Good stuff; thanks.

Original thread: https://forums.rpgmakerweb.com/threads/how-locate-actor-with-a-certain-class-in-party.70359/