MV (SOLVED) Super basic js question. Adding a value to local var

Trying to rewrite this using local variables and I don't have any references on hand as to the correct syntax. This is the code I am modifying:

Code:
<Custom React Effect>
$gameVariables.setValue(41, $gameVariables.value(41) + 1);

if ($gameVariables.value(41) >= 2) {
$gameVariables.setValue(42, 1)
}
</Custom React Effect>

So it should say


Code:
<Custom React Effect>
var mindsEye = Sum(mindsEye, + 1);

if (mindsEye.value >= 2) {
var mindsEyeResult = 1
}
<Custom React Effect>

But I am getting NaN as a response. So clearly I am inputting the syntax wrong. Any help would be appreciated 😅

One other question however while I am at it. I need this result to be saved in between turns on the actor who has the state. Should I be using const here instead of var? Multiple actors will potentially have this state at the same time, so I need the result to be unique per actor, rather than per instance of it being run.
But I am getting NaN as a response. So clearly I am inputting the syntax wrong.
Yup! I dunno where you might have gotten that Sum() from, but JavaScript (and most languages) don't need functions to do basic math. You just type it.

Code:
var mindsEye = mindsEye + 1;

There are also a number of shorthands for simple math, such as
Code:
mindsEye += 1;
mindsEye++;

However, there are bigger issues with what you're trying to do.

You can't declare a variable and then try to use itself as a value. When you say var mindsEye you've just made a new, empty variable that isn't anything. Trying to say var mindsEye = mindsEye... is going to be undefined

It looks like you're presuming mindsEye is going to have some preexisting value, but there are two things wrong with that.
1 - If it did, you wouldn't type var (I also see this lower down under your if statement). var is only for declaring a variable, you don't need to use it again when you're using/modifying that variable (and, in fact, you can't).

2 - Variables that you declare inside a block (such as your Custom React Effect notetag) do not persist. As soon as the notetag is done evaluating its code, that mindsEye variable will disappear.

That's why if you look at almost any of Yanfly's Tips & Tricks for these plugins and notetags, they declare variables as properties of the battler. I'm not going to look to provide immediate references for you - it's a good idea for you to browse through them, you'll see it tons (you might also want to consider doing a few chapters of JavaScript tutorials if you want to start making your own notetag code).

Finally, you have more wrong syntax when you refer to mindsEye.value(41) which doesn't exist. mindsEye is not an object with a value property and I dunno what the 41 would have to do with it. I guess you didn't change a portion of the code that you copied from referencing game variables.

So the correct way to do this would be:
Code:
<Custom React Effect>
// If the battler has a mindsEye property already, keep its value. Otherwise make the property
target.mindsEye = target.mindsEye || 0;

// Then increment it by 1
target.mindsEye++;

// If it's at least 2, make a mindsEyeResult...for some reason
if (target.mindsEye >= 2)
    target.mindsEyeResult = 1;
<Custom React Effect>

I'm not sure why you need a separate mindsEyeResult variable instead of just referencing mindsEye, but there you go.

You probably also want to do things to get rid of this variable when the state goes away, unless it's supposed to keep incrementing for the entire game.
Code:
<Custom Remove Effect>
delete user.mindsEye;
delete user.mindsEyeResult;
</Custom Remove Effect>
Hey thank you for that @ATT_Turan . I was just thinking that the reason it wasn't working out was indeed what you said, that it wasn't originally defined in the first place.

So the trick to saving the properties to an actor is using target.mindsEye = target.mindsEye || 0; then if I am understanding this correctly? Is there any difference between using this as opposed to
target._mindsEye = target._mindsEye || []; ?

Regardless with the right syntax and proper formatting I should be able to do what I was looking for. Thank you again for the assistance.

End code and indeed it works!

JavaScript:
<Custom React Effect>
if (this.item().skillRangeType == "Melee" && this.item().skillDmgType == "Attack"  && this.item().skillAtkType == "Action" )
{
$gameVariables.setValue(17, 1);
target.mindsEye = target.mindsEye || 0;
target.mindsEye++;


if (target.mindsEye >= 2) {
delete user.mindsEye;
delete user.mindsEyeResult;
target.removeState(250);
}
}

