[SOLVED] Displaying hit rate when targeting an enemy.

Joined
Oct 26, 2018
Messages
21
Reaction score
3
First Language
English (AUS)
Primarily Uses
RMXP
I want to make it so that when you pick an attack method and you're about to pick the enemy you want to attack, you get to see what your chances are of hitting them. So far, I've worked something out for 1v1 battles. However, I know that with multiple enemies or actors, it's going to be problematic:

1595899915490.png

So far I have not been able to incorporate the skill's hitrate into this, only the actor's normal hitrate. Here is my code for
Ruby:
#==============================================================================
# ** Arrow_Enemy
#------------------------------------------------------------------------------
#  This arrow cursor is used to choose enemies. This class inherits from the
#  Arrow_Base class.
#==============================================================================

class Arrow_Enemy < Arrow_Base
  #--------------------------------------------------------------------------
  # * Get Enemy Indicated by Cursor, actor performing action, and the
  #   competence being used in battle.
  #--------------------------------------------------------------------------
  def enemy
    return $game_troop.enemies[@index]
  end
  def actor
    return $game_party.actors[@index]
  end
  def skill
    return 0
  end
  #---------------------------------------------------------------------------
  # * Frame Update
  #---------------------------------------------------------------------------
  def update
    super
    # Skip if indicating a nonexistant enemy
    $game_troop.enemies.size.times do
      break if self.enemy.exist?
      @index += 1
      @index %= $game_troop.enemies.size
    end
    # Cursor right
    if Input.repeat?(Input::RIGHT)
      $game_system.se_play($data_system.cursor_se)
      $game_troop.enemies.size.times do
        @index += 1
        @index %= $game_troop.enemies.size
        break if self.enemy.exist?
      end
    end
    # Cursor left
    if Input.repeat?(Input::LEFT)
      $game_system.se_play($data_system.cursor_se)
      $game_troop.enemies.size.times do
        @index += $game_troop.enemies.size - 1
        @index %= $game_troop.enemies.size
        break if self.enemy.exist?
      end
    end
    # Set sprite coordinates
    if self.enemy != nil
      self.x = self.enemy.screen_x
      self.y = self.enemy.screen_y
    end
  end
  #--------------------------------------------------------------------------
  # * Help Text Update
  #--------------------------------------------------------------------------
  def update_help
    # Display enemy name and states in the help window
    @help_window.set_enemy(self.enemy, self.actor, self.skill)
  end
end
Ruby:
#==============================================================================
# ** Window_Help
#------------------------------------------------------------------------------
#  This window shows skill and item explanations along with actor status.
#==============================================================================

class Window_Help < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  #--------------------------------------------------------------------------
  # * Set Text
  #  text  : text string displayed in window
  #  align : alignment (0..flush left, 1..center, 2..flush right)
  #--------------------------------------------------------------------------
  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 0, self.width - 40, 32, text, align)
      @text = text
      @align = align
      @actor = nil
    end
    self.visible = true
  end
  #--------------------------------------------------------------------------
  # * Set Actor
  #     actor : status displaying actor
  #--------------------------------------------------------------------------
  def set_actor(actor)
    if actor != @actor
      self.contents.clear
      draw_actor_name(actor, 4, 0)
      draw_actor_state(actor, 140, 0)
      draw_actor_hp(actor, 284, 0)
      draw_actor_sp(actor, 460, 0)
      @actor = actor
      @text = nil
      self.visible = true
    end
  end
  #--------------------------------------------------------------------------
  # * Set Enemy
  #     enemy : name and status displaying enemy, as well as success rate of
  #     action
  #--------------------------------------------------------------------------
  def set_enemy(enemy, actor, skill)
    text = ""
    state_text = make_battler_state_text(enemy, 112, false)
    if state_text != ""
      text += state_text
    else
      text += "[Normal]"
    end
    #get hit rate
    hit = actor.hit
    avoid = enemy.eva
    skillhit = ConquestExeCompetence::COMPETENCES[skill][:hitbonus]
    hitrate = (hit - avoid)
    #if hitrate > 100
    #  hitrate = 100
    #end
    hitrate = hitrate.to_s
    text += " (Hit: " + hitrate + "%)"
    set_text(text, 1)
  end
end

I need a better way to get the IDs for the actor and the skill or item they may be using. Any help on how to do this?
EDIT: This is solved! Thanks for your help, @MobiusXVI
 
Last edited:

MobiusXVI

Game Maker
Veteran
Joined
Mar 20, 2013
Messages
374
Reaction score
89
First Language
English
Primarily Uses
Yeah, you're really close. You can try setting the actor whenever enemy selection starts by doing this:
1595949092252.png
Note that you'll need to change your definition of "actor" in Arrow_Enemy to be a property so that you can set it. Once you have access to the current actor, then I'd recommend taking a look at their property called "current_action" in the "Game_Battler" class. That will tell you whether they are attacking normally, using a skill, or using an item.
 
