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.

[Resolved]Hello a small problem with a script of mine, maybe someone can help?

Status
Not open for further replies.
I have a HP and MP bar on my screen but im having some problems with the refreshing in the scripting.

For example, switching maps makes the hp and sp bar dissapear, but if I open and close my menu it reappears. Anyway to make it so that after I switch maps it wont make the HP and MP bars dissapear?

Heres the script, its not mine by the way, its slipknots:

Code:
#==============================================================================
# ** HP & SP Bars on Map
#------------------------------------------------------------------------------
# Slipknot
# 1.0
# 05.23.06
#==============================================================================

#------------------------------------------------------------------------------
# SDK Log
#------------------------------------------------------------------------------
SDK.log('HP & SP Bars on Map', 'Slipknot', 1.0, '05.23.06')

#------------------------------------------------------------------------------
# Begin SDK Enabled Check
#------------------------------------------------------------------------------
if SDK.state('HP & SP Bars on Map') == true

module MapBars_Settings
  #--------------------------------------------------------------------------
  HPBar = true
  #--------------------------------------------------------------------------
  SPBar = true
  #--------------------------------------------------------------------------
  LevelText = 'Lv: '
  #--------------------------------------------------------------------------
  AllowChange = true
  #--------------------------------------------------------------------------
end

#------------------------------------------------------------------------------
# Begin Scene_Map Edit
#------------------------------------------------------------------------------
class Scene_Map
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader(:sprite_bars)
  #--------------------------------------------------------------------------
  # * Alias
  #--------------------------------------------------------------------------
  alias slipknot_mapbars_maindraw main_draw
  alias slipknot_mapbars_updgraphics update_graphics
  alias slipknot_mapbars_maindispose main_dispose
  #--------------------------------------------------------------------------
  # * Main Draw
  #--------------------------------------------------------------------------
  def main_draw
    slipknot_mapbars_maindraw
    @sprite_bars = Sprite_Bars.new
  end
  #--------------------------------------------------------------------------
  # * Main Dispose
  #--------------------------------------------------------------------------
  def main_dispose
    slipknot_mapbars_maindispose
    @sprite_bars.dispose
  end
  #--------------------------------------------------------------------------
  # * Update Graphics
  #--------------------------------------------------------------------------
  def update_graphics
    slipknot_mapbars_updgraphics
    @sprite_bars.update
  end
end
#------------------------------------------------------------------------------
# End Scene_Map Edit
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# Begin Interpreter Edit
#------------------------------------------------------------------------------
class Interpreter
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader(:hp_bar, :sp_bar, :bars_index)
  attr_accessor(:bars_need_refresh)
  #--------------------------------------------------------------------------
  # * Alias
  #--------------------------------------------------------------------------
  alias slipknot_mapbars_initialize initialize
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(depth = 0, main = false)
    slipknot_mapbars_initialize(depth, main)
    if main
      @bars_need_refresh = true
      @hp_bar = MapBars_Settings::HPBar
      @sp_bar = MapBars_Settings::SPBar
      @bars_index = 0
    end
  end
  #--------------------------------------------------------------------------
  # * Bar's Actor Index
  #--------------------------------------------------------------------------
  def bars_index=(index)
    if @bars_index != index
      @bars_index = index
      @bars_need_refresh = true
    end
    return true
  end
  def set_bars_index(index = 0)
    self.bars_index = (index)
  end
  #--------------------------------------------------------------------------
  # * Show HP Bar
  #--------------------------------------------------------------------------
  def show_hp_bar(value = true)
    @hp_bar = value
    @bars_need_refresh = true
  end
  #--------------------------------------------------------------------------
  # * Show SP Bar
  #--------------------------------------------------------------------------
  def show_sp_bar(value = true)
    @sp_bar = value
    @bars_need_refresh = true
  end
