Variable in damage formula returns 'null'

I want to set the value of a healing skill to a variable.
When I simply put v[107] I got "v is undefined".
I then changed it to $gameVariables[107]. This produced no error message and the text in the skill description box correctly showed "Heal 60 HP]
However, when I went to use it nothing happened and the console simply showed "null"

Here is how the damage formula looks in the editor:
1658135771221.png


Is there some arcane method known only to the mystics and sages of our time to get a variable to work in a damage formula?

Thanks.
Wasn't the correct script call something like $gameVariables.value(id)?
It would be:
Code:
$gameVariables.value(107)

Though I don't know why v[107] wouldn't work for you. It should also be valid, unless you did something weird, or you've got a plugin interfering with it somehow. Was there definitely a value stored in that variable at the time?
@TheoAllen That works perfectly. Thank you. I leave it to you to decide if you are a mystic or you are a sage.

Was there definitely a value stored in that variable at the time?
Yes, otherwise the text in the skill description would have read "Heal 0 HP".
It was the lack of .value that was the problem. I'm still doing things the Ruby way where something like that was not necessary, so it never occurred to me that it was needed here.
@TheoAllen That works perfectly. Thank you. I leave it to you to decide if you are a mystic or you are a sage.


Yes, otherwise the text in the skill description would have read "Heal 0 HP".
It was the lack of .value that was the problem. I'm still doing things the Ruby way where something like that was not necessary, so it never occurred to me that it was needed here.
The lack of .value was the issue when you were trying to use $gameVariables, but it was not the problem when you were trying to use v[107]. It is most definitely a valid shortcut to use v[id] in a damage formula, so you still have something whacky going on.

