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.

Why is my HUD causing lag?

I made a HUD for my game, and it is causing MAJOR lag, without it I run 40 FPS, and with it, I run 4 FPS...could someone help and see what I did wrong to make it lag like this.

Here is the HUD, and please, don't take this and claim it as your own :) thanks!

Code:
#------------------------------------------------------
# Shifter HUD 
# v 1.0
# Author - Joshua Long
# This HUD is made for Shifter.
#------------------------------------------------------
class Window_hud < Window_Base
  def initialize
    super(-10, -10, 650, 650)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = "Arial"
    self.contents.font.size = 15
    self.back_opacity = 0
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
    return if !@actor
    draw_actor_graphic(@actor, 18, 58)
    draw_bar(63, 70, @actor.sp, @actor.maxsp, 90, 8, Color.new(88, 200, 244, 255), Color.new(0, 56, 200, 255))
    self.contents.draw_text(0, 48, 120, 32, "Stamina: " + $game_party.actors[0].sp.to_s + " / " + $game_party.actors[0].maxsp.to_s)
    draw_actor_name(@actor, 0, -10.5)
    self.contents.draw_text(40, 0, 92, 32, "Strength: " +$game_party.actors[0].str.to_s)
    self.contents.draw_text(40, 12, 92, 32, "Agility: " +$game_party.actors[0].agi.to_s)
    self.contents.draw_text(40, 24, 92, 32, "Intelligence: " +$game_party.actors[0].int.to_s)
    self.contents.draw_text(40, 36, 92, 32, "Dexterity: " +$game_party.actors[0].dex.to_s)
  end
  def reset_variables
    @actor = $game_party.actors[0]
    @oldsp = @actor ? @actor.sp : 0
    @oldmaxsp = @actor ? @actor.maxsp : 0
    @str = @actor ? @actor.str : 0
    @agi = @actor ? @actor.agi : 0
    @int = @actor ? @actor.int : 0
    @eva = @actor ? @actor.dex : 0
  end
  def update
    @actor = $game_party.actors[0] 
    @oldsp = @actor ? @actor.sp : 0 
    @oldmaxsp = @actor ? @actor.maxsp : 0 
    @str = @actor ? @actor.str : 0 
    @agi = @actor ? @actor.agi : 0 
    @int = @actor ? @actor.int : 0 
    @eva = @actor ? @actor.dex : 0
    refresh
  end
end
class Scene_Map
  alias hud_main main
  alias hud_update update
  def main
    @hud = Window_hud.new
    hud_main
    @hud.dispose
  end
  def update
    @hud.update
    hud_update
  end
end
#================================================= =============================
# ** Window_Base
#------------------------------------------------------------------------------
# This class is for all in-game windows.
#================================================= =============================
class Window_Base < Window
#--------------------------------------------------------------------------
# * Draw Slant Bar
#--------------------------------------------------------------------------
def draw_bar(x, y, min, max, width = 152, height = 6,
bar_color = Color.new(150, 0, 0, 255), end_color = Color.new(255, 255, 60, 255))
# Draw Border
for i in 0..height
self.contents.fill_rect(x, y + height - i, width, 1, Color.new(50, 50, 50, 255))
end
# Draw Background
for i in 1..(height - 1)
r = 100 * (height - i) / height + 0 * i / height
g = 100 * (height - i) / height + 0 * i / height
b = 100 * (height - i) / height + 0 * i / height
a = 255 * (height - i) / height + 255 * i / height
self.contents.fill_rect(x, y + height - i, width, 1, Color.new(r, b, g, a))
end
# Draws Bar
for i in 1..( (min / max.to_f) * width - 1)
for j in 1..(height - 1)
r = bar_color.red * (width - i) / width + end_color.red * i / width
g = bar_color.green * (width - i) / width + end_color.green * i / width
b = bar_color.blue * (width - i) / width + end_color.blue * i / width
a = bar_color.alpha * (width - i) / width + end_color.alpha * i / width
self.contents.fill_rect(x + i - 1, y + height - j, 1, 1, Color.new(r, g, b, a))
end
end
end
end
 

ikos

Member

The lag is from the extra junk on the screen.
You have a few active bars and an image that's basically constantly being displayed.
That's the most likely scenario.
 

khmp

Sponsor

The problem is that you refresh every update. Meaning the window gets redrawn every time it's update gets called. You can keep a store of instance variables within the window so that it only gets redrawn when information changes. And when you are redrawing expensive items like slanting bars you want to limit the amount of times they get redrawn to absolute minimum.

Replace the methods sharing the names with the ones below. IE. Replace shifter's reset_variables with the one below.
Code:
def reset_variables
  @actor = $game_party.actors[0]
  @oldsp = @actor ? @actor.sp : 0
  @oldmaxsp = @actor ? @actor.maxsp : 0
  @str = @actor ? @actor.str : 0
  @agi = @actor ? @actor.agi : 0
  @int = @actor ? @actor.int : 0
  @eva = @actor ? @actor.dex : 0
end

def update
  actor = $game_party.actors[0]
  refresh if actor != @actor ||
    @oldsp != actor.sp ||
    @oldmaxsp != actor.maxsp ||
    @str != actor.str ||
    @agi != actor.agi ||
    @int != actor.int ||
    @eva != actor.dex
end

I hope that helps. You can even take the idea even further if you want to learn of course. And only redraw what changes and leave the rest of the window alone while doing so.
 

khmp

Sponsor

HAHA I forgot an "||", and left in some junk. I'm slipping, my bad.

Code:
def update
  actor = $game_party.actors[0]
  refresh if actor != @actor ||
    @oldsp != actor.sp ||
    @oldmaxsp != actor.maxsp ||
    @str != actor.str ||
    @agi != actor.agi ||
    @int != actor.int ||
    @eva != actor.dex
end
 

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