Joined
Oct 26, 2018
Messages
21
Reaction score
3
First Language
English (AUS)
Primarily Uses
RMXP
I've added the thing you told me to add, but I'm not sure how to make the actor a property in Arrow_Enemy.

EDIT: That line doesn't seem to have any effect on the Arrow_Enemy, despite seemingly creating a property in Arrow_Enemy. I can't seem to reference that line... or maybe I'm doing it wrong.
 
Last edited:

MobiusXVI

Game Maker
Veteran
Joined
Mar 20, 2013
Messages
374
Reaction score
89
First Language
English
Primarily Uses
Here's what I've got working:
Ruby:
#==============================================================================
# ** Arrow_Enemy
#------------------------------------------------------------------------------
#  This arrow cursor is used to choose enemies. This class inherits from the
#  Arrow_Base class.
#==============================================================================

class Arrow_Enemy < Arrow_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor   :actor                    # actor
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     viewport : viewport
  #--------------------------------------------------------------------------
  def initialize(viewport)
    super(viewport)
    @actor = nil
  end
  #--------------------------------------------------------------------------
  # * Help Text Update
  #--------------------------------------------------------------------------
  def update_help
    # Display enemy name and states in the help window
    @help_window.set_enemy(self.enemy, self.actor)
  end
end

#==============================================================================
# ** Window_Help
#------------------------------------------------------------------------------
#  This window shows skill and item explanations along with actor status.
#==============================================================================

class Window_Help < Window_Base
  #--------------------------------------------------------------------------
  # * Set Enemy
  #     enemy : name and status displaying enemy
  #--------------------------------------------------------------------------
  def set_enemy(enemy, actor)
    text = ""
    state_text = make_battler_state_text(enemy, 112, false)
    if state_text != ""
      text += state_text
    else
      text += "[Normal]"
    end
    # Check for actor
    if actor == nil
      set_text(text, 1)
      return
    end
    #get hit rate
    hit = actor.hit
    avoid = enemy.eva
    #skillhit = ConquestExeCompetence::COMPETENCES[skill][:hitbonus]
    hitrate = (hit - avoid)
    #if hitrate > 100
    #  hitrate = 100
    #end
    hitrate = hitrate.to_s
    text += " (Hit: " + hitrate + "%)"
    set_text(text, 1)
  end
end

#==============================================================================
# ** Scene_Battle (part 3)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  alias mobius_hit_chance_start_enemy_select start_enemy_select
  #--------------------------------------------------------------------------
  # * Start Enemy Selection
  #--------------------------------------------------------------------------
  def start_enemy_select
    mobius_hit_chance_start_enemy_select
    @enemy_arrow.actor = @active_battler
  end
end

If you paste that into your scripts, you should be able to play around with the "set_enemy" method to get what you want out of the actor.
 
Joined
Oct 26, 2018
Messages
21
Reaction score
3
First Language
English (AUS)
Primarily Uses
RMXP
Oh right, I just realised that I used @active.battler instead of @active_battler. I also moved the @enemy_arrow.actor = @active_battler up before the @enemy_arrow.help_window = @help_window line. Now it works. I did add the things you told me to add to Arrow_Enemy though. Going without it would cause an error.

The only thing now is taking the battle action into account. I've started out with the following in the Arrow_Enemy class:
Ruby:
  def skill
    return actor.current_action
  end
 
Last edited:

MobiusXVI

Game Maker
Veteran
Joined
Mar 20, 2013
Messages
374
Reaction score
89
First Language
English
Primarily Uses
Yeah, so take a look at the "Game_BattleAction" class, which is what the "current_action" is. First, you need to check the "kind" of action that it is where:
kind=0 is "basic" (more on that later)
kind=1 is a skill
kind=2 is using an item

If it's a basic action, then you need to check the "basic" property where:
basic=0 is an attack
basic=1 is guarding
basic=2 is trying to escape
basic=3 is doing nothing (only used if like paralyzed)

Once you know what the action is then you can use "skill_id" or "item_id" to determine those.
 
Joined
Oct 26, 2018
Messages
21
Reaction score
3
First Language
English (AUS)
Primarily Uses
RMXP
I've come up with a working code now!

Ruby:
#==============================================================================
# ** Arrow_Enemy
#------------------------------------------------------------------------------
#  This arrow cursor is used to choose enemies. This class inherits from the
#  Arrow_Base class.
#==============================================================================

