Setting Variable to Enemy HP based on Enemy Index variable?

Hi, I'm trying to do something that requires I get the index of the enemy I last targeted with an actor, and then that specific enemy's health. Unfortunately, Control Variable doesn't let me use a specific variable as the enemy's index, so I've taken to using script calls. The script calls I tried to use, however, didn't work and just gave me an error:
1653382277514.png

Can anyone help me? I'd rather not use conditional branches to check the variable I set the enemy index to and set a variable accordingly, but I will if that's the only option. Forgive me for that, I just find it a bit unnecessary to do.
I actually asked a fairly similar question on the MV board a few days ago... Not sure how different the code is between MZ and MV but I was trying to get the agility of the current target, the code that ended up working for me was
$gameTroop.smoothTarget(BattleManager._action._targetIndex).agi
Again I'm not sure how different MZ is but that ended up working for me, maybe something simalar could be done to get HP instead?
The error says you tried hp(), but hp is not a function, it's a value. Try this:

$gameTroop.members()[$gameTemp.lastActionData(5)].hp
$gameTemp.lastActionData is new to MZ, it's just a script version for the various "Last" options under Control Variables -> Game Data. You could use a variable instead if you want, e.g.

$gameTroop.members()[$gameVariables.value(123)].hp

The Last Target Enemy Index value is updated immediately after an action's damage & effects are applied to a target. So if this script call doesn't work for you, it might be a timing problem. In that case, you may want to specify exactly where you're trying to use this, e.g.
  • In the damage formula (use b.index() for current target index);
  • In a follow-up common event (should be OK);
  • In a troop event (if so, what conditions and when/how are they triggered?);
  • In a notetag from a plugin (if so, which tag/plugin?);
  • Somewhere else?
The error says you tried hp(), but hp is not a function, it's a value. Try this:

$gameTroop.members()[$gameTemp.lastActionData(5)].hp

$gameTemp.lastActionData is new to MZ, it's just a script version for the various "Last" options under Control Variables -> Game Data. You could use a variable instead if you want, e.g.

$gameTroop.members()[$gameVariables.value(123)].hp


The Last Target Enemy Index value is updated immediately after an action's damage & effects are applied to a target. So if this script call doesn't work for you, it might be a timing problem. In that case, you may want to specify exactly where you're trying to use this, e.g.
  • In the damage formula (use b.index() for current target index);
  • In a follow-up common event (should be OK);
  • In a troop event (if so, what conditions and when/how are they triggered?);
  • In a notetag from a plugin (if so, which tag/plugin?);
  • Somewhere else?
God, I am dumb. That was the issue, the little () after .hp. Of course, there were lots of other issues after that, but I managed to solve them myself (despite them being debatably more difficult). After a few extra things it should be ready, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/setting-variable-to-enemy-hp-based-on-enemy-index-variable.147884/