Trouble with resetting attack

Hey all,

Around two months ago, I made some abilities with the help of some lovely people from the forum. Having changed a bunch of systems since then, I was trying to iterate on one of those abilities and hit a bunch of errors I can't seem to solve.

Basically how these abilities work is that the class has opener skills with MP cost which turn on switches(6, 7) and each switch is required for a different finisher skill which then turns off those switches so the finisher can only be used once per turn. The change I'm trying to implement is to have one of these finishers "reset its cooldown" when it kills an enemy.

The first method I've tried is this:

JavaScript:
<Instant Cast>

<Custom Requirement>
if ($gameSwitches.value(7)) {
value = true;
} else {
value = false;
}
</Custom Requirement>


 <Custom Execution>
$gameSwitches.setValue(6, false);
if (target.isDead()) {
$gameSwitches.setValue(7, true);
} else {
$gameSwitches.setValue(7, false);
}
 </Custom Execution>

This code gives me the following error in the console: target is not defined.

My guess was that by the time the code runs, the target is already dead and removed from the troop list so the code has no target. So, thinking that may be the problem, I've tried the following code so that the target is saved in a variable before it is dead:

JavaScript:
 <Instant Cast>

<Custom Requirement>
if ($gameSwitches.value(7)) {
value = true;
} else {
value = false;
}
</Custom Requirement>

<Before Eval>
$gameVariables.setValue(21, target);
</Before Eval>

 <Custom Execution>
$gameSwitches.setValue(6, false);
if ($gameVariables.value(21).isDead()) {
$gameSwitches.setValue(7, true);
} else {
$gameSwitches.setValue(7, false);
}
 </Custom Execution>

But this code gives the following error: $gameVariables.value(21).isDead() is not a function.

I'm really not sure what this error means. I'm probably doing something wrong, but what?


Additional Question: I didn't want to make a separate thread for this question so I'll tack it on at the end: Is there a plugin like BaseTroopEvent that runs at the end of every combat? If not, I'll probably do this by making a common event and adding it at the end of each battle but a way to do that automatically would be helpful.
This code gives me the following error in the console: target is not defined.

My guess was that by the time the code runs, the target is already dead and removed from the troop list
not quite. within a custom execution notetag there is no "target". you only get user. within the order of the skill tags, execution is one of the first timings and happens before the skill is performed. if you want to do something that also has the ability to check if it killed the target, use <After Eval> or <Post-Damage Eval> and check if the target's hp == 0.

also dead enemies visibly disappear if theyre static battlers, but theyre not removed from the troop on death. isDead() would be kind of pointless for them to have if you could never access the enemy while they were dead 😛

But this code gives the following error: $gameVariables.value(21).isDead() is not a function.

I'm really not sure what this error means. I'm probably doing something wrong, but what?
same reason as before. execution runs before before eval. the variable hasnt been written to yet at that point.

Is there a plugin like BaseTroopEvent that runs at the end of every combat?
you can try this if you have it. otherwise i think HIME has a plugin that can do it
@Robro33

Thank you for the reply!

I didn't know Custom Execution didn't take a target. Just before reading your message, I got this ability to finally work by using:

JavaScript:
<Instant Cast>

<Custom Requirement>
if ($gameSwitches.value(7)) {
value = true;
} else {
value = false;
}
</Custom Requirement>

<Before Eval>
$gameVariables.setValue(21, target.hp);
</Before Eval>

<Post-Damage Eval>
$gameSwitches.setValue(6, false);
if (this.isDamage() && value >= $gameVariables.value(21)) {
$gameSwitches.setValue(7, true);
} else {
$gameSwitches.setValue(7, false);
}
</Post-Damage Eval>

After reading your post, I tried my previous idea of using .isDead() with the correct notetag but the following code doesn't work.

Code:
 <Instant Cast>

<Custom Requirement>
if ($gameSwitches.value(7)) {
value = true;
} else {
value = false;
}
</Custom Requirement>


   <Post-Damage Eval>
