MZ Looking for some help modifying TP gain in relation to quantity of targets

Greetings,

As you may or may not know, RPG Maker MZ by default multiplies any TP gain entered into the "TP Gain:" box by the number of targets hit by the item or skill.

I am looking to write a simple plugin that modifies the way TP gain works. What I would like to do is modify TP gained per use of a skill or item to always be exactly what it says in the "TP Gain:" box within the database, regardless of the number of targets hit.

I am wondering if anyone can point me in the right direction to find where or what I would need to do to make this change happen. I believe I am on the right track, but I may be grasping at straws a bit.

I think I would need to modify the following function in rmmz_objects.js. I have tried several iterations of modifying this but I'm a bit stumped, as no matter what I change it's always applying the changes for each target hit:

Game_Action.prototype.apply = function(target) {
const result = target.result();
this.subject().clearResult();
result.clear();
result.used = this.testApply(target);
result.missed = result.used && Math.random() >= this.itemHit(target);
result.evaded = !result.missed && Math.random() < this.itemEva(target);
result.physical = this.isPhysical();
result.drain = this.isDrain();
if (result.isHit()) {
if (this.item().damage.type > 0) {
result.critical = Math.random() < this.itemCri(target);
const value = this.makeDamageValue(target, result.critical);
this.executeDamage(target, value);
}
for (const effect of this.item().effects) {
this.applyItemEffect(target, effect);
}
this.applyItemUserEffect(target);
}
this.updateLastTarget(target);
};

Following is the function that calculates TP gain when a skill or item is used:

Game_Action.prototype.applyItemUserEffect = function(/*target*/) {
const value = Math.floor(this.item().tpGain * this.subject().tcr);
this.subject().gainSilentTp(value);
};


Any help with this would be greatly appreciated, I've scoured google for a few hours at this point but haven't been able to find any concrete leads on how exactly to make the change I'm looking for.

Note: Just for clarity's sake, I'm going to turn this into a plugin, I won't be modifying the core files directly.

Thank you in advance and have a wonderful week,

-Niniann
Any help with this would be greatly appreciated, I've scoured google for a few hours at this point but haven't been able to find any concrete leads on how exactly to make the change I'm looking for.
try removing the this.applyItemUserEffect(target) line from Game_Action.prototype.apply() entirely as apply() is ran per target, and then replicate it within something earlier on that gets ran per skill like BattleManager.startAction

Alternatively, flipping a switch or making a property somewhere within startAction(), using it to determine if applyItemUserEffect() gives the user TP at all, and then flipping the switch / deleting the property would also cause the effects to go off once a skill. The idea is the same though. hitch on to something that happens once per skill to make it once per skill
try removing the this.applyItemUserEffect(target) line from Game_Action.prototype.apply() entirely as apply() is ran per target, and then replicate it within something earlier on that gets ran per skill like BattleManager.startAction

Alternatively, flipping a switch or making a property somewhere within startAction(), using it to determine if applyItemUserEffect() gives the user TP at all, and then flipping the switch / deleting the property would also cause the effects to go off once a skill. The idea is the same though. hitch on to something that happens once per skill to make it once per skill
Robro33,

Bear with me, my JS skills are intermediate on the best of days and I'm still very much learning and not a programmer by trade.

So first off, thank you for the reply and idea! I just experimented with this a bit, unfortunately I get a crash with the error "applyItemUserEffect is not a function".

So I created a BattleManager.applyItemUserEffect function and it'll read that, but then crash with "this.item is not a function"...for which I then created a BattleManager.item function, but it crashes due to "object is undefined".

Correct me if I'm wrong and I very well could be, but it seems to me that your idea could work but it's going to require writing new versions of a hefty amount of functions, so I'd like to find a simpler solution if possible.

After learning the result of the BattleManager changes, I'd ideally like to target a function within rmmz_objects.js so that I can keep changes targeted to where they need to be. Though if that is possible, well, that remains to be seen.

However, I will try and follow your line of thinking. There must be something earlier I can target than Game_Action.prototype.apply to get the TP gain to fire at the start of the use of a skill or item itself.

Thank you again, it is very much appreciated 🙂
Bear with me, my JS skills are intermediate on the best of days and I'm still very much learning and not a programmer by trade.
No worries, we all start somewhere.

So I created a BattleManager.applyItemUserEffect function and it'll read that, but then crash with "this.item is not a function"...for which I then created a BattleManager.item function, but it crashes due to "object is undefined".
yeah, just copying the function call and placing it there wouldnt do anything as the BattleManager doesnt have it, and giving the BattleManager the same function without changes will fail as theres no this._subject.

