HP bar showing up every time player loses health (Not for the Default Battle System).

Joined
Oct 9, 2018
Messages
305
Reaction score
178
First Language
Greek
Primarily Uses
RMVXA
Hello!
I was looking for a way or a script that when the player loses health, a small health bar would show up above their head (for a few moments and then disappear maybe?). I am not talking about the default battle system..
More like a HUD system..
 

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
4,255
Reaction score
1,157
First Language
English
Primarily Uses
RMVXA
Ruby:
# ╔═════════════════════════════════════╦════════════════════╗
# ║ Title: Actor HP on Map              ║  Version: 1.04     ║
# ║ Author: Roninator2                  ║                    ║
# ╠═════════════════════════════════════╬════════════════════╣
# ║ Function:                           ║   Date Created     ║
# ║ Trimmed down version                ╠════════════════════╣
# ║ Show actor hp on the map            ║    10 Oct 2021     ║
# ╚═════════════════════════════════════╩════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Allows to display actor hp on screen when damaged        ║
# ╚══════════════════════════════════════════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Instructions:                                            ║
# ║   Adjust the settings to your preference                 ║
# ╚══════════════════════════════════════════════════════════╝
# ╔═════════════════════════════════════╗
# ║ Terms of use:                       ║
# ║ Free for all uses in RPG Maker      ║
# ╚═════════════════════════════════════╝
# Not compatible with Vlue Sleek gauges

module R2_MAP_HUD
  # Configure options
  Hud_Width     = 120     # Hud Width
  Hud_Height    = 40     # Hud Height
 
  HP_Width      = 95     # HP bar width , # must be 25 less than hud window
  HP_Height     = 10      # HP bar heigth
 
  HP_Color1     = 22      # Set the HP color
  HP_Color2     = 26      # Set the HP color
 
  Window_Opacity      = 0         # window visibility, does not affect contents
  Window_Back_Opacity = 0         # Window background opacity
  Window_Graphic      = "Window"  # Window Graphic file to use
 
  Toggle        = :ALT  # Button to call the HP bar
    
  Time_Held     = 60 # length of time (in frames) to show the HP bar
  
  SWITCH        = 5 # Switch to turn script off.
end
# ╔══════════════════════════════════════════════════════════╗
# ║ End of Configuration                                     ║
# ╚══════════════════════════════════════════════════════════╝

class Actor_Hud_Window < Window_Base
  def initialize(id)
    x = $game_player.screen_x - (R2_MAP_HUD::Hud_Width / 2)
    y = $game_player.screen_y - (R2_MAP_HUD::Hud_Height * 2)
    w = R2_MAP_HUD::Hud_Width
    h = R2_MAP_HUD::Hud_Height
    super(x,y,w,h)
    self.windowskin = Cache.system(R2_MAP_HUD::Window_Graphic)
    self.opacity = R2_MAP_HUD::Window_Opacity
    self.back_opacity = R2_MAP_HUD::Window_Back_Opacity
    self.contents_opacity = 255
    @current_actor = id
    refresh
  end
 
  def actor_data
    @actor = $game_actors[@current_actor]
    width = R2_MAP_HUD::HP_Width
    height = R2_MAP_HUD::HP_Height
    draw_actor_hud_hp(@actor, 0, 0, width, height)
    @current_hp = @actor.hp
  end
 
  def draw_actor_hud_hp(actor, x, y, width, height)
    color1 = text_color(R2_MAP_HUD::HP_Color1)
    color2 = text_color(R2_MAP_HUD::HP_Color2)
    draw_gauge(x, y, width, actor.hp_rate, color1, color2)
  end
 
  def draw_gauge(x, y, width, rate, color1, color2)
    fill_w = (width * rate).to_i
    gauge_y = y + line_height - 28
    height = R2_MAP_HUD::HP_Height
    contents.fill_rect(x, gauge_y, width, height, gauge_back_color)
    contents.gradient_fill_rect(x, gauge_y, fill_w, height, color1, color2)
  end

  def refresh
    self.contents.clear
    actor_data
  end
 
  def hud_data_changed
    return true if @current_hp != @actor.hp
    return false
  end
 
  alias r2_hud_update_vlue    update
  def update
    refresh if hud_data_changed
    r2_hud_update_vlue
  end
 
end

