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.

HUD Help

I expanded my HUD once again, however the last thing I would like isn't working, really.
Because I'm like trying for the past 10 hours how to do something like this :

Code:
@percent = @now_exp / @next_exp * 100.00

So that it shows the % of Exp. that is already gotten.
  For understanding :
  - You need 20 Exp. to get to Level 2.
  - You defeat a monster and get 2 Exp.
  - You now have 10.00% (With the decimals. :cool:)

Code:
SWITCHC = 43 #change to your switch, don't put in any leading zeros!
class Game_Actor
#--------------------------------------------------------------------------
# * Get the current EXP
#--------------------------------------------------------------------------
def now_exp
return @exp - @exp_list[@level]
end
#--------------------------------------------------------------------------
# * Get the next level's EXP
#--------------------------------------------------------------------------
def next_exp
return @exp_list[@level+1] > 0 ? @exp_list[@level+1] - @exp_list[@level] : 0
end
end
#========================================================================
# ** Window_HUD
#------------------------------------------------------------------------------
# This class is used to display HP, SP and Gold on the map.
#========================================================================
class Window_HUD < Window_Base
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(-16, 404, 680, 480)
self.contents = Bitmap.new(width-32, height-32)
self.opacity = 0
self.contents.font.size = 14
self.visible = $game_switches[SWITCHC]
@actor = $game_party.actors[0]
@old_hp = @actor.hp
@old_maxhp = @actor.maxhp
@old_sp = @actor.sp
@old_maxsp = @actor.maxsp
@old_exp = @actor.now_exp
@maxexp = @actor.next_exp
@old_level = @actor.level
@old_gold = $game_party.gold
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.font.color = system_color
self.contents.font.color = normal_color

#Images ------------------------------------------------------------------
#bitmap = RPG::Cache.picture("Game - Base HUD3")
#self.contents.blt(0, 27, bitmap, Rect.new(0, 10, 640, 640))
#bitmap = RPG::Cache.picture("Game - HUD2")
#self.contents.blt(0, 0, bitmap, Rect.new(0, 10, 640, 565))
#bitmap = RPG::Cache.icon("Level - (#{@actor.level})")
#self.contents.blt(40, 37, bitmap, Rect.new(0, 0, 100, 100))
#bitmap = RPG::Cache.icon("Game - HUD HP Symbol")
#self.contents.blt(213, 22, bitmap, Rect.new(0, 0, 24, 24))
#bitmap = RPG::Cache.icon("Game - HUD SP Symbol")
#self.contents.blt(324, 23, bitmap, Rect.new(0, 0, 24, 24))
#bitmap = RPG::Cache.icon("Game - HUD EXP Symbol")
#self.contents.blt(435, 27, bitmap, Rect.new(0, 0, 50, 50))
#bitmap = RPG::Cache.icon("Game - HUD Mesos Symbol")
#self.contents.blt(80, 44, bitmap, Rect.new(0, 0, 24, 24))
#--------------------------------------------------------------------------
x = 218
y = 15
a = 480
self.contents.font.bold = true
self.contents.font.name = "Verdana"
self.contents.font.size = 11
self.contents.draw_text(80, 20, 120, 28, @actor.name)
self.contents.draw_text(128, 20, 120, 28, "[#{@actor.class_name}]")
self.contents.draw_text(463, 21, 250, 28, "[#{@actor.now_exp} / #{@actor.next_exp}]")
self.contents.draw_text(237, 21, 100, 28, "[#{@actor.hp} / #{@actor.maxhp}]")
self.contents.draw_text(349, 21, 100, 28, "[#{@actor.sp} / #{@actor.maxsp}]")
y += 30
draw_hp_bar(@actor, x, y, 100, 7)
x += 110
draw_sp_bar(@actor, x, y, 100, 7)
x += 110
draw_exp_bar(@actor, x, y, 100, 7)
x += 130
x = 32
self.contents.draw_text(95, 38, 200, 28, "Mesos : " + $game_party.gold.to_s)
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
  def update
    super
    self.visible = $game_switches[SWITCHC]
    if @old_hp != @actor.hp or
      @old_maxhp != @actor.maxhp or
      @old_sp != @actor.sp or
      @old_maxsp != @actor.maxsp or
      @old_exp != @actor.now_exp or
      @maxexp != @actor.now_exp or
      @old_level != @actor.level or
      @old_gold != $game_party.gold
      refresh
      @old_hp = @actor.hp
      @old_maxhp = @actor.maxhp
      @old_maxsp = @actor.maxsp
      @old_sp = @actor.sp
      @old_exp = @actor.now_exp
      @maxexp = @actor.next_exp
      @old_level = @actor.level
      @old_gold = $game_party.gold
    end
  end