I suspect that the source of the issue lies in whatever you're doing to display the damage value in the skill description. Are you using a plugin to do that?
In a damage formula I recommend using (v[123] || 0) (this is what I've put on the Script Call List) or, like others have suggested, the usual $gameVariables.value(123).

The difference is that v[x] in the damage formula is a "raw" value reference, thus will be undefined if that variable has not been set yet during the playthrough.
I suspect that the source of the issue lies in whatever you're doing to display the damage value in the skill description. Are you using a plugin to do that?

No, not at all. In the editor the skill description is written:
Heals \v[107] HP
and so it displays whatever the current value of that variable is.

@caethyril I think I now see why the simple version didn't work - the result of doing all the testing on a test map which bypasses all the setup stuff of a proper New Game.

Can I ask you - what is the purpose of the || 0 part of the damage formula you suggest?
In JavaScript, A || B returns:
  • A if it is "truthy" (i.e. auto-converts to true);
  • B otherwise.
[Edit: e.g. v[123] || 0 returns the value of variable 123, or 0 if that value is either undefined or 0.]

This is due to the short-circuit behaviour of the logical OR operator, details here:
Also, in case you're confused by the word "truthy" in this context, here's an explanation:
@caethyril Thank you. I think I understand that now, and every little helps.
In a damage formula I recommend using (v[123] || 0) (this is what I've put on the Script Call List) or, like others have suggested, the usual $gameVariables.value(123).

The difference is that v[x] in the damage formula is a "raw" value reference, thus will be undefined if that variable has not been set yet during the playthrough.
Is that really likely to help often enough to be considered a general rule though? If an error occurs in the damage formula, then it should fall back to using a value of zero (rather than crashing) anyway, right?

It seems to me like the only time it would help is in a situation where you've got multiple expressions in the damage formula, and you would still want to consider the rest of the formula to be valid even when that variable is undefined. In other words, the possibility of the variable being undefined is an expected and acceptable occurence.

If the variable being undefined is *not* expected behavior, then simply replacing it with a zero might not yield the intended amount of damage, meaning that a bug is still occurring, even though you prevented a formal error from triggering. This could actually be a bad thing, because it might make the bug harder to notice. If you see that your skill is consistently doing 0 damage, then it's pretty obvious that there is a problem. But if it's doing damage (just the incorrect amount), then you might not necessarily realize it.

So if it's really only helpful when the possibility of the variable being undefined is both expected and acceptable, then what is the value in using it to try to protect against *unexpected* occurences?

It's pretty late right now, so I definitely could be overlooking something. I just personally can't think of a lot of situations off the top of my head where I would accidentally have an undefined variable in my damage formula, yet I would still want the engine to carry on and evaluate the rest of my damage formula as if that's not a problem.
Is that really likely to help often enough to be considered a general rule though? If an error occurs in the damage formula, then it should fall back to using a value of zero (rather than crashing) anyway, right?

It seems to me like the only time it would help is in a situation where you've got multiple expressions in the damage formula, and you would still want to consider the rest of the formula to be valid even when that variable is undefined. In other words, the possibility of the variable being undefined is an expected and acceptable occurence.

If the variable being undefined is *not* expected behavior, then simply replacing it with a zero might not yield the intended amount of damage, meaning that a bug is still occurring, even though you prevented a formal error from triggering. This could actually be a bad thing, because it might make the bug harder to notice. If you see that your skill is consistently doing 0 damage, then it's pretty obvious that there is a problem. But if it's doing damage (just the incorrect amount), then you might not necessarily realize it.

So if it's really only helpful when the possibility of the variable being undefined is both expected and acceptable, then what is the value in using it to try to protect against *unexpected* occurences?

It's pretty late right now, so I definitely could be overlooking something. I just personally can't think of a lot of situations off the top of my head where I would accidentally have an undefined variable in my damage formula, yet I would still want the engine to carry on and evaluate the rest of my damage formula as if that's not a problem.
What you've overlooked is that a game variable in the engine starts out as undefined until a value is assigned to it. $gameVariables.value is specifically coded to return 0 if that's the case, whereas v[123] returns the raw value. Therefore, it will *always* return undefined if the variable hasn't otherwise been used yet.
What you've overlooked is that a game variable in the engine starts out as undefined until a value is assigned to it. $gameVariables.value is specifically coded to return 0 if that's the case, whereas v[123] returns the raw value. Therefore, it will *always* return undefined if the variable hasn't otherwise been used yet.
I understand that. But what I'm saying is that I don't see why it would be generally desirable behavior for the damage formula to just treat that that value as a zero and then continue evaluating the rest of the formula as if there is no problem.

I'd rather it just stop evaluating, and return a flat 0, so that I can easily recognize that I screwed up and failed to set the variable to whatever value it was supposed to be before it was supposed to be be used.

Preventing unintended behavior from triggering an error isn't always a good thing. That's analogous to disabling your body's ability to feel pain. Your body uses pain to let you know that there is a problem. Ideally, you don't want to simply disable the pain--you want to put a stop to whatever is causing that pain.

Similarly, if your damage formula is expecting a variable to contain a real value, then simply replacing the undefined variable with the value of 0 isn't going to make the formula do the amount of damage that you intended. All it's going to do is make the damage formula evaluate to some other incorrect value, which might obstruct your ability to notice the problem. A developer isn't really doing themself any favors by hiding errors from themself.

I get that there *can* be situations where you expect the possibility of the variable being undefined, and that the formula is designed in a way where that's alright, but I suspect that that's moreso the exception than the rule.
@Arthran - here's a basic example:

1 + v[1]
  • If variable 1 has not been set yet this playthrough, the result will be undefined. By default this will get converted to an output of 0.

  • If variable 1 is 0, the result will be 1.
Replacing v[1] with (v[1] || 0) makes both of these situations produce the same result, i.e. 1.

The usual behaviour people expect from game variables is that they all start at 0. So I think it's a good idea to make the damage formula version work in the same way.

Edit: there is also a note on the relevant Script Call List entry stating the reasoning. People are free to avoid the || 0 safety check if they wish, the list is just a reference.
@Arthran - here's a basic example:

1 + v[1]

  • If variable 1 has not been set yet this playthrough, the result will be undefined. By default this will get converted to an output of 0.

  • If variable 1 is 0, the result will be 1.
Replacing v[1] with (v[1] || 0) makes both of these situations produce the same result, i.e. 1.
I definitely understand the concept. But what I'm saying is that, what if variable 1 isn't supposed to hold a value of 0 at that moment? What if I'm actually wanting it to hold a value of 1, and thus expecting the damage formula to evaluate to 2?

But say that I screwed up somewhere, and the variable is undefined. Then it gets converted to 0 by your failsafe, and then the damage formula evaluates to 1.

But the problem is, if I'm expecting the damage formula to evaluate to 2, then I wind up with the wrong result either way. What's the point in your failsafe if the formula winds up returning an unintended value either way?

I don't see how forcing it to evaluate to some incorrect non-zero value is really any better than just letting it trigger the error and evaluate to an incorrect zero value instead. As I mentioned before, it honestly seems worse to me, because it could obstruct your ability to realize that it's spitting out an incorrect value.
I definitely understand the concept. But what I'm saying is that, what if variable 1 isn't supposed to hold a value of 0 at that moment? What if I'm actually wanting it to hold a value of 1, and thus expecting the damage formula to evaluate to 2?

But say that I screwed up somewhere, and the variable is undefined. Then it gets converted to 0 by your failsafe, and then the damage formula evaluates to 1.

But the problem is, if I'm expecting the damage formula to evaluate to 2, then I wind up with the wrong result either way. What's the point in your failsafe if the formula winds up returning an unintended value either way?

I don't see how forcing it to evaluate to some incorrect non-zero value is really any better than just letting it trigger the error and evaluate to an incorrect zero value instead. As I mentioned before, it honestly seems worse to me, because it could obstruct your ability to realize that it's spitting out an incorrect value.
Your modification to the example doesn't change anything. As soon as you put "undefined" into a formula, it'll end up having a final result of NaN, which the engine converts to 0.

Let's say you have a skill that deals 100 damage plus 100 more for each enemy of a certain type you've killed, and killing that enemy increments variable X by 1.

100 + (100 * v[123])

If you use the skill when no enemies have been killed yet, it'll deal 0, because v 123 is undefined due to not having been assigned a value yet.

100 + (100 * (v[123] || 0))

If you use the skill with this formula and no enemies killed, it'll deal 100 damage as intended, because there's no undefined value causing a NaN.
Your modification to the example doesn't change anything. As soon as you put "undefined" into a formula, it'll end up having a final result of NaN, which the engine converts to 0.

Let's say you have a skill that deals 100 damage plus 100 more for each enemy of a certain type you've killed, and killing that enemy increments variable X by 1.

100 + (100 * v[123])

If you use the skill when no enemies have been killed yet, it'll deal 0, because v 123 is undefined due to not having been assigned a value yet.

100 + (100 * (v[123] || 0))

If you use the skill with this formula and no enemies killed, it'll deal 100 damage as intended, because there's no undefined value causing a NaN.
My modification to the example does change things, because it's illustrating that the result winds up wrong either way. Caethyril's example was of a situation where the variable was actually supposed to contain the value of 0, so it obviously worked out properly to convert the undefined variable to 0. But the problem is that there is an infinite amount of numbers that *aren't* 0, and if the variable is supposed to hold one of those numbers, then converting it to 0 will yield an incorrect result. If you're intending to do 2 damage, then it's just as incorrect for the formula to return 1 as it would be for the formula to return 0.

So in the case of the variable being *unexpectedly* undefined, there is no value in using || 0. You're just trading one wrong result for another wrong result that is potentially harder to notice.

In the example you just now made, you're not talking about an *unexpected* case of the variable being undefined. You're talking about a case where you did expect it, and built your formula in a way that makes it okay. That's not the same thing.

I previously acknowledged that cases like that can exist. But my point was that I don't believe that you should *always* use || 0 as a general rule. You should definitely use it when you are expecting to need it, but I don't see the value in using it blindly. In cases where you aren't expecting to need it, it's not necessarily going to protect you from undesired behavior (and can actually obscure it).

I think a much better general rule is: "always define your variables before you try to use them."
My modification to the example does change things, because it's illustrating that the result winds up wrong either way. Caethyril's example was of a situation where the variable was actually supposed to contain the value of 0, so it obviously worked out properly to convert the undefined variable to 0. But the problem is that there is an infinite amount of numbers that *aren't* 0, and if the variable is supposed to hold one of those numbers, then converting it to 0 will yield an incorrect result. If you're intending to do 2 damage, then it's just as incorrect for the formula to return 1 as it would be for the formula to return 0.

So in the case of the variable being *unexpectedly* undefined, there is no value in using || 0. You're just trading one wrong result for another wrong result that is potentially harder to notice.

In the example you just now made, you're not talking about an *unexpected* case of the variable being undefined. You're talking about a case where you did expect it, and built your formula in a way that makes it okay. That's not the same thing.

I previously acknowledged that cases like that can exist. But my point was that I don't believe that you should *always* use || 0 as a general rule. You should definitely use it when you are expecting to need it, but I don't see the value in using it blindly. In cases where you aren't expecting to need it, it's not necessarily going to protect you from undesired behavior (and can actually obscure it).

I think a much better general rule is: "always define your variables before you try to use them."
That would require always having a "variable operations - set - 0" for every variable you're using in the game, in an event that must be placed somewhere in the game that it will definitely run before the player encounters any instance of v[123] in a combat formula.

I'm not sure what you're trying to argue any more, because the only case where the variable *could* be undefined is when it hasn't been used anywhere yet. What possible case are you imagining where you're using that notation but don't expect a variable to be undefined?
That would require always having a "variable operations - set - 0" for every variable you're using in the game, in an event that must be placed somewhere in the game that it will definitely run before the player encounters any instance of v[123] in a combat formula.

I'm not sure what you're trying to argue any more, because the only case where the variable *could* be undefined is when it hasn't been used anywhere yet. What possible case are you imagining where you're using that notation but don't expect a variable to be undefined?
I'm *not* using that notation while expecting the variable to be undefined, and I'm not trying to claim that there is never a benefit to using that notation. What I'm arguing is against Caethyril's assertion that one should *always* add || 0 along with every usage of v[id]. I was saying that I don't see a reason why it should be a general rule that you *always* do that, because there are going to be cases where you're never expecting the variable to be either undefined or 0, and in those cases, you wouldn't be doing yourself any favors by using that notation.

For example, look at Kes' formula in this thread. It doesn't look like they *want* that variable to ever return a value of 0. They were expecting it to return 60 (not 0), so tacking a || 0 into there would have served absolutely no purpose, and I was simply questioning the validity of advising him/her to add a || 0. I think that there are likely plenty of situations where your formula might always be expecting the variable to be a non-zero value, and adding || 0 to the expression is always going to be an incorrect course of action in those situations. It would serve no purpose, because in the event of that variable being undefined, your damage formula would wind up returning the wrong value regardless.

If you're intentionally planning to use the variable before it ever receives an initial value, and you intend for the initial value to be 0, then sure... in that situation, it makes perfect sense to add || 0 to the expression. What I was questioning is whether or not that situation is actually dominate enough that "always add || 0" should be a general rule. To me, it seems more like you should add || 0 when it makes sense to, and you should not add it when it does not make sense to.
What I'm arguing is against Caethyril's assertion that one should *always* add || 0 along with every usage of v[id]. I was saying that I don't see a reason why it should be a general rule that you *always* do that
You are perfectly entitled to that view, it's a valid approach. I'd appreciate it if you'd stop putting words in my mouth, though: I never stated it should "always" be done in one way or another. The closest I came to that was saying that I've put my recommendation on the Script Call List:
In a damage formula I recommend using (v[123] || 0) (this is what I've put on the Script Call List) or, like others have suggested, the usual $gameVariables.value(123).

The difference is that v[x] in the damage formula is a "raw" value reference, thus will be undefined if that variable has not been set yet during the playthrough.
The usual behaviour people expect from game variables is that they all start at 0. So I think it's a good idea to make the damage formula version work in the same way.

Edit: there is also a note on the relevant Script Call List entry stating the reasoning. People are free to avoid the || 0 safety check if they wish, the list is just a reference.
I also explained what the || 0 does, to help people understand what it's for and why they may (or may not!) want to use it:
e.g. v[123] || 0 returns the value of variable 123, or 0 if that value is either undefined or 0.

On the Script Call List you can find "Get Game Variable" in the "Damage Formula" section, currently row 1027. Here's the accompanying note:
Because this is a raw reference, variables that have not been assigned a value will be undefined. v[1] || 0 returns 0 if v[1] is "falsy". More information here: Logical OR.

Alternatively you can use the usual methods:
  • $gameVariables.value(variableId)
  • $gameVariables.setValue(variableId, value);
Are we done here?
Are we done here?
I think so. My query has been well answered.
I shall Report the thread and ask for it to be closed.

Original thread: https://forums.rpgmakerweb.com/threads/variable-in-damage-formula-returns-null.149574/