Sprite Lagging (About HP/MP/EXP/STA HUD)

● ARCHIVED · READ-ONLY
Started by aldwin7894 7 posts View original ↗
  1. Hi, I am making a HUD script for my game with hp/mp/exp/stamina. When i tested it, after making changes to hp/mp/exp/stamina, from a fps of 35-42, it drops to 8-12. I am a neophyte on scripting, i have just learned it a while ago. I think it might be on the update method or on proper disposal? xD
    Please help me..
     
    Screenshot:

    Spoiler
    THIS IS THE FULL SCRIPT:(I have just embedded the XS - Stamina script so that i'll not get confused.)

    Spoiler
    Code:
    module ELSWORD	module HUD    #==========================================================================#		#  \\*HUD    #==========================================================================#           #[FONT, FONT SIZE, COLOR, OUTLINE COLOR, BOLD, ITALIC, SHADOW]     FONT = [["Friz Quadrata"], 12, Color.new(0,0,0), Color.new(255, 255, 255, 80), true, false, false]                                #GUI PICTURE		HUD_GUI = "GUI"    #HP BAR PICTURE		HUD_HP = "hp bar"    #MP BAR PICTURE		HUD_MP = "mp bar"    #STAMINA BAR PICTURE		HUD_STAMINA = "stamina bar"    #EXP BAR PICTURE    EXP_UI = "exp ui"		HUD_EXP = "exp bar"    #==========================================================================#		#  \\*STAMINA    #==========================================================================#    # Setup default and max stamina value.    # Use a integer.    INIT_STAMINA = 2000    MAX_STAMINA = 2000    # Stamina recover. How long it should wait before the player regain stamina.    # 60 frames (1 second).    # RECOVER_TIME = integer    RECOVER_TIME = 540    # Set how much stamina you will recover when not dashing.    # INIT_RECOVER = integer    INIT_RECOVER = 250    # Recover region.    # The player will regain stamina much faster in this area.    # RECOVER_REGION_ID = region_id    RECOVER_REGION_ID = 20    # Set how much stamina the player will recover in the region.    # RECOVER_REGION = integer    RECOVER_REGION_STAMINA = INIT_RECOVER * 2    # Set how fast the recover time should be in the region.    # RECOVER_REGION_TIME = integer    RECOVER_REGION_TIME = 60    # Set if you want stamina to be recovered while moving. (nil to disable)    # This should obviously be less then INIT_RECOVER.    # RECOVER_WHILE_MOVING = integer    RECOVER_WHILE_MOVING = nil	endend#==============================================================================##   \\*END OF EDITABLE REGION#==============================================================================##==============================================================================##  //*STAMINA#==============================================================================##==============================================================================## ** Game_Interpreter#==============================================================================#class Game_Interpreter  alias xail_stamina_sys_gm_interpreter_cmd_314 command_314  def command_314(*args, &block)    # // Method to regain stamina when recovering. (resting at an Inn etc)    xail_stamina_sys_gm_interpreter_cmd_314(*args, &block)    $game_player.set_stamina($game_player.max_stamina, :stamina)    end  end#==============================================================================## ** Game_Player#==============================================================================#class Game_Player < Game_Character  attr_reader :stamina, :max_stamina  alias xail_stamina_sys_gm_player_init initialize  def initialize(*args, &block)    # // Method to initialize game player.    player = $game_party.members[0]    @stamina = ELSWORD::HUD::INIT_STAMINA    @max_stamina = ELSWORD::HUD::MAX_STAMINA    @stamina_rate = 0    @stamina_time = ELSWORD::HUD::RECOVER_TIME    xail_stamina_sys_gm_player_init(*args, &block)  end    def set_stamina(value, stamina_type, operator = :add)    # // Method to set stamina based on stamina type.    case stamina_type    when :stamina      @stamina = (@stamina + value).clamp(0, @max_stamina) if operator == :add      @stamina = (@stamina - value).clamp(0, @max_stamina) if operator == :lose    when :max_stamina      @max_stamina += value if operator == :add      @max_stamina -= value if operator == :lose    end  end  alias xail_stamina_sys_gm_player_move_by_input move_by_input  def move_by_input(*args, &block)  # // Method to control movement.    recover_stamina if @stamina < @max_stamina and !dash? or !moving?      set_stamina(10, :stamina, :lose) if dash? and moving?    xail_stamina_sys_gm_player_move_by_input(*args, &block)  end    alias xail_stamina_sys_gm_player_dash? dash?  def dash?(*args, &block)    # // Method to check if player is dashing.    return false if @stamina <= 0    return xail_stamina_sys_gm_player_dash?(*args, &block)  end    def recover_stamina    # // Method to recover stamina.    @stamina_rate += 1    if @stamina_rate >= @stamina_time      if region_id == ELSWORD::HUD::RECOVER_REGION_ID        @stamina_time = ELSWORD::HUD::RECOVER_REGION_TIME        stamina = ELSWORD::HUD::RECOVER_REGION_STAMINA      else        @stamina_time = ELSWORD::HUD::RECOVER_TIME        stamina = ELSWORD::HUD::INIT_RECOVER      end      unless ELSWORD::HUD::RECOVER_WHILE_MOVING.nil?        set_stamina(ELSWORD::HUD::RECOVER_WHILE_MOVING, :stamina) unless dash? and !moving?       end      set_stamina(stamina, :stamina) unless dash? or moving?      @stamina_rate = 0    end  end  end#==============================================================================##   \\*END OF STAMINA#==============================================================================##==============================================================================##		//*ACTOR NAME AND FACE WINDOW#==============================================================================#class Elsword_Window < Window_Base  alias aldwin7894_elsword_window_initialize initialize  def initialize    super(0,-10,Graphics.width,125)    self.z = 275    self.opacity = 0    create_contents    @actor = $game_party.members[0]    refresh()  end    def refresh    self.contents.clear    contents.clear    draw_window_content  end    def draw_window_content    @level = @actor.level    draw_face(@actor.face_name, @actor.face_index, 0, -30, enable = true)    draw_actor_name(@actor, 150, 0)    draw_actor_level(@actor, 300, 0)  end    def update    super()    if @level != @actor.level      refresh    end      end  end#==============================================================================##   \\*ACTOR NAME AND FACE WINDOW#==============================================================================##==============================================================================##		//*HUD#==============================================================================#class Scene_Map < Scene_Base  alias aldwin7894_elsword_hud_start start  def start(*args, &block)    aldwin7894_elsword_hud_start(*args, &block)    super()    @old_stamina = $game_player.stamina    @old_max_stamina = $game_player.max_stamina    @wait_stamina = 2    #=================================================    @elsword_window = Elsword_Window.new    #=================================================    @hud_gui = Cache.picture(ELSWORD::HUD::HUD_GUI)		@hud_hp = Cache.picture(ELSWORD::HUD::HUD_HP)		@hud_mp = Cache.picture(ELSWORD::HUD::HUD_MP)		@hud_stamina = Cache.picture(ELSWORD::HUD::HUD_STAMINA)		@hud_exp = Cache.picture(ELSWORD::HUD::HUD_EXP)    @exp_ui = Cache.picture(ELSWORD::HUD::EXP_UI)    @back = Cache.picture("back.png")    #=================================================    @hud_spr = Sprite.new()    @hud_spr.bitmap = @hud_gui    @hud_spr.x = 0    @hud_spr.y = 0	  @hud_spr.z = 280    src_rect = Rect.new(0, 0, @hud_gui.width, @hud_gui.height)        @hud_spr.bitmap.blt(0, 0, @hud_gui, src_rect)    #=================================================    @back_bitmap = Sprite.new()    @back_bitmap.bitmap = @back    @back_bitmap.x = 0    @back_bitmap.y = 0    @back_bitmap.z = 270    src_rect = Rect.new(0, 0, @back.width, @back.height)    @back_bitmap.bitmap.blt(0, 0, @back, src_rect)    #=================================================    @exp_ui_spr = Sprite.new()    @exp_ui_spr.bitmap = @exp_ui    @exp_ui_spr.x = 0    @exp_ui_spr.y = Graphics.height - @exp_ui.height    @exp_ui_spr.z = 270    src_rect = Rect.new(0, 0, @exp_ui.width, @exp_ui.height)        @exp_ui_spr.bitmap.blt(0, 0, @exp_ui, src_rect)    #=================================================    refresh()  end    def create_elsword_sta_hud()    @sta_spr = Sprite.new(@viewport1)    @sta_spr.bitmap = Bitmap.new(@hud_stamina.width, @hud_stamina.height)    @sta_spr.x = 128.9    @sta_spr.y = 73	  @sta_spr.z = 300        src_rect = Rect.new(0, 0, @hud_stamina.width * @old_stamina / $game_player.max_stamina, @hud_stamina.height)    @sta_spr.bitmap.blt(0, 0, @hud_stamina, src_rect)  end    def create_elsword_hp_hud()    @hp_spr = Sprite.new(@viewport3)    @hp_spr.bitmap = Bitmap.new(@hud_hp.width, @hud_hp.height)    @hp_spr.x = 133.7    @hp_spr.y = 32.5	  @hp_spr.z = 300        src_rect = Rect.new(0, 0, @hud_hp.width * @hp/@mhp, @hud_hp.height)        @hp_spr.bitmap.blt(0, 0, @hud_hp, src_rect)  end    def create_elsword_exp_hud()    @exp_spr = Sprite.new(@viewport2)    @exp_spr.bitmap = Bitmap.new(@hud_exp.width, @hud_exp.height)    @exp_spr.x = 60    @exp_spr.y = Graphics.height - @exp_ui.height + 15    @exp_spr.z = 350    src_rect = Rect.new(0, 0, @hud_exp.width * @current_exp/@exp_lvl, @hud_exp.height)    @exp_spr.bitmap.blt(0, 0, @hud_exp, src_rect)  end    def create_elsword_mp_hud()    @mp_spr = Sprite.new(@viewport2)    @mp_spr.bitmap = Bitmap.new(@hud_mp.width, @hud_mp.height)    @mp_spr.x = 128    @mp_spr.y = 48.5	  @mp_spr.z = 300        src_rect = Rect.new(0, 0, @hud_mp.width * @mp/@mmp, @hud_mp.height)     @mp_spr.bitmap.blt(0, 0, @hud_mp, src_rect)  end    def draw_hp_texts()    @hp_spr.bitmap.font.name = ELSWORD::HUD::FONT[0]    @hp_spr.bitmap.font.size = ELSWORD::HUD::FONT[1]    @hp_spr.bitmap.font.color = ELSWORD::HUD::FONT[2]    @hp_spr.bitmap.font.out_color = ELSWORD::HUD::FONT[3]    @hp_spr.bitmap.font.bold = ELSWORD::HUD::FONT[4]    @hp_spr.bitmap.font.italic = ELSWORD::HUD::FONT[5]    @hp_spr.bitmap.font.shadow = ELSWORD::HUD::FONT[6]    @hp_spr.bitmap.draw_text(@hp_spr.bitmap.width - 80, -6.5, @hp_spr.bitmap.width, 24, @hp.to_s + " / " + @mhp.to_s, 0)  end    def draw_mp_texts()    @mp_spr.bitmap.font.name = ELSWORD::HUD::FONT[0]    @mp_spr.bitmap.font.size = 10    @mp_spr.bitmap.font.color = ELSWORD::HUD::FONT[2]    @mp_spr.bitmap.font.out_color = ELSWORD::HUD::FONT[3]    @mp_spr.bitmap.font.bold = ELSWORD::HUD::FONT[4]    @mp_spr.bitmap.font.italic = ELSWORD::HUD::FONT[5]    @mp_spr.bitmap.font.shadow = ELSWORD::HUD::FONT[6]    @mp_spr.bitmap.draw_text(@mp_spr.bitmap.width - 80, -7.5, @mp_spr.bitmap.width, 24, @mp.to_s + " / " + @mmp.to_s, 0)  end    def draw_exp_texts()    @exp_spr.bitmap.font.name = ELSWORD::HUD::FONT[0]    @exp_spr.bitmap.font.size = 10    @exp_spr.bitmap.font.color = ELSWORD::HUD::FONT[2]    @exp_spr.bitmap.font.out_color = ELSWORD::HUD::FONT[3]    @exp_spr.bitmap.font.bold = ELSWORD::HUD::FONT[4]    @exp_spr.bitmap.font.italic = ELSWORD::HUD::FONT[5]    @exp_spr.bitmap.font.shadow = ELSWORD::HUD::FONT[6]    @exp_spr.bitmap.draw_text(@exp_spr.bitmap.width / 2 -20, -7.8, @exp_spr.bitmap.width, 24,  @current_exp.to_s + " / " + @exp_lvl.to_s, 0)  end    def refresh    @actor = $game_party.members[0]    @exp = @actor.exp    @current_exp = @actor.exp - @actor.current_level_exp    @exp_lvl = @actor.next_level_exp - @actor.current_level_exp        @hp = @actor.hp    @mhp = @actor.mhp    @mp = @actor.mp    @mmp = @actor.mmp  end    alias aldwin7894_hud_update update  def update(*args, &block)    return unless SceneManager.scene_is?(Scene_Map)    @elsword_window.update if @level != @actor.level    #=================================================    if @hud_spr.nil? || @hp_spr.nil? || @sta_spr.nil? || @mp_spr.nil? || @exp_spr.nil?      create_elsword_hp_hud()      create_elsword_mp_hud()      create_elsword_sta_hud()      create_elsword_exp_hud()      draw_mp_texts()      draw_hp_texts()      draw_exp_texts()    end    #=================================================    if @hp != @mhp      if @hp == 0        @hp_spr.dispose      end        @hp_spr.dispose()        create_elsword_hp_hud()        draw_hp_texts()    end    #=================================================    if @mp != @mmp      if @mp == 0        @mp_spr.dispose      end      @mp_spr.bitmap.clear      create_elsword_mp_hud()      draw_mp_texts()    end    #=================================================    if @exp != @actor.exp      @exp_spr.bitmap.dispose      create_elsword_exp_hud()      draw_exp_texts()      refresh()    elsif @exp == @actor.exp      @exp_spr.bitmap.clear      create_elsword_exp_hud()      draw_exp_texts()      refresh()    end    #=================================================      @wait_stamina -= 1 if @wait_stamina != 0    if @old_stamina != $game_player.stamina and @wait_stamina == 0      if @old_stamina < $game_player.stamina        @old_stamina += 10      else        @old_stamina -= 10 if $game_player.dash? & $game_player.moving?      end      @sta_spr.bitmap.clear      create_elsword_sta_hud()      @wait_stamina = 1    end    aldwin7894_hud_update(*args, &block)  end    alias aldwin7894_elsword_hud_terminate terminate  def terminate(*args, &block)    # // Method to terminate scene map.    aldwin7894_elsword_hud_terminate(*args, &block)    @sta_spr.dispose    @hp_spr.dispose    @mp_spr.dispose    @elsword_window.dispose    @exp_spr.dispose    @exp_ui_spr.dispose  endend#==============================================================================##   \\*END OF HUD#==============================================================================#
  2. I assume it is because you handle the update wrong.

    I haven't made a detailed look at your script but you want to update only if any value changes.

    So you need to make a temp variable for all of your values.

    For example for hp you make a variable called @old_hp. Then you add a check into your update method which should look like this

    if @old_hp != @hp

      @old_hp = hp

       # update all visual hp stuff

    end

    So that at the end your things dont redraw every frame, but on every change only.
  3. *shudder*

    Please remove your hud from Scene_Map.. Thats realy nasty looking.

    Here is a very simple tutorial on how to make a hud : http://dekitarpg.wordpress.com/2014/08/24/basic-hudgui/

    The only thing my tutorial doesnt cover is making the data only be redrawn when required, which Evgenij mentioned in the last post :)
  4. Thank you very much for all of your replies, i will try your suggestions..
  5. I ran into this myself when I first set up a hud.


    Take a look at how Game_Map handles updating of events and other stuff. It uses a flag - @need_refresh. Any time something changes (variables, switches, other stuff) that might affect what you see on the map, those things set $game_map.need_refresh to true. Then in the Game_Map.update method (I think - I don't have Ace in front of me), it checks that flag to see if the events and other stuff on the map need to be refreshed.


    I put something similar in for my hud. I gave it its own class and attached it to either Game_Map or Scene_Map (again, I think it was Game_Map), and I added a similar flag - @need_hud_refresh. Any method of any class that changed things important to the hud then set this variable to true, and Game_Map checks to see if it needs to update the hud based on that value (and then resets it to false when it does update it).
  6. @Dekita, i have used the idea of you $d13x hud and now its ok. Thank you very much..
  7. No worries :)