Identifying the weakest actor in battle

I want to create an assassin skill whereby the enemy automatically targets the actor with the lowest HP. Is there an efficient way to do this? As I am using a CTB system, I can't use anything that utilises 'turn', it has to be at the point that the enemy is going to act.

Thanks.
Have your skill call a common event, then find the target using the following script call and then you can force an action against that target.
Code:
target = $game_party.members[0]
$game_party.members.each do|a|
if (a.hp < target.hp)
    target = a  
  end
end

I cannot access the source code right now, but you should be able to use your_enemy.force_action(skill, target).
You can condense that down to
Code:
target = $game_party.members.min_by {|member| member.hp }
@Heirukichi and @Trihan I think I may have done a couple of things wrong.

I created the skill and gave it a particular animation and called the common event. Nothing else. In the common I put the main code (first Heirukichi's, then on another attempt Trihan's). In order to check that code was working okay, I put a bit of dialogue text. I then put the second piece of code, exactly as shown but instead of 'skill'. I put the skill id.

The common event is called - however, the enemy does a small attack at that point. It appears to be random who gets targetted at this stage. I know this is happening because the animation shows, and 1HP damage is done. (I have a snippet which ensures that 1HP is always the minimum, to avoid the Null thing.) The test dialogue appears, and then the common event tries to run the second piece of code. At which point I get the usual error message
1595508592737.png


Sigh. I know I'm a scripting idiot. What have I done wrong?
Can you screenshot your event mate?
@Trihan

Here is the skill
1595509780580.png


Here is the common event using your bit of code.
1595509852095.png
your_enemy was a placeholder, Kes. You need to replace it with the actual enemy object that's going to be acting.
@Trihan So does enemy object mean "enemy[id]"
(I did remind you that I'm a scripting idiot!)
$game_troop.members[id]
@Trihan I get the same error message when it goes to run the second code. The CE now looks like this:

1595511212680.png


I solved the first problem (acting after calling the common event and before the text) just by removing scope to 'None'. I thought I'd already done that, but saw I hadn't.
$game_troop only has 8 elements max; it's the index of the battle enemy, not the ID of the enemy in the database.

However, there's a better way to do this:

Code:
enemy = SceneManager.scene.subject
target_id = $game_party.members.min_by {|member| member.hp }.index
enemy.force_action(3, target_id)

This does require that you add the line

Code:
attr_accessor :subject
between lines 7 and 8 of Scene_Battle.
I made some tests here and I think I managed to do something in the way you are expecting it.

In the common event, just call a script with the following code
Ruby:
$game_party.members
  .select { |member| member.alive? }
  .min_by { |member| member.hp }
  .add_state(1)

It will automatically add the death state to the lowest hp character.
And by the way, if you do not filter dead characters from your possible targets, the enemy will always target a dead character since its hp is zero.
@CaRa_CrAzY Thank you for your suggestion, but I'm not aiming for this to be a death skill, but an attack which targets the weakest party member.

@Trihan Like a storm-tossed ship sailing safely into port, I think I've arrived. It does everything that it needs to without crashing. What more could anyone want in life?

Thank you so much for your help and patience. Much appreciated.
I think at this point you should just put me on retainer given how much I've helped you over the years. 😉
@CaRa_CrAzY Thank you for your suggestion, but I'm not aiming for this to be a death skill, but an attack which targets the weakest party member.

Lol, I missunderstood that part. 😛
Anyways, can you test if it works when there are dead party members?

If it is always aiming the dead character I suggest you modifying the script to something like this:
Ruby:
enemy = SceneManager.scene.subject
target_id = $game_party.members
  .select { |member| member.alive? }
  .min_by { |member| member.hp }
  .index
enemy.force_action(3, target_id)
Yeah, confirmed that you'll need the select to remove dead members. If not, it'll try to target the dead one and then smooth_target will move on to the next available, but not necessarily the one with the lowest HP.
@Trihan and @CaRa_CrAzY It took some minutes to get an actor killed off so I could test this, but it seems to work as intended even with 2 dead actors, so I think we're secure on this one now.
Did you try it with 3 actors where the first one in the list has the lowest HP to start, then the next lowest is actor #3? They should start targeting #2 when the first one dies instead of the weakest one.
The short answer is, no I didn't, not like that.

I just used my current battle party (5 members)
I used F6 to reduce all actors' HP to 1 before anything else.
I then had each actor guard until it was the enemy's turn to act. My Guard skill restores a small percentage of HP, so by the time the enemy acted 4 had guarded and recieved variable amounts of HP.
The enemy went for the actor that still only had 1 HP
Repeat, and when it got to the enemy's next turn it again went for the lowest and defeated him as his HP was still pretty low.
Repeat, and then the enemy went for the lowest, but that actor had enough HP by this time to survive the hit.
Just to add
Code:
$game_party.members .select { |member| member.alive? }
This is redundant. You could use this instead 😛
Code:
$game_party.alive_members

Original thread: https://forums.rpgmakerweb.com/threads/identifying-the-weakest-actor-in-battle.124396/