$gameSwitches.setValue(6, false);
if (target.isDead()) {
$gameSwitches.setValue(7, true);
} else {
$gameSwitches.setValue(7, false);
}
 </Post-Damage Eval>

Why would this be the case? Am I misunderstanding how isDead() works fundamentally?
its a timing issue more or less.

isDead() checks if the battler has their death state active on them. the issue is this - the time that the death state is added to a target that died to the skill is actually after all of those skill core tags run. unless the target was already dead to begin with or you applied death with a skill core tag, you wont get it to return true. its why i suggested checking if their hp == 0 instead of using isDead()
its a timing issue more or less.

isDead() checks if the battler has their death state active on them. the issue is this - the time that the death state is added to a target that died to the skill is actually after all of those skill core tags run. unless the target was already dead to begin with or you applied death with a skill core tag, you wont get it to return true. its why i suggested checking if their hp == 0 instead of using isDead()

Ah, I see. Thanks for explaining it! So, isDead() is not the correct method to use here. Both my third method which checks if the damage exceeds their current hp and your suggested solution of checking if their hp is 0 work perfectly, and seeing how your method is simpler and cleaner, I will go with that. Thanks for the help!
I didn't know Custom Execution didn't take a target.
If you check out where it is in the plugin documentation, it's with the requirements and costs - the purpose of Custom Execution is to pay costs that can't be set in the skill entry in the database.

Thus, the provided example of showing the custom cost display of 1,000 gold, the display eval that shows the skill as usable only if the party has at least 1,000 gold, then the custom execution removes that 1,000 gold to pay the cost.

As a side tip, you can trim down a lot of your code. Two bits of syntax/logic you could apply are:
1 - If you're only executing a single statement after a conditional, you don't need to use braces to create a code block
Code:
if (this.isDamage() && value >= $gameVariables.value(21))
    $gameSwitches.setValue(7, true);
else
    $gameSwitches.setValue(7, false);
2 - If the only thing you're doing from a conditional is setting a boolean, you can skip it entirely and just set the boolean equal to the condition itself
Code:
$gameSwitches.setValue(7, this.isDamage() && value >= $gameVariables.value(21));

My final tip is: proper style is to indent code that's inside of a code block. That makes it easier to visually distinguish which code is not automatically happening. You can see I did it above, and to use your original, longer code:
Code:
if (this.isDamage() && value >= $gameVariables.value(21)) {
    $gameSwitches.setValue(7, true);
} else {
    $gameSwitches.setValue(7, false);
}
@ATT_Turan

Thanks for your input!

1- I'm not yet %100 sure where the brackets are necessary, so I've been using them just in case. Good to know I don't need them here, but I may still continue using them because being a beginner, I tend to get lost in the code and the braces help me distinguish what is what. As far as I know, there is no negative to using them, right?

2- This is super interesting! So, I can just check whether a conditional is true or not inside a setValue function? I'll be sure to use this in the future.

As for the indentations, I was doing that at the start but stopped doing it somewhere along the way. I should pay more attention to it, especially when I'm sharing said code for others to read.

One final question that is tangentially related:

If I define a variable, for example inside a pre eval notetag, like this:

var x = (y)

and then try to use that variable inside another notetag(on the same actor,skill, state etc.), for example post eval, will the plugin recognize that variable? Or does that variable only exist within that notetag and I would have to use a global variable instead?
As far as I know, there is no negative to using them, right?
no downside, but your code takes up a few extra lines and if youre not using proper indenting, it makes your code harder to read.

This is super interesting! So, I can just check whether a conditional is true or not inside a setValue function? I'll be sure to use this in the future.
at the end of the day, IF statements runs off of boolean values. if you already have a boolean value from your IF condition, you can cut the middle man and just use that value directly since youre assigning boolean values based on it

