MV Transforming skills in battle based on external factor

Hello, I'm relatively unskilled in javascript and thus require some assistance!

Pretty much what I want to do is have skills "transform" in battle based on an external value, which I currently have as a gameVariable. Basically it's supposed to work like:
Battler uses Fire when the variable is a certain number > Another skill (for example; Earth) is cast instead.

At first I tried looking around for plugins similar to what I want to do in an effort to look at the code and try to incorporate it into my own plugin. I found Mr. Trivel's Different Skill Effects Depending on Target plugin and decided to copy some of the code for it. In the end, my code looks like this:

Spoiler: Code here
JavaScript:
_alias_gma_apply = Game_Action.prototype.apply;
Game_Action.prototype.apply = function(target){
    if (this.item().meta === 'undefined'){
    } else {
        this.fieldSkillChange(this.item());
    }
    _alias_gma_apply.call(this, target);
    if (this.isSkill() && target.result().isHit()){
        var gaf = this.item().damage.elementId;
        $gameVariables.setValue(1, gaf);
    }
    if(this._tempItem){
        this._item = this._tempItem;
        this._tempItem = null;
    }
}

Game_Action.prototype.fieldSkillChange = function(item){
    var chn = parseInt(item.meta.change);
    var chnto = parseInt(item.meta.change2);
    if ($gameVariables.value(1) === chn){
        this._tempItem = this._item;
        this.setSkill(chnto);
    }
}

And this actually works! For example, Fire DOES turn into Earth in the sense that it deals Earth damage and the variable containing the "Field Effect" changes into the value of Earth's elementId. The only issue is that the animation that plays is still the one for Fire. This is where the crux of my problem is.

How do I make it so the skill has the correct animation? Do I need to put the code somewhere else, like Scene_Battle or BattleManager? Quite frankly, I'm stumped. Any help is appreciated!
Sorry for double-posting, but I actually solved this issue. In case anyone wants to know how I did it, I'll put my "findings" here.

Spoiler: Removed code
JavaScript:
_alias_gma_apply = Game_Action.prototype.apply;
Game_Action.prototype.apply = function(target){
    /*if (this.item().meta === 'undefined'){
    } else {
        this.fieldSkillChange(this.item());
    }*/
    _alias_gma_apply.call(this, target);
    if (this.isSkill() && target.result().isHit()){
        var gaf = this.item().damage.elementId;
        $gameVariables.setValue(1, gaf);
    }
   /* if(this._tempItem){
        this._item = this._tempItem;
        this._tempItem = null;
    }*/
}

/*Game_Action.prototype.fieldSkillChange = function(item){
    var chn = parseInt(item.meta.change);
    var chnto = parseInt(item.meta.change2);
    if ($gameVariables.value(1) === chn){
        this._tempItem = this._item;
        this.setSkill(chnto);
    }
}*/

The commented code above is what I removed. Instead, I added this before the Game_Action apply alias.

Spoiler: Added code
JavaScript:
BattleManager.startAction = function() {
    var subject = this._subject;
    var action = subject.currentAction();
    if (action.item().meta === 'undefined'){
        } else {
            var chn = parseInt(action.item().meta.change);
            var chnto = parseInt(action.item().meta.change2);
            if ($gameVariables.value(1) === chn){subject.forceAction(chnto, -2); action = subject.currentAction();}
    }
    var targets = action.makeTargets();
    this._phase = 'action';
    this._action = action;
    this._targets = targets;
    subject.useItem(action.item());
    this._action.applyGlobal();
    this.refreshStatus();
    this._logWindow.startAction(subject, action, targets);
};

Now as I said; I'm not very skilled in javascript and I thusly have no idea if my implementation will horribly mess things up down the line. So I guess take it with a grain of salt? It is worth noting though that this achieves exactly what I wanted.
I haven't inspected your coding, because I'm not going to download someone's plugin, figure out how it works, then see what you changed 😉

But my answer to your original question is: I would implement this using Yanfly's Action Sequences. Since they allow you to program the functionality of the entire skill, it's easy to control and change what's happening.
I haven't inspected your coding, because I'm not going to download someone's plugin, figure out how it works, then see what you changed 😉

