Basic hp/mp scan script

I've been having a really tough time with a basic enemy HP and MP "scan" script.  I've tried Yanfly Engine Ace - Enemy Target Info, but it's actually way too fancy for what I want.  I just want to have an actor use a skill that shows whatever enemy he/she targets' current HP and MP.  I think I'm close. 


Right now, I've got a skill that adds a state to the target called "Revealed".  The index of the state is '026' in my game. 


The skill also calls a common event which has a script that sets two game variables, which I then display in a battle text window.  Here's the common event:


Untitled.png


The script text I have (which is also in the image) is:


$game_troop.members.any? { |m| m.state?(26)$game_variables[5] = m.name$game_variables[6] = m.hp$game_variables[7] = m.mp}


I'm not very good with Ruby, but my understanding is that this script is supposed to check to see if *any* enemy in a troop has the state 'revealed' (index 26) applied to it, and then set my game variables with that target's name, and current hp and mp.  The problem is though that no matter which enemy in a troop larger than one that I target, it always only returns enemy A's hp and mp.


Can someone point out what stupid thing I may be doing here?


Thank you!!!
The logic in your script call is wrong.  The first line is meant to return true or false based on whether or not ANY member has the state.  But the code within the block sets the variables whether the state is applied or not.  The fact that the variables are set is probably returning true after the first member is tested.


You need to rethink your logic.  What if several members have the state?  Which one should the variable be set to?


Try something like this - this will set the variables to the first enemy that has the state, and ignore the rest:


Script: mbrs = $game_troop.members.select { |m| m.state?(26) }
if mbrs.size > 0
$game_variables[5] = mbrs[0].name
$game_variables[6] = mbrs[0].hp
$game_variables[7] = mbrs[0].mp
end


You should set those variables to blank or 0 before the script call, so that if NO members have that state, they will not contain anything.


Note - I haven't tested this code, so you might end up with some syntax errors.


Also, if you're only interested in the living enemies and want to ignore those that have already been defeated, use alive_members instead of members.
Wow, thank you!  That is what I needed.  There's not much information out there that I could find that explains how the logic of iterating through battle troops works. 


This works perfectly!!

Original thread: https://forums.rpgmakerweb.com/threads/basic-hp-mp-scan-script.57896/