Correct me if I'm wrong and I very well could be, but it seems to me that your idea could work but it's going to require writing new versions of a hefty amount of functions, so I'd like to find a simpler solution if possible.
what i had meant for you to do is replicate what the function does, rather than the function itself. making new functions can work, but isnt needed. Within startAction(), you already have variables to easily access the subject and the action, so easy options are to:

- use action.applyItemUserEffect(). even though theres a target parameter, it actually isnt used and can be ignored
Spoiler
Code:
BattleManager.startAction = function() {
    const subject = this._subject;
    const action = subject.currentAction();
    const targets = action.makeTargets();
    this._phase = "action";
    this._action = action;
    this._targets = targets;
    subject.cancelMotionRefresh();
    subject.useItem(action.item());
    action.applyItemUserEffect();   //added line
    this._action.applyGlobal();
    this._logWindow.startAction(subject, action, targets);
};

- or to use subject.gainTp(). You'd have to adapt the math in applyItemUserEffect to determine how much should be gained, but you have all the parts.
Spoiler
Code:
BattleManager.startAction = function() {
    const subject = this._subject;
    const action = subject.currentAction();
    const targets = action.makeTargets();
    this._phase = "action";
    this._action = action;
    this._targets = targets;
    subject.cancelMotionRefresh();
    subject.useItem(action.item());
    subject.gainTp(Math.floor(action.item().tpGain * subject.tcr));  //taken directly from applyItemUserEffect
    this._action.applyGlobal();
    this._logWindow.startAction(subject, action, targets);
};

both approaches work, and if you remove the call in apply like mentioned, the actions only give TP once. Both can be single line changes 😛
ive spoiler'd the changes if you still want to try yourself

After learning the result of the BattleManager changes, I'd ideally like to target a function within rmmz_objects.js so that I can keep changes targeted to where they need to be. Though if that is possible, well, that remains to be seen.
when youre modifying the game's code, its best practice to make your changes as plugins instead of directly modifying the file. there are a handful of reasons for it, but keeping track of the changes youve made is exactly one of them.

its not a fancy process. you just take the functions with your added changes, save them as a .js file, place it in your project's js/plugins folder, load it as a plugin, and save your project. your changes will be applied.
No worries, we all start somewhere.


yeah, just copying the function call and placing it there wouldnt do anything as the BattleManager doesnt have it, and giving the BattleManager the same function without changes will fail as theres no this._subject.


what i had meant for you to do is replicate what the function does, rather than the function itself. making new functions can work, but isnt needed. Within startAction(), you already have variables to easily access the subject and the action, so easy options are to:

- use action.applyItemUserEffect(). even though theres a target parameter, it actually isnt used and can be ignored
Spoiler
Code:
BattleManager.startAction = function() {
    const subject = this._subject;
    const action = subject.currentAction();
    const targets = action.makeTargets();
    this._phase = "action";
    this._action = action;
    this._targets = targets;
    subject.cancelMotionRefresh();
    subject.useItem(action.item());
    action.applyItemUserEffect();   //added line
    this._action.applyGlobal();
    this._logWindow.startAction(subject, action, targets);
};

- or to use subject.gainTp(). You'd have to adapt the math in applyItemUserEffect to determine how much should be gained, but you have all the parts.
Spoiler
Code:
BattleManager.startAction = function() {
    const subject = this._subject;
    const action = subject.currentAction();
    const targets = action.makeTargets();
    this._phase = "action";
    this._action = action;
    this._targets = targets;
    subject.cancelMotionRefresh();
    subject.useItem(action.item());
    subject.gainTp(Math.floor(action.item().tpGain * subject.tcr));  //taken directly from applyItemUserEffect
    this._action.applyGlobal();
    this._logWindow.startAction(subject, action, targets);
};

both approaches work, and if you remove the call in apply like mentioned, the actions only give TP once. Both can be single line changes 😛
ive spoiler'd the changes if you still want to try yourself


when youre modifying the game's code, its best practice to make your changes as plugins instead of directly modifying the file. there are a handful of reasons for it, but keeping track of the changes youve made is exactly one of them.

its not a fancy process. you just take the functions with your added changes, save them as a .js file, place it in your project's js/plugins folder, load it as a plugin, and save your project. your changes will be applied.
Again, thank you so much! I have been working on this some more this evening with your suggestions. I did try to write out a plugin without unveiling the spoilers but couldn't quite get it to work. Closest I got was start.action.applyItemUserEffect() within the BattleManager.startAction function(which doesn't work, haha) before I gave up and clicked the tag. Almost!