class Arrow_Enemy < Arrow_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor   :actor                    # actor
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     viewport : viewport
  #--------------------------------------------------------------------------
  def initialize(viewport)
    super(viewport)
    @actor = nil
  end
  #--------------------------------------------------------------------------
  # * Get Enemy Indicated by Cursor and the competence/item being used
  #   against them.
  #--------------------------------------------------------------------------
  def enemy
    return $game_troop.enemies[@index]
  end
  def action
    base = actor.current_action
    if base.kind == 0
      if base.basic == 0
        return ["Attack"]
      else
        return ["No Attack"]
      end
    elsif base.kind == 1
      n = ["Competence"]
      skillid = base.skill_id
      n.push(skillid)
      return n
    else
      n = ["Item"]
      itemid = base.item_id
      n.push(skillid)
      return n
    end
  end
  #---------------------------------------------------------------------------
  # * Frame Update
  #---------------------------------------------------------------------------
  def update
    super
    # Skip if indicating a nonexistant enemy
    $game_troop.enemies.size.times do
      break if self.enemy.exist?
      @index += 1
      @index %= $game_troop.enemies.size
    end
    # Cursor right
    if Input.repeat?(Input::RIGHT)
      $game_system.se_play($data_system.cursor_se)
      $game_troop.enemies.size.times do
        @index += 1
        @index %= $game_troop.enemies.size
        break if self.enemy.exist?
      end
    end
    # Cursor left
    if Input.repeat?(Input::LEFT)
      $game_system.se_play($data_system.cursor_se)
      $game_troop.enemies.size.times do
        @index += $game_troop.enemies.size - 1
        @index %= $game_troop.enemies.size
        break if self.enemy.exist?
      end
    end
    # Set sprite coordinates
    if self.enemy != nil
      self.x = self.enemy.screen_x
      self.y = self.enemy.screen_y
    end
  end
  #--------------------------------------------------------------------------
  # * Help Text Update
  #--------------------------------------------------------------------------
  def update_help
    # Display enemy name and states in the help window
    @help_window.set_enemy(self.enemy, self.actor, self.action)
  end
end
Ruby:
#==============================================================================
# ** Window_Help
#------------------------------------------------------------------------------
#  This window shows skill and item explanations along with actor status.
#==============================================================================

class Window_Help < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  #--------------------------------------------------------------------------
  # * Set Text
  #  text  : text string displayed in window
  #  align : alignment (0..flush left, 1..center, 2..flush right)
  #--------------------------------------------------------------------------
  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 0, self.width - 40, 32, text, align)
      @text = text
      @align = align
      @actor = nil
    end
    self.visible = true
  end
  #--------------------------------------------------------------------------
  # * Set Actor
  #     actor : status displaying actor
  #--------------------------------------------------------------------------
  def set_actor(actor)
    if actor != @actor
      self.contents.clear
      draw_actor_name(actor, 4, 0)
      draw_actor_state(actor, 140, 0)
      draw_actor_hp(actor, 284, 0)
      draw_actor_sp(actor, 460, 0)
      @actor = actor
      @text = nil
      self.visible = true
    end
  end
  #--------------------------------------------------------------------------
  # * Set Enemy
  #     enemy : name and status displaying enemy, as well as success rate of
  #     action
  #--------------------------------------------------------------------------
  def set_enemy(enemy, actor, action)
    text = ""
    state_text = make_battler_state_text(enemy, 112, false)
    if state_text != ""
      text += state_text
    else
      text += "[Normal]"
    end
    # Check for actor
    if actor == nil
      set_text(text, 1)
      return
    end
    #get hit rate
    hit = actor.hit
    avoid = enemy.eva
    if action.include?("Competence")
      skillid = action[1]
      hit += ConquestExeCompetence::COMPETENCES[skillid][:hitbonus]
    end
    if action.include?("Item")
      itemid = action[1]
      hit += ConquestExeItems::ITEMS[itemid][:hitbonus]
    end
    hitrate = (hit - avoid)
    #if hitrate > 100
    #  hitrate = 100
    #end
    hitrate = hitrate.to_s
    text += " (Hit: " + hitrate + "%)"
    set_text(text, 1)
  end
end
 

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

Latest Threads

Latest Profile Posts

Yaaay! My second public script is out and approved! A VX Ace Final Fantasy IX styled Throw Script!

I look forward to all the constructive feedback I get but most importantly I hope everyone who ends up using it enjoys!

Yaaaay! Making scripts is fun lol. :D
When you're lost out therrrrre and you're alllll alone, ahwahwah waitin, to carry you hooooommmme, uhhheverywhere you look! (someone hit me with those backing vocals)
X.X cant focus... too many thing want do... help!
Day 2 of teaching MV, student can move cat across the map.
Got inspired and started writing a simple tower defence battle system last night :o

Forum statistics

Threads
100,525
Messages
976,803
Members
132,080
Latest member
nwr
Top