#--------------------------------------------------------------------------
# * Draw HP Bar
#--------------------------------------------------------------------------
def draw_hp_bar(actor, x, y, length, thick)
width = length
height = thick
c1 = Color.new(255, 70, 70)
c2 = Color.new(155, 40, 40)
e1 = actor.hp
e2 = actor.maxhp
self.contents.fill_rect(x-1, y - 1, width+2, height + 3, Color.new(255, 255, 255, 255))
self.contents.fill_rect(x, y, width, height + 1, Color.new(0, 0, 0, 255))
w = width * e1 / e2
for i in 0..height
r = c1.red + (c2.red - c1.red) * (height -i)/height + 0 * i/height
g = c1.green + (c2.green - c1.green) * (height -i)/height + 0 * i/height
b = c1.blue + (c2.blue - c1.blue) * (height -i)/height + 0 * i/height
a = c1.alpha + (c2.alpha - c1.alpha)* (height -i)/height + 255 * i/height
self.contents.fill_rect(x, y+i, w, 1, Color.new(r, g, b, a))
end
end
#--------------------------------------------------------------------------
# * Draw SP Bar
#--------------------------------------------------------------------------
def draw_sp_bar(actor, x, y, length, thick)
width = length
height = thick
c1 = Color.new(70, 70, 255)
c2 = Color.new(40, 40, 155)
e1 = actor.sp
e2 = actor.maxsp
self.contents.fill_rect(x-1, y - 1, width+2, height + 3, Color.new(255, 255, 255))
self.contents.fill_rect(x, y, width, height + 1, Color.new(0, 0, 0))
w = width * e1 / e2
for i in 0..height
r = c1.red + (c2.red - c1.red) * (height -i)/height + 0 * i/height
g = c1.green + (c2.green - c1.green) * (height -i)/height + 0 * i/height
b = c1.blue + (c2.blue - c1.blue) * (height -i)/height + 0 * i/height
a = c1.alpha + (c2.alpha - c1.alpha)* (height -i)/height + 255 * i/height
self.contents.fill_rect(x, y+i, w, 1, Color.new(r, g, b, a))
end
end
#--------------------------------------------------------------------------
# * Draw EXP Bar
#--------------------------------------------------------------------------
def draw_exp_bar(actor, x, y, length, thick)
width = length
height = thick
c1 = Color.new(255, 255, 60)
c2 = Color.new(110, 190, 10)
e1 = actor.now_exp
e2 = actor.next_exp
if actor.next_exp == 0
e1 = 1
e2 = 1
end
self.contents.fill_rect(x-1, y - 1, width+2, height + 3, Color.new(255, 255, 255, 255))
self.contents.fill_rect(x, y, width, height + 1, Color.new(0, 0, 0, 255))
w = width * e1 / e2
for i in 0..height
r = c1.red + (c2.red - c1.red) * (height -i)/height + 0 * i/height
g = c1.green + (c2.green - c1.green) * (height -i)/height + 0 * i/height
b = c1.blue + (c2.blue - c1.blue) * (height -i)/height + 0 * i/height
a = c1.alpha + (c2.alpha - c1.alpha)* (height -i)/height + 255 * i/height
self.contents.fill_rect(x, y+i, w, 1, Color.new(r, g, b, a))
end
end
end
#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
# This class performs map screen processing.
#==============================================================================
class Scene_Map
#--------------------------------------------------------------------------
# * Object Aliasing
#--------------------------------------------------------------------------
alias hud_scene_map_main main
alias hud_scene_map_update update
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def main
@HUD = Window_HUD.new
hud_scene_map_main
@HUD.dispose
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
@HUD.update
hud_scene_map_update
end
end

Thanks if anyone is willing to do this for me. I'm tired and sick of this. ???
Btw; If you look in the script you'll see I #'ed the pictures I use.
 

poccil

Sponsor

The instance variable @level in Game_Actor stores the actor's initial level, not its current level, which can be obtained with Game_Actor#level.  That's because the actor's level is calculated from its current experience.  Therefore the problem can be fixed by replacing "@level" with "self.level".  Note that the expPercent method below, also of Game_Actor, retrieves the EXP percentage as you request.  Replace the code at the top (except the switch constant) with:

Code:
class Game_Actor
#--------------------------------------------------------------------------
# * Get the current EXP
#--------------------------------------------------------------------------
def now_exp
return @exp - @exp_list[self.level]
end
#--------------------------------------------------------------------------
# * Get the next level's EXP
#--------------------------------------------------------------------------
def next_exp
return @exp_list[self.level+1] > 0 ? @exp_list[self.level+1] - @exp_list[self.level] : 0
end
def expPercent
  percent = self.now_exp / self.next_exp * 100.00
  return sprintf("%.2f%%",percent)
end
end
 
EDIT4
I've been like trying to get this right for 22 hours total.

The problem was the calculating method I guess, I changed:
Code:
procent = self.now_exp / self.next_exp * 100.00

Into :
Code:
procent = 100.00 / self.next_exp * self.now_exp

This has been resolved, thanks to :
- poccil.
<Teaching> ;)
- My dad.
<Reminding me that there's more than 1 method to calculate percentage.>
- Myself.
<Coding> ;D

=================================================================

Thanks for explaining that to me poccil. :cool:
I did what you said however, the exp. percentage remains at 0.00%.

EDIT1 :

I just tried it in a new project, still no luck, same result.

EDIT2 :

The percent still doesn't work however, I remade the third def of your code into this :
Code:
def percent
  if self.next_exp == 0
  procent = 0.00
 return sprintf("%.2f%%", procent)
 else
  procent = self.now_exp / self.next_exp * 100.00
 return sprintf("%.2f%%", procent)
end
end
end
Is that correct? (Because there was an error when the actor was at the max. level.)
 

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