[RMMV] $gameActors.actor($gameVariables.value(1)).changeClass(1); doesn't work :(

Hi! I need a single script to change X actor (determined by a variable) to class 1; it's part of a phase in the game where you can add party members and decide (among other things) their class. I could make endless if/else branches (well, one for each actor) without even using script, but it could require a giant amount of time.
Now this script doesn't work
Code:
$gameActors.actor($gameVariables.value(1)).changeClass(1);
Nor this one.
Code:
var a = $gameVariables.value(1);
$gameActors.actor(a).changeClass(1);
Is there any way I can do this with a single script? Thank you!
$gameActors only refers to the database, not the actual actor in the party. Think of it as a prototype actor from which an actor is created. To do what you want, you need to change the class of the actual actor in the party. So instead of $gameActors[actor_id], use

Code:
$gameParty.members()[party_id]

But be careful, the party_id refers to the order the actor is in the party, not the actor_id of the hero. Anyways, I think you get the idea.
This is terrible news. Thank you anyway. It's weird that I get a preset command to change an actor's class in the event menu, but I can't just script the same thing.
If party_id is the only way to go fast (without creating dozens of if/elses), I will have to be extra careful that things don't get messed up.

Edit

Waaait

Are you saying that there's no way to have $gameActors.actor(id) to be determined by a variable?! This is a disaster for me!
$gameActors does not only reffer to the database. $dataActors is the database only variable. $gameActors is the instanced actor data specific to each game. It could be that your variable is storing a string, not a number.

For example:
Code:
$gameActors.actor(1).changeClass(1);
is 100% valid code that WILL change your actors class id. ( i legit just double checked to be sure)

you could try ensuring that your variable is returning a valid id before switching, perhaps something like this:
Code:
var aid = parseInt($gameVariables.value(1)) || 1; 
var cid = parseInt($gameVariables.value(2)) || 1; 
$gameActors.actor(aid).changeClass(cid);

also just checked that, which works.
^

Basically what I wanted to say but with more details.
What's in the variable? The actor id (as seen in the Actors tab in the database)? Or their position in the party?

The event menu gives you a way to change an actor's class, but you have to provide the actor id (and in that case it's a reference to what you see in the actors tab). And yes, there is a way to do that via script. But you need to clarify WHO it is that you're trying to change.

If you say change Actor 1's class, and actor 1 isn't even in the party, then of course nothing is going to happen.
If you want to change the first party member's class (the leader) then it would be $gameParty.leader().changeClass(1)
We need more information on what that variable refers to and how you're setting it to be able to tell you why your command isn't working.

Also, if you change the class and then add them to the party with the Initialise option, any previous changes you've done are going to be ignored and they'll get whatever they have in the database. So if that's the problem, add them to the party first, and then do the change class command.
The variable has to refer to actor id, not party order. The game calculates it as "how many party members have you ever added to the party in this game". There's a common event in the game that adds +1 to the variable, adds actor id = variable to the party, and then gets you a choice to which class you want that actor to be (among other things). Nothing else will ever change that variable.

As for now, I'm still running a draft event on two actors (ids 1 and 2) which are in the party.

I thought it could be because I have too many plugins (frogboy race core, class change core, etc) but if I write gameActors.actor(1).changeClass(1); it works flawlessly.

I'll try @MushroomCake28' s code as soon as I get my hands on my pc, thank you!
This is weird. On my computer
$gameActors.actor($gameVariables.value(1)).changeClass(1) works with no issues. The code should work. So apparently the variable $gameVariables.value(1) returns weird values. For example, when you said that
There's a common event in the game that adds +1 to the variable, adds actor id = variable to the party, and then gets you a choice to which class you want that actor to be (among other things). Nothing else will ever change that variable.
Is there something that reduces the variable value back to 0? If not, then the variable would grow infinitely in value.

You can see switch and variable values if you press f9 in game. This turns on debug menu.
or just add a Text command with \V[1] immediately before the script call so you can confirm exactly what is in that variable at the moment of the call.

You're not using a self variables plugin, are you, and have variable 1 set up to be a self variable?
No self variables. It's working now! I have this horrible feeling that I somehow just either forgot to set the variable to 0 before running the event that gives it +1, or mistyped the script. Thank you guys, you have been precious!

edit: I probably found what's wrong. I actually wrote keepExp inside brackets... And I get the idea that's something you should not do.
You don't have to set it to 0. If it is not set, the first time it's accessed, it defaults to 0.

Variable 1.

You haven't added any plugins have you, and forgot to configure them? A lot of plugins that use variables (Yami's overlay mapping, Yanfly's message plugin) default to variable 1, and if you don't change the plugin, and don't name the variable to indicate that it's used by a plugin, then you will be using the same variable for multiple things.


Writing the text keepExp I'd expect to cause the game to crash, or at least an error message in the console, which is a good place to check when things don't work as you expect. But yes, it might prevent the changeClass command from working. In all of those places where you said "I used this script call and it didn't work", you probably should have posted the script call that you really used. If you had said
Code:
$gameActors.actor($gameVariables.value(1)).changeClass(1, keepExp);
the very first person who answered you would have told you that.
Yeah, I think I copied another instance of the script in the same event but from a different branch. I'm sorry, don't be mad :LZScheeze: you really helped me out here guys, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/rmmv-gameactors-actor-gamevariables-value-1-changeclass-1-doesnt-work.108780/