class Scene_Map < Scene_Base
  alias r2_map_hud_start  start
  def start
    @map_hud_seen = false
    start_hud
    r2_map_hud_start
  end
  def start_hud
    @actor_shown = $game_party.leader
    @map_hud = Actor_Hud_Window.new(@actor_shown.id)
    @map_hud.hide
  end
  alias r2_map_update_switch_actor    update
  def update
    r2_map_update_switch_actor
    return if $game_switches[R2_MAP_HUD::SWITCH]
    if @map_hud.visible
      @map_hud.x = $game_player.screen_x - (R2_MAP_HUD::Hud_Width / 2)
      @map_hud.y = $game_player.screen_y - (R2_MAP_HUD::Hud_Height * 2)
    end
    if Input.trigger?(R2_MAP_HUD::Toggle)
      @map_hud_seen = !@map_hud_seen
      set_timer
    end
    if @map_hud.hud_data_changed
      hud_update
      @map_hud_seen = true
      set_timer
    end
    if @timer == Graphics.frame_count
      @map_hud_seen = false
    end
    if @map_hud_seen == false
      @map_hud.hide if @map_hud.visible
    else
      @map_hud.show
    end
  end
  def set_timer
    @map_hud.x = $game_player.screen_x - (R2_MAP_HUD::Hud_Width / 2)
    @map_hud.y = $game_player.screen_y - (R2_MAP_HUD::Hud_Height * 2)
    @timer = Graphics.frame_count
    @timer += R2_MAP_HUD::Time_Held
  end
  def hud_update
    return if @actor_shown.nil?
    @map_hud.refresh
  end
end
 
Last edited:
Joined
Oct 9, 2018
Messages
305
Reaction score
178
First Language
Greek
Primarily Uses
RMVXA
MY SAVIOR RONINATOR2 IS BACK SAVING (ONCE AGAIN) THE SITUATION!!
Thank you very very much!!
Also, You weren't online for a few days i kinda woried..

The script is working just fine! (it's not nesesary but if you can set it) can the hp bar "follow" the player? it does appear and dissapear just fine with nice timing but when i move the player it stays behind.
 
Last edited:

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
4,255
Reaction score
1,157
First Language
English
Primarily Uses
RMVXA
Script updated.

I had a bad storm in my area. It snapped over 180 power poles. Took 6 days to get power back.
And another 3 to get internet.
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
492
Reaction score
206
First Language
English
Primarily Uses
RMXP
Friendly Suggestions:

Ruby:
  def hud_data_changed() @current_hp != @actor.hp end
# several lines of code here
  @map_hud_seen = false if @timer == Graphics.frame_count

I gotta say both lines are totally optional but the first one does improve @Roninator2 's code.

One thing I didn't understand, Ron, was that you used Graphics.frame_count as @timer 's basic value and then added 60 on a separate line. That would mean it'd just show up for a second if Graphics.frame_rate is set to 60. You could let TRG set that value at will by setting just another custom CONSTANT inside the new module you had provided above.
 

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
4,255
Reaction score
1,157
First Language
English
Primarily Uses
RMVXA
One thing I didn't understand, Ron, was that you used Graphics.frame_count as @timer 's basic value and then added 60 on a separate line. That would mean it'd just show up for a second if Graphics.frame_rate is set to 60. You could let TRG set that value at will by setting just another custom CONSTANT inside the new module you had provided above.
It's myself not fully understanding the system.
In my game if I did Graphics.frame_count I get a seconds ( at least it looks that way) and when testing the output It looked like I got just that, So I added 6 originally and it just blinked on the screen.
Then I changed to 60 and it stayed for a few seconds. Then I just left it.
But you right that it should be a value the player can change easily from the module.
I'll add that.
 
Joined
Oct 9, 2018
Messages
305
Reaction score
178
First Language
Greek
Primarily Uses
RMVXA
Hello! if it is easy for you, can you add a on/off switch option? Because the bar appears when i am doing different things sometimes and maybe by adding a switch i can manage to balance this.
 
Joined
Oct 9, 2018
Messages
305
Reaction score
178
First Language
Greek
Primarily Uses
RMVXA
Awesome thank you!
The bar still shows up, i think i know how.. when i select things from the items it happens..
i am mentioning it because you may know why..
Anyway, it doesn't bother me very much! I can still work with it!
 

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
4,255
Reaction score
1,157
First Language
English
Primarily Uses
RMVXA
Ah I see, it shows up when exiting the menu.
Let me see what I can do.

*update, had to change .open to .show. should work for you now.
 
Last edited:

Latest Threads

Latest Profile Posts

I'm having a surprisingly easy time with the wait for Hogwarts Legacy, though I know other gamers are salivating hard.
:awat:What are you two down there doing?
This status is here just to take up space. And to make you read that.
Commenting just for the sake of commenting. :biggrin:
Just... two more to go...
dylan kickstaeter.png

Forum statistics

Threads
124,357
Messages
1,162,989
Members
163,148
Latest member
MagneBP666
Top