Identifying the weakest actor in battle

Status
Not open for further replies.

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
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.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
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).
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
You can condense that down to
Code:
target = $game_party.members.min_by {|member| member.hp }
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@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?
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
Can you screenshot your event mate?
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@Trihan

Here is the skill
1595509780580.png

Here is the common event using your bit of code.
1595509852095.png
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
your_enemy was a placeholder, Kes. You need to replace it with the actual enemy object that's going to be acting.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@Trihan So does enemy object mean "enemy[id]"
(I did remind you that I'm a scripting idiot!)
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
$game_troop.members[id]
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
$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.
 
  • Like
Reactions: Kes

CaRa_CrAzY

Undefined Custom Title
Veteran
Joined
Jan 19, 2019
Messages
65
Reaction score
27
First Language
Portuguese
Primarily Uses
Other
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.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
I think at this point you should just put me on retainer given how much I've helped you over the years. ;)
 

CaRa_CrAzY

Undefined Custom Title
Veteran
Joined
Jan 19, 2019
Messages
65
Reaction score
27
First Language
Portuguese
Primarily Uses
Other
@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. :p
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)
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
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.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
@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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,960
First Language
English
Primarily Uses
RMMV
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.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,712
First Language
English
Primarily Uses
RMVXA
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.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,088
Members
137,585
Latest member
Reversinator
Top