I can see many ways to do this.
- Using a troop event to force an action at the end of each turn.
If you do it this way, you have to use a script call and store the target into a variable
- Targeting all enemies at once and calculating the right target using the damage formula.
When doing it this way, you have to keep in mind that, as any other skill that relies on the damage formula, the formula is only applied when the skill hits. Be sure to set it in the right way.
That said, when we talk about efficiency, we usually consider efficiency related to the time spent to perform a certain action. However, it is also possible to consider efficiency based on the time spent to write the code itself. In this case, there is no big difference in terms of time spent coding the action itself, however, there is a big difference when it comes to how the action is performed and how much the engine need to perform that action.
The code to store your target is always the same:
Code:
$game_variables[var_id_for_hp_rate] = 1
$game_variables[var_id_for_target] = 0
$game_switches[switch_id_for_flag] = false
$game_troop.members.each.with_index do |e, i|
next unless e.alive?
if (e.hp_rate < $game_variables[var_id_for_hp_rate])
$game_variables[var_id_for_hp_rate]
$game_variables[var_id_for_target] = i
$game_switches[switch_id_for_flag] = true
end
end
This allows you to force an action based on the target id (stored in $game_variables[var_id_for_target]), to access the target using $game_troop.members[$game_variables[var_id_for_target]] and only cast the skill if a valid target was found ($game_switches[switch_id_for_flag] is true). When the switch is false, enemies are dead or they did not take damage at all. If the former is true, unless you are using a script to prevent that, BattleManager.judge_win_loss returns true and the battle ends (so the event is not going to be executed and you can discard this option at all, it is not worth considering), if the latter is true, you can simply force the action against a random target.