So I made this into a plugin and tested this change in a blank project with no plugins and it works exactly how I wanted it to! Whatever is in the "Gain TP:" box is the TP a skill or item user gets, full stop. On a vanilla project with no other plugins, this does the trick!

Next step is a different beast. Getting this to work with Visustella's Skill/States core. I am currently trying to alias the code I wrote but am getting a crash when I run the following code:

NinGainTPManager = BattleManager.startAction;
BattleManager.startAction = function() {
NinGainTPManager.call(this);
const Ninsubject = this._subject;
const Ninaction = Ninsubject.currentAction();
const Nintargets = Ninaction.makeTargets();
Ninaction.applyItemUserEffect();
};

The crash says "Cannot read properties of undefined (reading 'states')" and points me to Visustella's Skills/States core for the error.

I don't know if I'm doing something wrong in my alias or what, I followed a guide online on how to do it and have checked everything thrice, but it's just not playing nice with Visustella. Tried changing the plugin order too.

I fully realize that Visustella may not be your wheelhouse Robro33 so if you cannot assist further please do not sweat it, thank you again for the assistance you've already provided, it has been incredibly helpful!

I may end up having to try and reach out to Visustella on their discord for further assistance, as I'm stumped as to why my alias is causing "states" to be undefined.
I'm stumped as to why my alias is causing "states" to be undefined.
the error is a little different than youre thinking. its not that states is undefined, its that engine cannot read the states of undefined. meaning at some point, some function is looking for the states of a battler and it cannot find that battler. its difficult to say much more without seeing the error trace.

Playing around VS plugins is always tricky because of they make changes that are obfuscated. are you sure that your alias is working in general? also makeTargets() also does nothing by itself. it validates the targets for the skill, but it doesnt set the BattleManager's _targets by itself.

but since we're dealing with BS battle core, you will not be able to alter the contents of apply(). Which means you cant remove the applyItemUserEffect call within apply that is called on every hit of a skill if they still have it there.
you might be able to to look towards applyItemUserEffect instead. try typing
Code:
Game_Action.prototype.applyItemUserEffect
into the console to see what it is during gameplay. It may be unchanged. if it is, then you can use the second method i mentioned in my first post

Spoiler
Code:
Game_Action.prototype.applyItemUserEffect = function(target) {
    if (!this._tpGained){
        var value = Math.floor(this.item().tpGain * this.subject().tcr);
        this.subject().gainSilentTp(value);
        this._tpGained = true;
    }
};
the error is a little different than youre thinking. its not that states is undefined, its that engine cannot read the states of undefined. meaning at some point, some function is looking for the states of a battler and it cannot find that battler. its difficult to say much more without seeing the error trace.

Playing around VS plugins is always tricky because of they make changes that are obfuscated. are you sure that your alias is working in general? also makeTargets() also does nothing by itself. it validates the targets for the skill, but it doesnt set the BattleManager's _targets by itself.

but since we're dealing with BS battle core, you will not be able to alter the contents of apply(). Which means you cant remove the applyItemUserEffect call within apply that is called on every hit of a skill if they still have it there.
you might be able to to look towards applyItemUserEffect instead. try typing
Code:
Game_Action.prototype.applyItemUserEffect
into the console to see what it is during gameplay. It may be unchanged. if it is, then you can use the second method i mentioned in my first post

Spoiler
Code:
Game_Action.prototype.applyItemUserEffect = function(target) {
    if (!this._tpGained){
        var value = Math.floor(this.item().tpGain * this.subject().tcr);
        this.subject().gainSilentTp(value);
        this._tpGained = true;
    }
};
Robro33,

Thank you again for the replies! Digging into your suggestions now.

I typed Game_Action.prototype.applyItemUserEffect into the console and got the following:
Capture.PNG
Looks like Visustella is doing something with it, but it has been obfuscated.

