MV Why are the variables not applying?

Posts I have seen and attempted to no success:

^ Also tested in a new project and no plugins! = same result. ^

Hello, I am trying to do the following:

Enemy HP persists when escaping, losing the battle etc.



However, as you can see from the GIF video above, it works when I re-enter the battle the first time but if I try to escape before doing any actions the second time around, the HP seems to reset and the variable also sets itself to 1000HP again instead of staying at the modified amount.

Here's my current setup:

1642050006421.png


1642050488664.png


This method makes the Orc have 1HP for some reason. Deleting the CHANGE ENEMY HP creates the same issue as before (resetting the variable HP)

I even checked to see where the variable re-sets itself and it isn't at the start of the battle. It starts to give me issues right after I escape without doing anything else on my first turn like in the GIF video.

1642050889090.png


I just don't understand what is happening. I have tried the other post's method as well but it was confusing to understand so I don't think I did it correctly at all.

I just want it to work for this battle only :/

Please send me tested solutions with some sort of visual example. I'm slow understanding explanations through text.

Thank you.
1642051651707.png

Unless I'm wrong, these variables are defined AFTER the battle as the "If Escape" and "If Lose" let think. You can't make battle commands happen outside of battles.
You need to workaround to define this variable. Perhaps add a page event inside the troop to change the variable into the enemy's health at the end of the turn (or every turn). Else you'll need scripting or a plugin.
Your first event doesn't work because it's a map event. There isn't any such thing as "Enemy #1" when you're not in battle, so it doesn't have anything to set your variable to.

The second thread you linked to has example code by Caethyril that you need to use in a troop event.

Edit: Stupid response, or else the text window wouldn't have an HP amount to display...
View attachment 212728
Unless I'm wrong, these variables are defined AFTER the battle as the "If Escape" and "If Lose" let think. You can't make battle commands happen outside of battles.
You need to workaround to define this variable. Perhaps add a page event inside the troop to change the variable into the enemy's health at the end of the turn (or every turn). Else you'll need scripting or a plugin.
^ If I take out the after-battle variable, the HP total doesn't update. I kinda need to know that the variable has stayed the same after battle (I checked the variable and switches list too while in-game. It doesn't update).

Your first event doesn't work because it's a map event. There isn't any such thing as "Enemy #1" when you're not in battle, so it doesn't have anything to set your variable to.

The second thread you linked to has example code by Caethyril that you need to use in a troop event.
1642052552882.png

^ Even this as well? When they talk about 'storing' the variable, I figured this was done in the event window and not the troop window?
^ If I take out the after-battle variable, the HP total doesn't update. I kinda need to know that the variable has stayed the same after battle (I checked the variable and switches list too while in-game. It doesn't update).


View attachment 212731
^ Even this as well? When they talk about 'storing' the variable, I figured this was done in the event window and not the troop window?
It is done in the troop windows. You cannot use the parameters of a troop enemy as a value for a variable outside of a battle, because there are no troop to begin with.

You need to make a 2nd page on your troop events, this second page should be activated at the end of each turn and it should change the variable into the enemy's health.
Even this as well? When they talk about 'storing' the variable, I figured this was done in the event window and not the troop window?
Hrm. Caethyril does claim it will reference the most recent troop, and the OP's response says it works.

If that's the case, the problem must be with your troop event. Where do you actually initialize variable 2 for the first time? I see it getting multiplied by -1, but where is it defined? You should be using the same variable in both events.

That's why it's not "carrying over" from your second battle - it's not actually getting carried into your second battle. You're seeing 1000 because you didn't hit it the second time.

It is done in the troop windows...You need to make a 2nd page on your troop events
If you read the reference thread, this does not appear to be correct. Caethyril knows her stuff.
Hrm. Caethyril does claim it will reference the most recent troop, and the OP's response says it works.

If that's the case, the problem must be with your troop event. Where do you actually initialize variable 2 for the first time? I see it getting multiplied by -1, but where is it defined? You should be using the same variable in both events.


If you read the reference thread, this does not appear to be correct. Caethyril knows her stuff.
another reference suggest doing so :

