Setting Variable to Enemy HP based on Enemy Index variable?

zXManyRegretsXz

Game Enthusiast
Member
Joined
Jan 11, 2022
Messages
22
Reaction score
45
First Language
English
Primarily Uses
RMMZ
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.
 

Kaymon145

Veteran
Veteran
Joined
Apr 22, 2022
Messages
34
Reaction score
17
First Language
English
Primarily Uses
RMMV
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?
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,677
Reaction score
2,761
First Language
EN
Primarily Uses
RMMZ
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?
 

zXManyRegretsXz

Game Enthusiast
Member
Joined
Jan 11, 2022
Messages
22
Reaction score
45
First Language
English
Primarily Uses
RMMZ
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!
 

Latest Threads

Latest Posts

Latest Profile Posts

NUMINA Part 1 is finally out ✨ Get it now with a 10% launch discount!
Made this simple Training Dummy :D

obraz_2022-07-01_095700524.png
I'm still undecided weather or not I should add minor swearing to my game. Like I'm going for an all ages demographic... but the idea of a cartoon character saying dammit, hell, or crap in normal conversation is really funny to me.
If we assume an Elf's strong suit is Dexterity (AGI) & Intellect (MAT), I imagine Strength (ATK) & Constitution (DEF) would be among their lowest stats. But I'm having a hard deciding which should be lower, Strength or Constitution?
I'm really sorry to have to do this, but I'm going to have to start blocking those who repeatedly put up sexual content and offer absolutely nothing (that I've seen) related to what this community is supposedly created for.
Don't take it personal. I'm a rape victim with permanent injuries who can never enjoy 'it' ever again, so it's the last thing I want to have put in my face. :(

Forum statistics

Threads
123,219
Messages
1,155,007
Members
161,600
Latest member
googoogagaman
Top