Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Window problems

I have made an error somewhere in this script, but I'm not sure where.

It is probably horribly coded. I've honestly never used the "for i in 1...blah" syntax before.

I wondered if anyone could help me?

Basically, the window shows but no content inside (in other words, the bars for the Hud don't show).

Code:
class Bitmap
  def draw_2pic_gradient_bar(x, y, current, max, background_file, bar_file)
    bitmap = RPG::Cache.picture(background_file)
    self.blt(x, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.picture(bar_file)
    w = Integer(bitmap.width * (current / max.to_f))
    self.blt(x, y, bitmap, Rect.new(0, 0, w, bitmap.height))
  end
end
class Game_Hud < Window_Base
  def initialize
    super(363-16, 232-16, 103+32, 75+32)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.z = 9999
    #self.opacity = 0
    #self.visible = $game_switches[x]
    self.windowskin = RPG::Cache.windowskin('001-blue01')
    draw_group_bars
    #draw_player_bars
    refresh
  end
  def draw_group_bars
    for i in 1...$game_party.actors.size
      x = 36
      y = 8 + i * 15
      p = $game_party.actors[i]
      name = p.name
      self.contents.draw_2pic_gradient_bar(x, y, p.hp, p.hpmax, 'party_hpback', 'party_hp')
      self.contents.font.colour = black
      self.contents.font.size = 12
      self.contents.draw_text(38, 10 + y, width, height, '#{name}')
    end
  end
  #def draw_player_bars
  #end
  def refresh_a
    for i in 1...$game_party.actors.size
      @hp_current = $game_party.actors[i].hp
      refresh_bars if @hp_current != @old_hp
    end
  end
  def refresh
    for i in 1...$game_party.actors.size
      @old_hp = $game_party.actors[i].hp
      refresh_a
    end
  end
end
class Scene_Map
  alias game_hud_main initialize
  alias game_hud_refresh update
  def initialize
    @hud_win = Game_Hud.new
    game_hud_main
  end
  def update
    @hud_win.refresh
    game_hud_refresh
  end
end

What it should show, if all is right, is this:
Mock-up only
http://img.photobucket.com/albums/v108/ ... test_0.png[/IMG]

What it currently shows is this:
http://img.photobucket.com/albums/v108/ ... test_1.png[/IMG]

The graphics:
http://img.photobucket.com/albums/v108/ ... rty_hp.png[/IMG]
http://img.photobucket.com/albums/v108/ ... hpback.png[/IMG]


Notes:

I know, it looks awful at the moment. That's because this is just the coding stages.

Sorry for the poor coding and lack of commenting, it's not for public release and I tend to just make mental notes, except for really important bits...



Notes again:
If you really care, this is what I'm aiming for:

http://img.photobucket.com/albums/v108/dudemaster/swamp2.png[/IMG]




Edit:

While this topic is here.. anyone know how I can change this to make a verticle bar, starting from the bottom going up?
i.e.
_
_
_
_
_
_
_



Code:
class Bitmap
  def draw_2pic_gradient_bar(x, y, current, max, background_file, bar_file)
    bitmap = RPG::Cache.picture(background_file)
    self.blt(x, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.picture(bar_file)
    w = Integer(bitmap.width * (current / max.to_f))
    self.blt(x, y, bitmap, Rect.new(0, 0, w, bitmap.height))
  end
end
 
Code:
  def refresh_a
    for i in 1...$game_party.actors.size
      @hp_current = $game_party.actors[i].hp
      refresh_bars if @hp_current != @old_hp
    end
  end
  def refresh
    for i in 1...$game_party.actors.size
      @old_hp = $game_party.actors[i].hp      refresh_a
    end
  end
I think this is your problem :)
Make the @old_hp and @current_hp an array. Actually, you can drop the @current_hp.
One, more thing, why do you start at 1 in this line?
Code:
for i in 1...$game_party.actors.size
try this instead:
Code:
def refresh
  @old_hp = [ ] if @old_hp == nil
  for i in 1...$game_party.actors.size
    if @old_hp[i] != $game_party.actors[i].hp
      refresh_bars
      @old_hp[i] = $game_party.actors[i].hp
    end
  end
end

Code:
class Scene_Map
  alias game_hud_main initialize
  alias game_hud_refresh update
  def initialize
    @hud_win = Game_Hud.new
    game_hud_main
    [COLOR=Red][b]@hud_win.dispose if not @hud_win.disposed?[/b][/COLOR]
  end
  def update
    @hud_win.refresh
    game_hud_refresh
  end
end
 
Well, it doesn't get any errors as such, so that code must be right, thanks.

But, the problem is still there, the bars don't show...

Here's the updated code:

Code:
# By Trickster -----------------------------------------------------------------
class Bitmap
  def draw_2pic_gradient_bar(x, y, current, max, background_file, bar_file)
    bitmap = RPG::Cache.picture(background_file)
    self.blt(x, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.picture(bar_file)
    w = Integer(bitmap.width * (current / max.to_f))
    self.blt(x, y, bitmap, Rect.new(0, 0, w, bitmap.height))
  end
  #def draw_2pic_vertical_bar(x, y, current, max, background_file, bar_file)
  #  bitmap = RPG::Cache.picture(background_file)
  #  self.blt(x, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
  #  bitmap = RPG::Cache.picture(bar_file)
  #  h = Integer(bitmap.width * (current / max.to_f))
  #  self.blt(x, y, bitmap, Rect.new(0, 0, bitmap.width, h))
  #end
end
# End --------------------------------------------------------------------------

class Game_Hud < Window_Base
  def initialize
    super(363-16, 232-16, 103+32, 75+32)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.z = 9999
    #self.opacity = 0
    #self.visible = $game_switches[x]
    self.windowskin = RPG::Cache.windowskin('001-blue01')
    refresh_bars
  end
  def refresh_bars
    #draw_player_bars
    draw_group_bars
  end
  def draw_group_bars
    for i in 1...$game_party.actors.size
      x = 36
      y = 8 + i * 15
      p = $game_party.actors[i]
      name = p.name
      self.contents.draw_2pic_gradient_bar(x, y, p.hp, p.hpmax, 'party_hpback', 'party_hp')
      self.contents.font.colour = black
      self.contents.font.size = 12
      self.contents.draw_text(38, 10 + y, width, height, '#{name}')
    end
  end
  #def draw_player_bars
  #  now = $game_party.actors[0].now_exp
  #  nex = $game_party.actors[0].next_exp
  #  hp = $game_party.actors[0].hp
  #  hpm = $game_party.actors[0].maxhp
  #  mp = $game_party.actors[0].sp
  #  mpm = $game_party.actors[0].maxsp
  #  self.contents.draw_2pic_vertical_bar(1, 1, hp, hpm, 'playerback', 'playerhp')
  #  self.contents.draw_2pic_vertical_bar(10, 1, mp, mpm, 'playerback', 'playermp')
  #  self.contents.draw_2pic_vertical_bar(19, 1, now, nex, 'playerback', 'playerxp')
  #end
  def refresh
    #draw_player_bars if @oldnow != $game_party.actors[0].now_exp
    #draw_player_bars if @oldhp != $game_party.actors[0].hp
    #draw_player_bars if @oldmp != $game_party.actors[0].sp
    @old_hp = [ ] if @old_hp == nil
    for i in 1...$game_party.actors.size
      if @old_hp[i] != $game_party.actors[i].hp
        draw_group_bars
        @old_hp[i] = $game_party.actors[i].hp
      end
    end
  end
end
class Scene_Map
  alias game_hud_main initialize
  alias game_hud_refresh update
  def initialize
    @hud_win = Game_Hud.new
    game_hud_main
    #@hud_win.dispose if not @hud_win.disposed?
  end
  def update
    @hud_win.refresh
    game_hud_refresh
  end
end

Note: I was trying something else here for another part of the Hud, but I've commented it out.
 
Am i your hero now? :D :*)

To call the Window:
Code:
@yourhud = Window_HUD.new

Script:
Code:
class Bitmap
  def draw_hp_picture(x, y, min, max, background='party_hpback', bar='party_hp')
    background = RPG::Cache.picture(background)
    bar = RPG::Cache.picture(bar)
    background_rect = Rect.new(0, 0, background.width, background.height)
    bar_rect = Rect.new(0, 0, bar.width*min/max, bar.height)
    self.blt(x, y, background, background_rect)
    self.blt(x, y, bar, bar_rect)
  end
end

#==============================================================================
# ** Window_HUD
#------------------------------------------------------------------------------
#  This window displays the HUD
#==============================================================================

class Window_HUD < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(363-16, 232-16, 103+32, 75+32)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.size = 12
    self.contents.font.color = Color.new(64, 64, 64)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      x = 0
      y = i * 15 + 8
      self.contents.draw_hp_picture(x, y, actor.hp, actor.maxhp)
      self.contents.draw_text(x, y, width, 12, actor.name)
    end
  end
end

Wyatt;239727 said:
Edit:

While this topic is here.. anyone know how I can change this to make a verticle bar, starting from the bottom going up?
i.e.
_
_
_
_
_
_
_
I'm not sure but you can try this one, maxhp is at the bottom, minimum hp is above.
Code:
class Bitmap
  def draw_hp_picture_vertical(x, y, min, max, background='party_hpback', bar='party_hp')
    background = RPG::Cache.picture(background)
    bar = RPG::Cache.picture(bar)
    background_rect = Rect.new(0, 0, background.width, background.height)
    bar_rect = Rect.new(0, 0, bar.width, bar.height*min/max)
    self.blt(x, y, background, background_rect)
    self.blt(x, y, bar, bar_rect)
  end
end
this one here is exactly the opposite
Code:
class Bitmap
  def draw_hp_picture_vertical(x, y, min, max, background='party_hpback', bar='party_hp')
    background = RPG::Cache.picture(background)
    bar = RPG::Cache.picture(bar)
    background_rect = Rect.new(0, 0, background.width, background.height)
    h = bar.height * min / max
    bar_rect = Rect.new(0, 0, bar.width, h)
    self.blt(x, y, background, background_rect)
    self.blt(x, y+bar.height-h, bar, bar_rect)
  end
end
just try whichever you like!
 
Am i your hero now?

Does this answer your question?

OMFG @_@ *falls over and dies*

It's absolutely brilliant, works perfectly. (Some of the numbers were off but that's just poor measurement on my behalf :))

Just to let you know, here's a screenshot of it in action:

I haven't tried the vertical bars yet.
Edit: Have now, and they also work perfectly.

*adds name to credits*

http://img.photobucket.com/albums/v108/ ... eywork.png[/IMG]
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top