Code:
Turn 0:
If (switch[1] on) {
  enemy1 HP = variable 1
} else {
  switch[1] = on
}

Turn 1 + X:
variable 1 = enemy1 HP

And you're right, I just noticed the problem :
1642053231357.png

You're doing the calculations in the variable 2, yet you're using the variable 1 to remove HPs.
The code here is entirely wrong. It should be something like this :

variable 1 *= -1
variable 1 += Max HP of #1 ORC
change enemy hp : #1 ORC, - variable 1


Or :
variable 1 = Max HP of #1 ORC - variable 1
change enemy hp : #1 ORC, - variable 1

(both works)

What is variable 2's purpose to begin with ?
Yes, that's what I said 😛
Yes, that's what I said 😛
Yeah pretty much. the variable 2 seems useless. though I don't understand why the reference suggested doing:
variable 1 *= -1
variable 1 += Max HP of #1 ORC
change enemy hp : #1 ORC, - variable 1


when you can do:
variable 1 = Max HP of #1 ORC - variable 1
change enemy hp : #1 ORC, - variable 1


It's complicating the code for nothing, but yeah both do the same maths in the end.

EDIT : Oh, maybe because the editor don't allow to set the variable to MAX HP then - variable 1, that would make sense. In this case to prevent using script call and to only use 1 variable, the first option make sense.
Hrm. Caethyril does claim it will reference the most recent troop, and the OP's response says it works.

If that's the case, the problem must be with your troop event. Where do you actually initialize variable 2 for the first time? I see it getting multiplied by -1, but where is it defined? You should be using the same variable in both events.

This is where I am a little puzzled.
I have two variables:
0001: Orc Current HP
0002: Orc HP


In the reference thread with Caethyril, I saw that they had two different variables. one with 0001: Boss HP and Variable 0009: Boss HP. So I followed that.

But in terms of where I've defined 0002, it's only in troop events (EDIT: I've set 0002 to the enemy HP to see if that helps...it doesn't):
1642054360264.png

1642054371065.png

Do I need to set a value to it elsewhere?

And I don't understand why I would need another page in troop events if Caethyril never mentioned that...?

I also put a conditional branch in since that's what worked for the OP, but hasn't worked for me.
It's just not staying the same. I have made a new page anyways to see what the variable is doing every turn and it updates then. But if I leave the fight, it goes back to the original HP 🙁

With the conditional branch I get this:

Action taken: Attack 1x > Escape

1642054264367.png


When I enter the battle again...:

1642054317256.png
EDIT : Oh, maybe because the editor don't allow to set the variable to MAX HP then - variable 1, that would make sense. In this case to prevent using script call and to only use 1 variable, the first option make sense.

I don't mind using script calls if I need to! I just wouldn't understand where to put it lol
You can use this script call :
Code:
$gameVariables.setValue(1, $gameTroop._enemies[0].mhp - $gameVariables.value(1))
This script call will basically do this :
variable 1 = enemy #1 Max HP - variable 1
1642054719668.png

the script call should replace these 2 control variables commands.

Here's how to understand the script :
$gameVariables.setValue(variable ID, value) - Is a function that allows to change a variable's value
$gameVariables.value(variable ID) - Is a function that returns the value of a variable
$gameTroop._enemies[0].mhp - Return the Max HP of the enemy #1
$gameTroop._enemies[x].mhp - Return the Max HP of the enemy #(x+1), for instance $gameTroop._enemies[2].mhp will return the Max HP of the enemy #3.

EDIT : corrections !
You can use this script call :
Code:
$gameVariables.setValue(1, $gameTroop._enemies[0].mhp - $gameVariables.value(1))
This script call will basically do this :
variable 1 = enemy #1 Max HP - variable 1
View attachment 212738

the script call should replace these 2 control variables commands.

Here's how to understand the script :
$gameVariables.setValue(variable ID, value) - Is a function that allows to change a variable's value
$gameVariables.value(variable ID) - Is a function that returns the value of a variable
$gameTroop._enemies[0].mhp - Return the Max HP of the enemy #1
$gameTroop._enemies[x].mhp - Return the Max HP of the enemy #(x+1), for instance $gameTroop._enemies[2].mhp will return the Max HP of the enemy #3.

