Szibes

Villager
Member
Joined
Mar 5, 2021
Messages
7
Reaction score
0
First Language
Polish
Primarily Uses
RMVXA
How I can make a hit chance appear somewhere on the screen when choosing an enemy to target, preferably next to their name.

Tsukihime has script, Target Hit Rate Info, that does something similar, but it's an add-on to yanfly script which displays info after pressing a button together with information that I'd want to keep away from player.
Or whole lotta of negative space, which is something even worse.
 

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
3,135
Reaction score
709
First Language
English
Primarily Uses
RMVXA
Some code replicated from Vlue Enemy HP Bars and Tsukihime Hit Rate Info
Ruby:
# ╔═════════════════════════════════════╦════════════════════╗
# ║ Title: Message for Hit Chance       ║  Version: 1.00     ║
# ║ Author: Roninator2                  ║                    ║
# ╠═════════════════════════════════════╬════════════════════╣
# ║ Function:                           ║   Date Created     ║
# ║                                     ╠════════════════════╣
# ║ Show hit chance percentage          ║    12 May 2021     ║
# ╚═════════════════════════════════════╩════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Change the message below to what you want displayed      ║
# ║ When a battler is selected                               ║
# ╚══════════════════════════════════════════════════════════╝
# ╔═════════════════════════════════════╗
# ║ Terms of use:                       ║
# ║ Free for all uses in RPG Maker      ║
# ╚═════════════════════════════════════╝
# ╔═════════════════════════════════════╗
# ║ Credits to:                         ║
# ║ Vlue & Tsukihime                    ║
# ╚═════════════════════════════════════╝
module R2_HIT
  #Whether to place the hit chance text above or below the enemy
  ABOVE_MONSTER = false
  #Text to display next to hit chance number
  HIT_TEXT = "Chance to hit"
  #The width of the text
  TEXT_WIDTH = 150
  #The height of the text
  TEXT_HEIGHT = 20
  #Offset the hit rate along the x-axis(left,right)
  TEXT_OFFSET_X = -50
  #Offset the hit rate along the y-axis(up,down)
  TEXT_OFFSET_Y = 2

  #The width of the text placement make larger if text is cut off
  LOC_WIDTH = 100
  #The height of the text placement
  LOC_HEIGHT = 15

  #Size of the displayed text
  TEXT_SIZE = Font.default_size
  #Font of the displayed text
  TEXT_FONT = Font.default_name

end

class Sprite_Battler
  alias hitrate_update update
  def update
    hitrate_update
    return unless @battler.is_a?(Game_Enemy)
    if @battler
      update_hitrate
    end
  end
  def update_hitrate
    setup_hitrate if @hit_rate.nil?
    if @hit_display.nil?
      @hit_display = Sprite_Base.new(self.viewport)
      @hit_display.bitmap = Bitmap.new(R2_HIT::TEXT_WIDTH,R2_HIT::TEXT_SIZE)
      @hit_display.bitmap.font.size = R2_HIT::TEXT_SIZE
      @hit_display.bitmap.font.name = R2_HIT::TEXT_FONT
      @hit_display.x = @hit_rate.x + R2_HIT::TEXT_OFFSET_X
      @hit_display.y = @hit_rate.y + R2_HIT::TEXT_OFFSET_Y
      @hit_display.z = 111
    end
    determine_text_visible
    return unless @hit_rate.visible
    if @hit_rate.opacity != self.opacity
      @hit_rate.opacity = self.opacity
    end
    @hit_rate.bitmap.clear
    @hit_display.opacity = @hit_rate.opacity if @hit_display.opacity != @hit_rate.opacity
    @hit_display.bitmap.clear
    text = draw_hit_rate(@hit_display.x,@hit_display.y)
    return if text.nil?
    @hit_display.bitmap.draw_text(0,0,R2_HIT::TEXT_WIDTH,@hit_display.height,text)
  end

  def setup_hitrate
    @hit_rate = Sprite_Base.new(self.viewport)
    width = R2_HIT::LOC_WIDTH
    height = R2_HIT::LOC_HEIGHT
    @hit_rate.bitmap = Bitmap.new(width,height)
    @hit_rate.x = self.x + @hit_rate.width / 2 + R2_HIT::TEXT_OFFSET_X
    @hit_rate.y = self.y + R2_HIT::TEXT_OFFSET_Y - self.bitmap.height - @hit_rate.height
    @hit_rate.y = self.y + R2_HIT::TEXT_OFFSET_Y unless R2_HIT::ABOVE_MONSTER
    @hit_rate.x = 0 if @hit_rate.x < 0
    @hit_rate.y = 0 if @hit_rate.y < 0
    @hit_rate.z = 104
  end

  def determine_text_visible
    if !@battler.alive?
      @hit_rate.visible = false
      @hit_display.visible = false
      return if !@battler.alive?
    end
    @hit_rate.visible = true
    return unless SceneManager.scene.is_a?(Scene_Battle)
    return unless SceneManager.scene.enemy_window
    @hit_rate.visible = SceneManager.scene.target_window_index == @battler.index
    @hit_rate.visible = false if !SceneManager.scene.enemy_window.active
    @hit_display.visible = false if !@hit_rate.visible
    @hit_display.visible = true if @hit_rate.visible
  end

  alias hitrate_dispose dispose
  def dispose
    @hit_rate.dispose if @hit_rate
    @hit_display.dispose if @hit_display
    hitrate_dispose
  end

  #-----------------------------------------------------------------------------
  # New
  #-----------------------------------------------------------------------------
  def draw_hit_rate(dx, dy)
    return unless SceneManager.scene_is?(Scene_Battle)
    return unless SceneManager.scene.enemy_window
    reveal(SceneManager.scene.enemy_window.enemy)
    hitdata = (get_hit_rate * 100).to_i
    text = "#{R2_HIT::HIT_TEXT} #{hitdata}%"
    return text
  end

  def reveal(battler)
    return if @battler == battler
    @battler = battler
  end

  def actor; return BattleManager.actor; end

  def get_hit_rate
    hit_rate = @battler.item_hit(actor, actor.current_action.item)
    evade_rate = @battler.item_eva(actor, actor.current_action.item)
    return (hit_rate) * (1 - evade_rate)
  end
end

class Scene_Battle
  attr_reader  :enemy_window
  def target_window_index
    begin
    @enemy_window.enemy.index
    rescue
      return -1
    end
  end
end
 
Last edited:

Szibes

Villager
Member
Joined
Mar 5, 2021
Messages
7
Reaction score
0
First Language
Polish
Primarily Uses
RMVXA
Sweet goodness gracious!
This is not what I wanted!
This is 100% more than I expected, thank you so much!
I tested it and expect for some position adjustment it works perfectly.
If I find any bugs I'll report them.
 

Latest Threads

Latest Posts

Latest Profile Posts

I hate doing voice over videos but people said they like hearing about the details.
Modern Town Overworld, World of Solaria Dungeon, 80's Retro-Futuristic Font | RPG Maker News #73

I been slowing down and not focusing on project currently.
I had this idea for VTubers vs MOTHER and its a bunch of Vtubers finding eight melodies to fight a psychic alien and a fat kid. IDK, kinda find it hard to use RPG Maker for more than 15 minutes
Working on some wizard/magic bases:
TestChara-10-hover.gif

I mean, technically it's a flying base but I haven't made any wings yet, so magic hovering is it for now.

Forum statistics

Threads
112,301
Messages
1,067,192
Members
145,930
Latest member
9Animeanetamak
Top