But my answer to your original question is: I would implement this using Yanfly's Action Sequences. Since they allow you to program the functionality of the entire skill, it's easy to control and change what's happening.
Oh, haha, I think there's a bit of a misunderstanding! After the changes I made, there are no usages of Mr. Trivel's plugin. This is all the code I'm using to get my desired effect.

Spoiler: Code
JavaScript:
BattleManager.startAction = function() {
    var subject = this._subject;
    var action = subject.currentAction();
    if (action.item().meta === 'undefined'){
        } else {
            var chn = parseInt(action.item().meta.change);
            var chnto = parseInt(action.item().meta.change2);
            if ($gameVariables.value(1) === chn){subject.forceAction(chnto, -2); action = subject.currentAction();}
    }
    var targets = action.makeTargets();
    this._phase = 'action';
    this._action = action;
    this._targets = targets;
    subject.useItem(action.item());
    this._action.applyGlobal();
    this.refreshStatus();
    this._logWindow.startAction(subject, action, targets);
};


//======================================================================================================

_alias_gma_apply = Game_Action.prototype.apply;
Game_Action.prototype.apply = function(target){
    _alias_gma_apply.call(this, target);
    if (this.isSkill() && target.result().isHit()){
        var gaf = this.item().damage.elementId;
        $gameVariables.setValue(1, gaf);
    }
}

That being said, thank you for answering! In case my method produces problems, I'll look into your suggestion. You can do just about anything with the help of Yanfly, eh? Hahaha
Just to offer the suggestion, it doesn't do anything wrong, but this is a very odd bit of coding:
Code:
    if (action.item().meta === 'undefined'){
        } else {
It doesn't really make any sense to have an empty if block. You could just do
if (action.item().meta!='undefined')

Also, does that all work? The reasons I ask:

- undefined is a term in JavaScript that checks whether a variable has not been set to a value. The way you're using it, in quotes, is checking for the character string "undefined". In order for that to ever be true, there would have to be code someplace that sets the property equal to the string "undefined," which I don't see - so your code will never not go into your else block that you wrote. That if check will always fail.

- The Game_Item class in MV doesn't have a meta property. The meta field, which is used to get data from notetags, belongs to the Skill class, kept in the $dataSkills array. So unless you're creating it someplace else in code you haven't put here, action.item().meta should never have any value in it.
@ATT_Turan: I agree with most of that, but the Game_Action#item method returns the corresponding $dataItems or $dataSkills object, not a Game_Item:
JavaScript:
Game_Action.prototype.item = function() {
    return this._item.object();
};
I.e. action.item().meta is the correct usage~ 👋

Also, all database objects with a note field (e.g. Items) get their meta property initialised to {}, so if the item exists then it should automatically be safe to check its meta properties.

@hohg: if you'd like the thread closed, you can report your post with a reason like "Solved".
the Game_Action#item method returns the corresponding $dataItems or $dataSkills object, not a Game_Item
I never noticed that - I assumed it was just an accessor for the ._item property, which is of the Game_Item type.
It doesn't really make any sense to have an empty if block. You could just do
if (action.item().meta!='undefined')

Also, does that all work? The reasons I ask:
You're right, the empty if block was pretty scuffed and I've changed it to what you suggested. As for 'undefined' being in quotations, the entire process works properly. I removed the quotations around it and everything still works the same as before.

so your code will never not go into your else block that you wrote.

This is super weird, because if one of the actors would use a skill which didn't have any notetags/"incorrect" notetags, this would throw an error upon using the skill, something like "cannot read meta property of undefined" or something along those lines. This doesn't happen though, strange. Oh well, I mean it all works like I want it to work, so I guess there isn't much to worry about.

@caethyril: Thank you, I didn't know that! I'll wait and see if Turan has anything he wishes to add and then I'll request for it to be closed.
Thread closed

Next time you solve your own question, please just edit your initial post (if nobody has posted after) to add the solution, to avoid double posting.

Original thread: https://forums.rpgmakerweb.com/threads/transforming-skills-in-battle-based-on-external-factor.142002/