EDIT : corrections !
Thank you, Reizod for the script call. That helped solve my issue halfway 🙂

But it now just KINDA works. KINDA.



As seen in the GIF video now, the HP does update outside of battle and after Harold's turn. But there's still these issues:

1. Doesn't update after Harold's first turn (I have to attack twice for it to update when I escape.)
2. When joining the battle for the second time, it doesn't show updated HP but some calculation that happens because of the current setup. (goes to 99HP. Actions taken: Escaped immediately after my first escape.)

Current Setup:
1642055849648.png

1642055869182.png

1642055884473.png
Just do it the way you did it before, set the variable after the battle on the map.
Undo everything you've done, only change the troop event 2 variables control you did before by the script call, and everything should be in order !

Like this :

1642056169480.png
1642056218522.png
@Reizod Aha!! That solved all my issues! Thank you so much 😀
(issue #1. Just needed to add 'Turn End' to the Troop Event page I wanted to see the updated HP in.)
(Issue #2. After adding the script call, setting the variable after the battle got rid of that weird calculation error I was getting.)

I can safely say this post is solved!
@Reizod Aha!! That solved all my issues! Thank you so much 😀
(issue #1. Just needed to add 'Turn End' to the Troop Event page I wanted to see the updated HP in.)
(Issue #2. setting the variable after the battle got rid of that weird calculation error I was getting.)

I can safely say this post is solved!
No problem ! Don't hesitate to ask if you have any issue understanding the script call I gave you. script call are nice, but they are way better when you understand exactly what they're doing 😅
No problem ! Don't hesitate to ask if you have any issue understanding the script call I gave you. script call are nice, but they are way better when you understand exactly what they're doing 😅

From how I understand it:

I am setting Variable: 0001 'Orc Current HP' to enemy#1's MHP in the troop window and subtracting its value with the one that was stored in Variable 0001 originally.

That allows the editor to use the CHANGE ENEMY HP function and minus the enemy HP with the total that is stored in 0001 after the script call.

so 5500HP minus 40 HP damage (5460 for the sake of the example)
Variable 0001 = 40

CHANGE ENEMY HP minus Variable 0001 (40) = 5460.

I gotta be at least half right, right?

I don't understand how [x] (x+1) would work though. What does that mean?
Moved to: Plugin Support


You said the post is resolved, but you are asking more questions. When you're ready to have it locked, please report your post and let us know.
From how I understand it:

I am setting Variable: 0001 'Orc Current HP' to enemy#1's MHP in the troop window and subtracting its value with the one that was stored in Variable 0001 originally.

That allows the editor to use the CHANGE ENEMY HP function and minus the enemy HP with the total that is stored in 0001 after the script call.

so 5500HP minus 40 HP damage (5460 for the sake of the example)
Variable 0001 = 40

CHANGE ENEMY HP minus Variable 0001 (40) = 5460.

I gotta be at least half right, right?

I don't understand how [x] (x+1) would work though. What does that mean?
You see, in the editor, the first enemy is #1, then #2, #3 etc etc.

however, in reality, arrays are used for these informations. and arrays uses index. index start at 0 and not 1. You'll see this happen alot in RPG Makers, for instance $gameParty.members()[1].hp is the HP value of the 2nd member of the party, and not the first one ! Because the first member is $gameParty.members()[0].hp (the first in the array).

this is why $gameTroop._enemies[0].mhp returns the max HP of the enemy #1. Because 0 + 1 = 1.
by the way, while you're playtesting, try to press F8 to open the console, if you go battle the ORC and type this : $gameTroop._enemies[0] then press enter, you'll see every parameters that the battler have, hp, mhp, atk, def, and much more !
1642058164792.png

it's the same for every arrays.

$gameParty.members()[0] ->
1642058230520.png
@Reizod ahh! Thank you for the in-depth explanation. This helps a lot.
Also, how correct/incorrect was I in my understanding of the script call? lol

Original thread: https://forums.rpgmakerweb.com/threads/why-are-the-variables-not-applying.143912/