Or does that variable only exist within that notetag and I would have to use a global variable instead?
variables defined in a notetag only exist within it. the second you leave it its gone. what you do with the game variables and switches is a way to go about it, but another is to add properties to objects thatll still exist when you need them. the user, the target, weapons if you use independent items, $gameTemp, whatever you want. using what you wrote as an example:

in your opener skill, you'd set user.openerA and user.openerB to be true, then
Code:
<Custom Requirement>
value = user.OpenerB;
</Custom Requirement>

<Before Eval>
target.preHp = target.hp;
</Before Eval>

<Post-Damage Eval>
user.openerA = false;
user.openerB = value >= target.preHp;
</Post-Damage Eval>
the benefit of using your battlers is that if multiple battlers can have these effects, you dont have to reserve a variable for each. if you use switch 6/7 to enable your finisher, then any battler with access to the finisher can use it, and anyone using it disables it for everyone. if you make them personal properties, then everyone gets to use theirs based on their own property independent of eachother
@Robro33

These abilities are only used by one actor, so using global switches shouldn't be a problem but what you're explaining seems incredibly useful if I understood it correctly.

Instead of defining a variable with

Var x = (y)

I can define a property to an object like

user.x = (y)

and this property will more or less act like a variable and I'll be able to access it from anywhere that can refer to that object. Is my understanding correct?
I tend to get lost in the code and the braces help me distinguish what is what. As far as I know, there is no negative to using them, right?
No, but as Robro says it's more characters that don't provide meaning and your brain has to filter out.

To me, this
Code:
if (this.isDamage() && value >= $gameVariables.value(21)) {
$gameSwitches.setValue(7, true);
} else {
$gameSwitches.setValue(7, false);
}

takes slightly longer to look at and understand than this
Code:
if (this.isDamage() && value >= $gameVariables.value(21))
    $gameSwitches.setValue(7, true);
else
    $gameSwitches.setValue(7, false);
There are fewer extraneous characters and fewer lines total. And, of course, the single-line solution I find the easiest because I don't have to look at each of those setValue() calls to see if they're referencing the same switch or something different.

If your eyes or brain work differently, that's okay, I'm just offering the tips.

2- This is super interesting! So, I can just check whether a conditional is true or not inside a setValue function?
A value is a value and can be used anywhere that value is relevant.

Just like you could do this
Code:
let x = 2 + 5;
$gameVariables.setValue(42, x);
but you could more directly do
Code:
$gameVariables.setValue(42, 2 + 5);

All that an if statement is doing is looking at a boolean value to be true. If the only thing you're doing after that is setting a boolean value, then it's an unnecessary line of code.
Code:
if (x > 5)
    math = true;
else
    math = false;
is the same as
Code:
math = x > 5;

And similarly the example I provided from your code above. It's particularly helpful because it eliminates the need for the else branch and its code - that function call covers both possibilities.

I can define a property to an object...and this property will more or less act like a variable and I'll be able to access it from anywhere that can refer to that object. Is my understanding correct?
Yes. It would probably be worth your time to read through Yanfly's Tips & Tricks articles, they make extensive use of this.
@ATT_Turan

Thanks a bunch, these are all great tips. I think I'll go through all of my code and try to simplify all of it using your tips so that I can get used to doing it this way!

Yes. It would probably be worth your time to read through Yanfly's Tips & Tricks articles, they make extensive use of this.

Using properties will most likely make a lot of things so much easier for me, this is great to know. I've read a couple of those tips & tricks but looks like I should take the time to read all of them in their entirety. I can probably find a lot more resources on this if I google something like "javascript object property." Looks like I'm spending another night reading and taking notes 😀
I always recommend to people who are trying to use JavaScript in their projects to choose a tutorial (I like the w3schools) and just do it in order.

None of the individual lessons/chapters take very long, and skipping around to just learn what you think you need to accomplish a specific RPG Maker task means you don't know what you don't know 😉

Good luck with your project.

Original thread: https://forums.rpgmakerweb.com/threads/trouble-with-resetting-attack.168723/