Enemy flash when hit with weakness

So a feature I quite enjoy in some games is when the enemies visibly react to getting hit by the element they're weak to.

I'm looking for a simple script that has it so an enemy shakes and flashes red if hit by an element weakness (or blue if hit by something they resist).

I don't know how feasible it is, but I'd also like it so I can play an animation on hit.
(Singular bump just to revive the thread because I'm still interested in the function.)
Ruby:
class Game_ActionResult
  attr_accessor :weakness_hit
  attr_accessor :resist_hit
 
  alias tlb_gar_clear_hit_flags clear_hit_flags
  def clear_hit_flags
    tlb_gar_clear_hit_flags
    @weakness_hit = false
    @resist_hit = false
  end
end

class Game_Battler < Game_BattlerBase
  def item_apply(user, item)
    @result.clear
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
        @result.critical = (rand < item_cri(user, item))
        @result.weakness_hit = item_element_rate(user, item) > 1
        @result.resist_hit = item_element_rate(user, item) < 1
        make_damage_value(user, item)
        execute_damage(user)
      end
      item.effects.each {|effect| item_effect_apply(user, item, effect) }
      item_user_effect(user, item)
    end
  end
end

class Game_Enemy < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_enemy_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_enemy_damage
  end
end

class Game_Actor < Game_Battler
  def perform_weakness_effect
  end
 
  def perform_resistance_effect
  end
end

class Sprite_Battler < Sprite_Base
  def start_effect(effect_type)
    @effect_type = effect_type
    case @effect_type
    when :appear
      @effect_duration = 16
      @battler_visible = true
    when :disappear
      @effect_duration = 32
      @battler_visible = false
    when :whiten
      @effect_duration = 16
      @battler_visible = true
    when :blink
      @effect_duration = 20
      @battler_visible = true
    when :weak
      @effect_duration = 40
      @battler_visible = true
    when :resist
      @effect_duration = 40
      @battler_visible = true
    when :collapse
      @effect_duration = 48
      @battler_visible = false
    when :boss_collapse
      @effect_duration = bitmap.height
      @battler_visible = false
    when :instant_collapse
      @effect_duration = 16
      @battler_visible = false
    end
    revert_to_normal
  end
 
  def update_effect
    if @effect_duration > 0
      @effect_duration -= 1
      case @effect_type
      when :whiten
        update_whiten
      when :blink
        update_blink
      when :weak
        update_weak
      when :resist
        update_resist
      when :appear
        update_appear
      when :disappear
        update_disappear
      when :collapse
        update_collapse
      when :boss_collapse
        update_boss_collapse
      when :instant_collapse
        update_instant_collapse
      end
      @effect_type = nil if @effect_duration == 0
    end
  end
 
  def update_weak
    self.color.set(255, 0, 0, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 6 - 2
  end
 
  def update_resist
    self.color.set(0, 0, 255, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 4 - 2
  end
end

class Window_BattleLog < Window_Selectable
  def display_hp_damage(target, item)
    return if target.result.hp_damage == 0 && item && !item.damage.to_hp?
    if target.result.weakness_hit
      target.perform_weakness_effect
    elsif target.result.resist_hit
      target.perform_resistance_effect
    elsif target.result.hp_damage > 0 && target.result.hp_drain == 0
      target.perform_damage_effect
    end
    Sound.play_recovery if target.result.hp_damage < 0
    add_text(target.result.hp_damage_text)
    wait
  end
end
Thanks so much for responding, @Trihan! Unfortunately I caught a quite... embarassing bug:

f3BPzWo.png


It seems like the script doesn't play nice with Ace Battle Engine's core script, since the numbers and pop-ups refused to go away when the turn ended. Aside from that, the script you made is perfectly usable. :LZSexcite:
the numbers and pop-ups refused to go away
The script by Trihan needs to be above ace battle engine.
The script by Trihan needs to be above ace battle engine.
I did that, but unfortunately the script itself seems to not work when it's above Ace Battle Engine. 🙁

Oh well, it was worth a shot. 🙂
I can probably work out a compatibility fix, but frankly it's gonna be low on my list of priorities for today because boy howdy do I have a lot of items on my docket. XD
@RCXDan @Trihan
How about just aliasing the update effect method. Seems to work in my tests.
Ruby:
class Game_ActionResult
  attr_accessor :weakness_hit
  attr_accessor :resist_hit
 
  alias tlb_gar_clear_hit_flags clear_hit_flags
  def clear_hit_flags
    tlb_gar_clear_hit_flags
    @weakness_hit = false
    @resist_hit = false
  end
end

class Game_Battler < Game_BattlerBase
  def item_apply(user, item)
    @result.clear
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
        @result.critical = (rand < item_cri(user, item))
        @result.weakness_hit = item_element_rate(user, item) > 1
        @result.resist_hit = item_element_rate(user, item) < 1
        make_damage_value(user, item)
        execute_damage(user)
      end
      item.effects.each {|effect| item_effect_apply(user, item, effect) }
      item_user_effect(user, item)
    end
  end
end

class Game_Enemy < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_enemy_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_enemy_damage
  end
end

class Game_Actor < Game_Battler
  def perform_weakness_effect
  end
 
  def perform_resistance_effect
  end
end

class Sprite_Battler < Sprite_Base
  def start_effect(effect_type)
    @effect_type = effect_type
    case @effect_type
    when :appear
      @effect_duration = 16
      @battler_visible = true
    when :disappear
      @effect_duration = 32
      @battler_visible = false
    when :whiten
      @effect_duration = 16
      @battler_visible = true
    when :blink
      @effect_duration = 20
      @battler_visible = true
    when :weak
      @effect_duration = 40
      @battler_visible = true
    when :resist
      @effect_duration = 40
      @battler_visible = true
    when :collapse
      @effect_duration = 48
      @battler_visible = false
    when :boss_collapse
      @effect_duration = bitmap.height
      @battler_visible = false
    when :instant_collapse
      @effect_duration = 16
      @battler_visible = false
    end
    revert_to_normal
  end
 
  alias sprite_battler_update_effect_trihan update_effect
  def update_effect
    sprite_battler_update_effect_trihan
    case @effect_type
    when :weak
      update_weak
    when :resist
      update_resist
    end
  end
 
  def update_weak
    self.color.set(255, 0, 0, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 6 - 2
  end
 
  def update_resist
    self.color.set(0, 0, 255, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 4 - 2
  end
end

class Window_BattleLog < Window_Selectable
  def display_hp_damage(target, item)
    return if target.result.hp_damage == 0 && item && !item.damage.to_hp?
    if target.result.weakness_hit
      target.perform_weakness_effect
    elsif target.result.resist_hit
      target.perform_resistance_effect
    elsif target.result.hp_damage > 0 && target.result.hp_drain == 0
      target.perform_damage_effect
    end
    Sound.play_recovery if target.result.hp_damage < 0
    add_text(target.result.hp_damage_text)
    wait
  end
end
Yeah, that works. I didn't bother with aliases originally since it was a quick snippet.
@Trihan @Roninator2
Both of you, thank you so much! It works now! 😂

Edit: Uh-oh. It doesn't seem to play sound effects when you yourself get hit with a weakness or resisted attack... or show the text that an attack missed.
show the text that an attack missed.
It showed the miss for me in my test.
I added the sound play for the effects. and added a return value for a bug if the actor sprite is not a bitmap.
Ruby:
class Game_ActionResult
  attr_accessor :weakness_hit
  attr_accessor :resist_hit
 
  alias tlb_gar_clear_hit_flags clear_hit_flags
  def clear_hit_flags
    tlb_gar_clear_hit_flags
    @weakness_hit = false
    @resist_hit = false
  end
end

class Game_Battler < Game_BattlerBase
  def item_apply(user, item)
    @result.clear
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
        @result.critical = (rand < item_cri(user, item))
        @result.weakness_hit = item_element_rate(user, item) > 1
        @result.resist_hit = item_element_rate(user, item) < 1
        make_damage_value(user, item)
        execute_damage(user)
      end
      item.effects.each {|effect| item_effect_apply(user, item, effect) }
      item_user_effect(user, item)
    end
  end
end

class Game_Enemy < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_enemy_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_enemy_damage
  end
end

class Game_Actor < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_actor_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_actor_damage
  end
end

class Sprite_Battler < Sprite_Base
  def start_effect(effect_type)
    @effect_type = effect_type
    case @effect_type
    when :appear
      @effect_duration = 16
      @battler_visible = true
    when :disappear
      @effect_duration = 32
      @battler_visible = false
    when :whiten
      @effect_duration = 16
      @battler_visible = true
    when :blink
      @effect_duration = 20
      @battler_visible = true
    when :weak
      @effect_duration = 40
      @battler_visible = true
    when :resist
      @effect_duration = 40
      @battler_visible = true
    when :collapse
      @effect_duration = 48
      @battler_visible = false
    when :boss_collapse
      @effect_duration = bitmap.height
      @battler_visible = false
    when :instant_collapse
      @effect_duration = 16
      @battler_visible = false
    end
    revert_to_normal
  end
 
  alias sprite_battler_update_effect_trihan update_effect
  def update_effect
    sprite_battler_update_effect_trihan
    case @effect_type
    when :weak
      update_weak
    when :resist
      update_resist
    end
  end
 
  def update_weak
    return if bitmap.nil?
    self.color.set(255, 0, 0, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 6 - 2
  end
 
  def update_resist
    return if bitmap.nil?
    self.color.set(0, 0, 255, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 4 - 2
  end
end

class Window_BattleLog < Window_Selectable
  def display_hp_damage(target, item)
    return if target.result.hp_damage == 0 && item && !item.damage.to_hp?
    if target.result.weakness_hit
      target.perform_weakness_effect
    elsif target.result.resist_hit
      target.perform_resistance_effect
    elsif target.result.hp_damage > 0 && target.result.hp_drain == 0
      target.perform_damage_effect
    end
    Sound.play_recovery if target.result.hp_damage < 0
    add_text(target.result.hp_damage_text)
    wait
  end
end
It showed the miss for me in my test.
I added the sound play for the effects. and added a return value for a bug if the actor sprite is not a bitmap.
Ruby:
class Game_ActionResult
  attr_accessor :weakness_hit
  attr_accessor :resist_hit
 
  alias tlb_gar_clear_hit_flags clear_hit_flags
  def clear_hit_flags
    tlb_gar_clear_hit_flags
    @weakness_hit = false
    @resist_hit = false
  end
end

class Game_Battler < Game_BattlerBase
  def item_apply(user, item)
    @result.clear
    @result.used = item_test(user, item)
    @result.missed = (@result.used && rand >= item_hit(user, item))
    @result.evaded = ([email protected] && rand < item_eva(user, item))
    if @result.hit?
      unless item.damage.none?
        @result.critical = (rand < item_cri(user, item))
        @result.weakness_hit = item_element_rate(user, item) > 1
        @result.resist_hit = item_element_rate(user, item) < 1
        make_damage_value(user, item)
        execute_damage(user)
      end
      item.effects.each {|effect| item_effect_apply(user, item, effect) }
      item_user_effect(user, item)
    end
  end
end

class Game_Enemy < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_enemy_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_enemy_damage
  end
end

class Game_Actor < Game_Battler
  def perform_weakness_effect
    @sprite_effect_type = :weak
    Sound.play_actor_damage
  end
 
  def perform_resistance_effect
    @sprite_effect_type = :resist
    Sound.play_actor_damage
  end
end

class Sprite_Battler < Sprite_Base
  def start_effect(effect_type)
    @effect_type = effect_type
    case @effect_type
    when :appear
      @effect_duration = 16
      @battler_visible = true
    when :disappear
      @effect_duration = 32
      @battler_visible = false
    when :whiten
      @effect_duration = 16
      @battler_visible = true
    when :blink
      @effect_duration = 20
      @battler_visible = true
    when :weak
      @effect_duration = 40
      @battler_visible = true
    when :resist
      @effect_duration = 40
      @battler_visible = true
    when :collapse
      @effect_duration = 48
      @battler_visible = false
    when :boss_collapse
      @effect_duration = bitmap.height
      @battler_visible = false
    when :instant_collapse
      @effect_duration = 16
      @battler_visible = false
    end
    revert_to_normal
  end
 
  alias sprite_battler_update_effect_trihan update_effect
  def update_effect
    sprite_battler_update_effect_trihan
    case @effect_type
    when :weak
      update_weak
    when :resist
      update_resist
    end
  end
 
  def update_weak
    return if bitmap.nil?
    self.color.set(255, 0, 0, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 6 - 2
  end
 
  def update_resist
    return if bitmap.nil?
    self.color.set(0, 0, 255, 0)
    self.color.alpha = 128 - (16 - @effect_duration) * 10
    self.ox = bitmap.width / 2 + @effect_duration % 2 * 4 - 2
  end
end

class Window_BattleLog < Window_Selectable
  def display_hp_damage(target, item)
    return if target.result.hp_damage == 0 && item && !item.damage.to_hp?
    if target.result.weakness_hit
      target.perform_weakness_effect
    elsif target.result.resist_hit
      target.perform_resistance_effect
    elsif target.result.hp_damage > 0 && target.result.hp_drain == 0
      target.perform_damage_effect
    end
    Sound.play_recovery if target.result.hp_damage < 0
    add_text(target.result.hp_damage_text)
    wait
  end
end
I actually still had the "miss" pop-up not showing up, and that was bothering me a lot.

So I instead chose to go crazy: I chopped up Yanfly Ace Engine and @Trihan's script into two halves each, placed them like this:

* Yanfly Ace Battle Engine (1/2)
* Trihan's Script (1/2)
* Yanfly Ace Battle Engine (2/2)
* Trihan's Script (2/2)

... A couple rounds of testing and a full replay of one of my games with it on later, now I can say it works for sure. Yeah, it's an extremely hacky workaround but now I have what I want. 🙂
Hey, it's not hacky if it does what you need it to. 😛

Original thread: https://forums.rpgmakerweb.com/threads/enemy-flash-when-hit-with-weakness.136363/