end
#------------------------------------------------------------------------------
# End Interpreter Edit
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# Begin Spriteset_Map Edit
#------------------------------------------------------------------------------
class Spriteset_Map
  #--------------------------------------------------------------------------
  # * Alias
  #--------------------------------------------------------------------------
  alias slipknot_mapbars_updpicturesprites update_picture_sprites
  #--------------------------------------------------------------------------
  # * Update Picture Sprites
  #--------------------------------------------------------------------------
  def update_picture_sprites
    @timer_sprite.x = ($game_system.map_interpreter.sp_bar ||
      $game_system.map_interpreter.sp_bar) ? 400 : 552
    slipknot_mapbars_updpicturesprites
  end
end
#------------------------------------------------------------------------------
# End Spriteset_Map Edit
#------------------------------------------------------------------------------

class Sprite_Bars < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    @sprite = Sprite.new()
    @sprite.bitmap = Bitmap.new(144, 82)
    @sprite.x = 488
    @sprite.y = 8
    @old_hp = @old_sp = false
    interpreter.bars_need_refresh = true
    update
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    if ! hp? && ! sp?
      bitmap.clear
      return
    end
    if MapBars_Settings::AllowChange && Input.press?(11)
      if Input.repeat?(Input::L)
        interpreter.bars_index = (interpreter.bars_index - 1) % $game_party.actors.size
      end
      if Input.repeat?(Input::R)
        interpreter.bars_index = (interpreter.bars_index + 1) % $game_party.actors.size
      end
    end
    actor = $game_party.actors[interpreter.bars_index]
    if need_refresh?
      bitmap.clear
      lvt = MapBars_Settings::LevelText + actor.level.to_s
      width = bitmap.text_size(lvt).width
      draw_text(1, 0, 143 - width, 22, actor.name)
      draw_text(143 - width, 0, width, 22, lvt)
    end  
    if hp? && (need_refresh? || @old_hp != actor.hp)
      draw_hp_bar(actor, 0, 22)
      @old_hp = actor.hp
      hp = true
    end
    if sp? && (need_refresh? || @old_sp != actor.sp)
      y = hp != nil ? 30 : 0
      draw_sp_bar(actor, 0, y + 22)
      @old_sp = actor.sp
    end
    interpreter.bars_need_refresh = false
  end
  #--------------------------------------------------------------------------
  # * @sprite.bitmap & $game_system.map_interpreter
  #--------------------------------------------------------------------------
  def bitmap
    @sprite.bitmap
  end
  def interpreter
    $game_system.map_interpreter
  end
  #--------------------------------------------------------------------------
  # * Return Interpreter Variables
  #--------------------------------------------------------------------------
  def need_refresh?
    interpreter.bars_need_refresh
  end
  def hp?
    interpreter.hp_bar
  end
  def sp?
    interpreter.sp_bar
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    @sprite.dispose
    bitmap.dispose
  end
  #--------------------------------------------------------------------------
  # * Draw HP Bar
  #--------------------------------------------------------------------------
  def draw_hp_bar(actor, x, y)
    bitmap.fill_rect(x, y, 144, 30, Color.new(0, 0, 0, 0))
    bitmap.font.color = system_color
    draw_text(x, y, 32, 30, $data_system.words.hp)
    hp_x = x + 36
    x += 32
    bitmap.fill_rect(x, y + 14, 112, 10, Color.new(0, 0, 0))
    width = 112.0 * actor.hp / actor.maxhp
    red = 144.0
    step = 80.0 / (width - 2).to_f
    for p in 0...(width - 2)
      bitmap.fill_rect(x + p + 1, y + 15, 1, 8, Color.new(red, 64, 32))
      red += step
    end
    bitmap.font.size -= 2
    bitmap.font.color = actor.hp == 0 ? knockout_color :
      actor.hp <= actor.maxhp / 4 ? crisis_color : normal_color
    draw_text(hp_x, y, 48, 26, actor.hp.to_s, 2)
    bitmap.font.color = normal_color
    draw_text(hp_x + 48, y, 12, 26, '/', 1)
    draw_text(hp_x + 60, y, 48, 26, actor.maxhp.to_s)
    bitmap.font.size += 2
  end
  #--------------------------------------------------------------------------
  # * Draw SP Bar
  #--------------------------------------------------------------------------
  def draw_sp_bar(actor, x, y)
    bitmap.fill_rect(x, y, 144, 30, Color.new(0, 0, 0, 0))
    self.bitmap.font.color = system_color
    draw_text(x, y, 32, 30, $data_system.words.sp)
    sp_x = x + 36
    x += 32
    bitmap.fill_rect(x, y + 14, 112, 10, Color.new(0, 0, 0))
    width = 112.0 * actor.sp / actor.maxsp
    blue = 144.0
    step = 80.0 / (width - 2).to_f
    for p in 0...(width - 2)
      bitmap.fill_rect(x + p + 1, y + 15, 1, 8, Color.new(32, 64, blue))
      blue += step
    end
    bitmap.font.size -= 2
    bitmap.font.color = actor.sp == 0 ? knockout_color :
      actor.sp <= actor.maxhp / 4 ? crisis_color : normal_color
    draw_text(sp_x, y, 46, 26, actor.sp.to_s, 2)
    bitmap.font.color = normal_color
    draw_text(sp_x + 46, y, 10, 26, '/', 1)
    draw_text(sp_x + 58, y, 46, 26, actor.maxsp.to_s)
    bitmap.font.size += 2
  end
  #--------------------------------------------------------------------------
  # * Draw Outline Text
  #--------------------------------------------------------------------------
  def draw_text(x, y, width, height, string, align = 0)
    col = bitmap.font.color.dup
    bitmap.font.color = Color.new(32, 32, 32, col.alpha)
    bitmap.draw_text(x - 1, y, width, height, string, align)
    bitmap.draw_text(x, y - 1, width, height, string, align)
    bitmap.draw_text(x + 1, y, width, height, string, align)
    bitmap.draw_text(x, y + 1, width, height, string, align)
    bitmap.font.color = col
    bitmap.draw_text(x, y, width, height, string, align)
  end