if ($gameVariables.value(17) == 1) {
value = 0;
target.result().missed = true;
}
</Custom React Effect>
So the trick to saving the properties to an actor is using target.mindsEye = target.mindsEye || 0; then if I am understanding this correctly? Is there any difference between using this as opposed to
target._mindsEye = target._mindsEye || []; ?
Yes. [] is not a value, it's creating an empty array. Doing
Code:
target.mindsEye = [];
target.mindsEye++;

will produce an error.

if (target.mindsEye >= 2) {
delete user.mindsEye;
delete user.mindsEyeResult;
target.removeState(250);
This looks incorrect. Unless you've created a mindsEyeResult variable someplace outside of this notetag, it doesn't exist to need deletion. (which won't hurt anything, but is also an unnecessary line of code)
Is there any difference between using this as opposed to
target._mindsEye = target._mindsEye || []; ?
if minds eye doesnt exist itll be set to be an empty array, and if it does itll remain the same. your property will be an array (unless you defined it prior to not be one for some reason) and your code should reflect that.
I forgot to remove the Result bit, thank you for pointing that out. Code seems to be working, I just need to make sure that all instances of v[17] are changed back to 0 and then revalued for each attack, including counters and precounters. I should be able to figure this out, but damn if it isn't confusing trying to write a function that includes and excludes counterattacking a counterattack with all the possible interactions that could prevent or allow an instance of this happening, and then manually changing the target and including conditions to stop a sequence if battler is knocked back or affected by state x,y,n, etc

The help I have received from both of you has been invaluable.
Have you looked at Yanfly's Counter Control? It handles all of that for you automatically.
I do use Counter Control, but a lot of the options are gutted as a compatibility patch needed to be applied which was only ever half completed and the creator left the forums a year ago or so. I actually was able to get what I needed done using a very crude script by placing specific conditionals and recording results with variables inside the functions that are called before initiating a sequence as well as right after.

The hardest part was making a precounter skill that interrupted the initial attack and then manually changing the target back and forth between this and a substituted initial attack applied after. Thankfully I know how to save the activeAction into a var and pass that to a forceAction applied later. Kind of surprised I got it working, just need to polish it a bit so the correct sequences are applied when other states (such as force dodge) are added into the mix.

Was even able to make a SpellSteal state, where when an active battler casts a skill on target affected by state(n), then the skill is instead cast by the defender onto the initial attacker, with the initial attacker still losing MP, TP, as well as their action, while the SpellStealer doesn't spend any MP/TP points to use it. Might be a bit OP so it only functions once per cast and has a higher cost for a support ability than others, such as the Precounter still.

Heck, with the precounter I was even able to set it so if the precounter causes a knockback onto the initial attacker, their attack misses due to being out of range. Pretty neat and just like Tactics Ogre with the Swordmaster Precounter + Knockback combo.

Also, got the kinks worked out with the forceDodge (mindsEye) script, just had to reset v[17] to zero with <after eval> instead of <post-damage eval> as the latter will not run in the case of a miss. Might need to modify it a bit so that the dodge effect is applied before regular dodge and block calcs, but that shouldn't be too difficult.

Actually getting somewhat close to a demo as I finish plugging in all of these states.

Spoiler
Updated code as I had to change some things, like swapping delete user.mindsEye; to delete target.mindsEye. For some reason delete user removed the attribute from the both characters when afflicted and engaging in a counterattack trade. Now it seems to be working flawlessly. Just need to add new attributes for melee and ranged dodge calcs and set melee dodge and block/parry to zero when state is active without changing range dodge evade rate. And check interactions with other dodge skills so that one has priority over the other to avoid counting a single dodge as a +1 for both.


JavaScript:
<Custom Apply Effect>
delete target.mindsEye;
</Custom Apply Effect>

<Custom React Effect>
if (this.item().skillRangeType == "Melee" && this.item().skillDmgType == "Attack" ) 
{
$gameVariables.setValue(17, 1);
target.mindsEye = target.mindsEye || 0;
target.mindsEye++;


if (target.mindsEye >= 2) {
delete target.mindsEye;
target.removeState(250);
}
} 


if ($gameVariables.value(17) == 1) {
value = 0;
target.result().missed = true;
}
</Custom React Effect>

</Custom Select Effect>



<Custom Apply Effect>
delete target.mindsEye;
</Custom Apply Effect>

<Custom React Effect>
if ($gameVariables.value(17) == 1) {
value = 0;
target.result().missed = true;
}
</Custom React Effect>

Original thread: https://forums.rpgmakerweb.com/threads/solved-super-basic-js-question-adding-a-value-to-local-var.168268/