However, I took the code you wrote in the spoiler tag, put it into the plugin for this, and commented out the rest of the stuff I had in the plugin including my attempt at the alias and it all works perfectly! There is no error or crash and each character is gaining only 10 TP on using an AoE spell instead of 10 for every target (10 being the number entered into the "TP Gain:" box.

I'm going to try turning this into an alias and see if everything goes smoothly, as it does appear that Visustella may have changed the function and is using it for something, likely in the battle core, and I don't want to burn whatever it is they're already cooking.

I will edit this post with the results shortly if I'm able to get it working!

Edit: Okay, I'm getting the same error as before. I'm starting to think it's something I'm doing wrong in the alias that is causing this, I'm going to look into that now. I know the code without being alias'd works in a brand new project with no other plugins, and also just straight up works without being alias'd in my actual project, so it must be the aliasing that is causing the issue?

Here is the error report:

Capture.PNG


This is what I wrote for the alias'd code:

NinGainTPPlugin = Game_Action.prototype.applyItemUserEffect
Game_Action.prototype.applyItemUserEffect = function(target) {
NinGainTPPlugin.call(this);
if (!this._tpGained){
var value = Math.floor(this.item().tpGain * this.subject().tcr);
this.subject().gainSilentTp(value);
this._tpGained = true;

}
};

Edit 2: Okay, it was indeed my alias that was wrote incorrectly. I needed NinGainTPPlugin.call(this, target, and possibly the const at the beginning). The following alias works just fine:

const NinGainTPPlugin = Game_Action.prototype.applyItemUserEffect
Game_Action.prototype.applyItemUserEffect = function(target) {
NinGainTPPlugin.call(this, target);
if (!this._tpGained){
var value = Math.floor(this.item().tpGain * this.subject().tcr);
this.subject().gainSilentTp(value);
this._tpGained = true;

}
};

However, because this is adding onto the existing function, it's not working as intended. Now characters get 10 TP for each target AND 10 additional TP, haha.

So I think I will have to do an actual overwrite in order to get this work as I want it too, and just see if anything else gets messed up through some playtesting.
I'm going to try turning this into an alias and see if everything goes smoothly, as it does appear that Visustella may have changed the function and is using it for something, likely in the battle core, and I don't want to burn whatever it is they're already cooking.
the problem with aliasing that function instead of overwriting is that you dont actually want the prior code to run in this case. when you make and call an alias, youre essentially adding extra code onto a pre-existing function. if you choose to use an alias here, the prior code will still run but it will run alongside your new code. So not only will each hit still generate TP, but the skill will also gain an extra hit's worth of TP on the first hit.

thats why my intention was to overwrite it, but i didnt expect VS to do anything to that function. its your call as to whether or not you want to overwrite it, but keep in mind that if you never utilize whatever part of the plugin uses it, you may not run into an issue over changing it.

that being said
Here is the error report:
its being used within the skills and states plugin. And as said before, your error is because a function is not getting a battler thats supposed to be supplied. the original function had a target parameter that i said was not used. Even though the base engine doesnt use it, it would seem like its not the case for the VS plugin. change
NinGainTPPlugin.call(this);
to
Code:
NinGainTPPlugin.call(this, target);
so that it is supplied with the target.
when youre writing aliases for functions, you generally want so set it up so that it still passes all the arguments it got called with into the base function.
the problem with aliasing that function instead of overwriting is that you dont actually want the prior code to run in this case. when you make and call an alias, youre essentially adding extra code onto a pre-existing function. if you choose to use an alias here, the prior code will still run but it will run alongside your new code. So not only will each hit still generate TP, but the skill will also gain an extra hit's worth of TP on the first hit.

thats why my intention was to overwrite it, but i didnt expect VS to do anything to that function. its your call as to whether or not you want to overwrite it, but keep in mind that if you never utilize whatever part of the plugin uses it, you may not run into an issue over changing it.

that being said

its being used within the skills and states plugin. And as said before, your error is because a function is not getting a battler thats supposed to be supplied. the original function had a target parameter that i said was not used. Even though the base engine doesnt use it, it would seem like its not the case for the VS plugin. change

to
Code:
NinGainTPPlugin.call(this, target);
so that it is supplied with the target.
when youre writing aliases for functions, you generally want so set it up so that it still passes all the arguments it got called with into the base function.
Robro33,

You are exactly right about the alias, got that fixed up. I am going to go with the overwrite for now as I don't notice any ill effects at a glance from doing some battle testing, and the TP gain is working perfectly with it.

We will see how things are working after more extensive testing, but for now I would consider the issue solved.

I learned a great deal from our discussion (and/or as a result of) including how to alias functions and how the inner cogs of the TP system whirl and click! Thank you again and I hope you have a wonderful week 🙂.

-Niniann
You are exactly right about the alias, got that fixed up.
ah
i see the edit now 🤣
its good that you got there in the end!

for now as I don't notice any ill effects at a glance from doing some battle testing
FWIW, in this case the odds are that it wouldnt cause a crash, but some feature provided by VS states and skills will cease to function. look out for skill notetags if you use any.
This thread is closed due to being reported as solved. If you need this reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/looking-for-some-help-modifying-tp-gain-in-relation-to-quantity-of-targets.175338/