end

#------------------------------------------------------------------------------
# End SDK Enabled Test
#------------------------------------------------------------------------------
end
 
Yes I have SDK, it is just a small refresh problem thats all, I need it to refresh every time I switch maps while playing.

Do you know how I can implement that in the script?

I dont have much scripting experience at all.
 
Nice scripting style!

Anyway, apparently the problem lies in Scene_Map#transfer_player , and its funkiness in handling Player Teleport. Truthfully, it's not really clear to me why the @sprite_bars is disposed (I think?) when transferring to another map...

I don't think SDK overrides this method, so look in the original Scene_Map if you want to inspect it.

Simply add this code in your Scene_Map override. Unfortunately, since the new line needs to be added in the middle of the old method, I had to include the old method in its original form. Highlighted line is the fix.

Code:
  def transfer_player
    $game_temp.player_transferring = false
    if $game_map.map_id != $game_temp.player_new_map_id
      $game_map.setup($game_temp.player_new_map_id)
    end
    $game_player.moveto($game_temp.player_new_x, $game_temp.player_new_y)
    case $game_temp.player_new_direction
    when 2
      $game_player.turn_down
    when 4
      $game_player.turn_left
    when 6
      $game_player.turn_right
    when 8
      $game_player.turn_up
    end        
    $game_player.straighten
    $game_map.update
    @spriteset.dispose
    @spriteset = Spriteset_Map.new    
[highlight]    # Edit.
    @sprite_bars = Sprite_Bars.new[/highlight]
    if $game_temp.transition_processing
      $game_temp.transition_processing = false
      Graphics.transition(20)
    end
    $game_map.autoplay
    Graphics.frame_reset    
    Input.update    
  end

I've only tested its appearance but not functionality. Let me know if the game still breaks.
 
This topic has been resolved. If Killout or any other users have any questions or further problems regarding this topic, please create a new thread about them.

Thank you!
 
Status
Not open for further replies.

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