How to determine enemies health percentage and compare with another enemy?

So, basic run down is a fight where one enemy will heal someone in the enemy troop with the lowest health in percent. Figured, alright, so take HP, divide by MHP, do that for each troop member and compare the results, using conditionals for targeting. Simple enough.


Well, if you divide HP by MHP via Control Variable, it ends up throwing out the number, as it's a float. So, then after a whole slew of other steps, I end up with this script call:


$game_variables[105] = $game_troop.members[1].hp
$game_variables[106] = $game_troop.members[1].mhp
$game_variables[102] = (($game_variables[105].to_f / $game_variables[106].to_f) *
100.0)


Where variables 105/106 are temporary, just meant to store the HP and MHP values, as you can't convert them to floats directly. So, instead, I have 2 variables that will stand-in, since they CAN be converted to floats. Take the resulting number, multiply it by 100, bam, you have a percentage.


Except now I get this lovely error:


Script 'Game_Interpreter' Line 1411: NoMethodError occurred.
undefined method 'hp' for nil:NilClass


But, here's the kicker: the call will work fine twice. It's only on the *third* call that it crashes. Am I being an idiot here? I feel like there has to be an easier way to do this, as it seems a fairly "common" mechanic.
are you always checking enemy #1 with those formulae and the third time it crashes? Or are you looping through different enemies?


Because if you're comparing it with different enemies, it might simply be that your index gets wrong in a loop and the enemy it tries to check doesn't exist in that troop...
I'm not looping through different enemies. Always checking enemy #1. What I actually do is that 102, 103, and 104 are different variables to represent different troop members (enemy #1, #2, #3 respectively). When I made 3 script calls back to back, adjusting the formula appropriately, it crashed consistently. So, then I removed the third call, crashes on the second turn. Removed the second call, crashes consistently on turn 3.


The idea is to store the health percent in a variable, then do it again with a different variable, and again with a different variable (changing $game_troop.members[??].?? each time), and then comparing those variables.


Can I ask what you mean by "your index gets wrong in a loop?" Because I've also had some weird stuff going on with target indexing, but I assumed it was because I'm garbage at math.
Bump. Any help with this would be massively appreciated 🙂
The issue seems to have lied in the fact that $game_troop.members starts its index at 0, not 1. So, as I have 3 enemies, as soon as it would try to call the third one, it would fail. Still not sure why it would crash when the third one wasn't called, but I haven't been able to reproduce the crash with the following code snippet:


$game_variables[105] = $game_troop.members[0].hp
$game_variables[106] = $game_troop.members[0].mhp
$game_variables[102] = (($game_variables[105].to_f / $game_variables[106].to_f) *
100.0)
$game_variables[105] = $game_troop.members[1].hp
$game_variables[106] = $game_troop.members[1].mhp
$game_variables[103] = (($game_variables[105].to_f / $game_variables[106].to_f) *
100.0)
$game_variables[105] = $game_troop.members[2].hp
$game_variables[106] = $game_troop.members[2].mhp
$game_variables[104] = (($game_variables[105].to_f / $game_variables[106].to_f) *
100.0)


So, take that as you will. I'm still not certain that the error *doesn't* arise due to some sort of error because of a lack of rounding, but I'll make a new thread/re-open this one should it be needed. So, thank you Andar, sorry for not properly vetting your help earlier. It's vastly appreciated!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-determine-enemies-health-percentage-and-compare-with-another-enemy.58229/