At wit's end with Bitmap.clear

I'm working on a custom HUD which tracks some game variables and a few switches.  The problem comes from swapping a bitmap out for another (as this is an almost entirely graphical HUD).  When I go to clear my bitmaps they just vanish forever; even when I assign a picture to them immediately afterward.

There must be something I'm not getting -- I can't imagine why this isn't working properly and I can't figure another good way to do this.  This is my first custom HUD and I'm assembling it largely from scratch so I'm not sure if there's something I have to do here that I'm not doing.

Any help would be greatly appreciated.

Code:
module DIM  module HUD    X = 0    Y = 0    WIDTH = 240    HEIGHT = 64        FONT = ["Arial Narrow", "Impact"]    FONT_SIZE = 16    FONT_BOLD = false    FONT_ITAL = false        Opacity = 0  endend  class Window_HUD < Window_Base  include DIM::HUD    def initialize()    super( X, Y, WIDTH, HEIGHT)    self.contents = Bitmap.new(width-32,height-32)     self.back_opacity = 0    @hud_sprite = Sprite.new()    @status_bar_san_bg = Sprite.new()    @status_bar_san_fg = Sprite.new()    @mental_status = Sprite.new()    @danger = Sprite.new()    @saferoom = Sprite.new()    @siren = Sprite.new()    @time_rem = Sprite.new()    @status = Sprite.new()       @current_val = -1    refresh  end   def refresh    self.contents.clear    self.opacity = Opacity        #p "Refreshing"    @hud_sprite.bitmap = Cache.picture("status_bar")    @hud_sprite.z = 300        @status_bar_san_bg.bitmap = Cache.picture("status_bar_san_bg")    @status_bar_san_bg.x = 76    @status_bar_san_bg.y = 27    @status_bar_san_bg.z = 301        @status_bar_san_fg.bitmap = Cache.picture("status_bar_san_fg")    @status_bar_san_fg.x = 76    @status_bar_san_fg.y = 27    @status_bar_san_fg.z = 302        @mental_status.bitmap = Cache.picture("mental_status")    @mental_status.x = 56    @mental_status.y = 4    @mental_status.z = 300              if @current_val != -1      @status.bitmap.clear()      #p "Status bitmap cleared"    end        if @current_val != $game_variables[94]      p "New Status"      case $game_variables[94]      when 50 .. 60        @status.bitmap = Cache.picture("status_insane")        p "::STATUS ( Insane )"      when 40 .. 49        @status.bitmap = Cache.picture("status_losingit")        p "::STATUS ( Losing It )"      when 30 .. 39        @status.bitmap = Cache.picture("status_panicking")        p "::STATUS ( Panicking )"      when 20 .. 29        @status.bitmap = Cache.picture("status_frightened")        p "::STATUS ( Frightened )"      when 10 .. 19        @status.bitmap = Cache.picture("status_onedge")        p "::STATUS ( On Edge )"      when 1 .. 9        @status.bitmap = Cache.picture("status_suspicious")        p "::STATUS ( Suspicious )"      when 0        @status.bitmap = Cache.picture("status_collected")        p "::STATUS ( Collected )"      else        p "!!ERROR!! Invalid status"      end    end    @current_val = $game_variables[94]        @status.x = 68    @status.y = 14    @status.z = 400        if $game_switches[7] || $game_switches[14] || $game_switches[90] || $game_switches[91]      @danger.bitmap = Cache.picture("danger")    else      @danger.bitmap = Cache.picture("led_off")    end    @danger.x = 215    @danger.y = 6    @danger.z = 300        if $game_switches[89]      @saferoom.bitmap = Cache.picture("saferoom")    else      @saferoom.bitmap = Cache.picture("led_off")    end    @saferoom.x = 21    @saferoom.y = 11    @saferoom.z = 300        if $game_switches[91] || $game_switches[97] || $game_switches[98] || $game_switches[99]      @siren.bitmap = Cache.picture("siren")    else      @siren.bitmap = Cache.picture("led_off")    end    @siren.x = 5    @siren.y = 1    @siren.z = 300        @time_rem.bitmap = Cache.picture("time_remaining")    @time_rem.x = 168    @time_rem.y = 3    @time_rem.z = 300  end        def update    refresh    super  end   def terminate    super()  endend class Scene_Map < Scene_Base  alias DIMHUD_start start  def start    DIMHUD_start()    @hud = Window_HUD.new  end    alias DIMHUD_update update  def update    DIMHUD_update()    if $game_switches[82] == 1      @hud.update    end  endend
when you set the bitmap, use a dup from the actual one loaded by the Cache.
I'm sorry, I don't follow.  How would do I do that?  Would I make a dummy bitmap and copy from it's cache or...?
For example, something like this:

@status_bar_san_bg.bitmap = Cache.picture("status_bar_san_bg").dupThat way, you can clear or do whatever you want with the bitmap without changing the original one that is being loaded by the Cache.

Try that with all the other ones.
EDIT 3: Resolved all outstanding issues except for one:  I have no idea how I'm going to do a graphical gauge.  I assume I have to do one because I can't "draw over" my graphics.  I can display them, I just don't know how to make it cut/mask the graphic.
1. Create a Bitmap objetct for the sprite that you want to use for the gauge, with the full dimensions of the filled bar. For example: Bitmap.new(120, 20) and then create a method to update the bar based on the values you want it to measure (and to only redraw the bar if the values changed from the ones that the bar is displaying).

2. That method should take the bitmap you want the bar to be filled with, the actual image file, like: bar_bitmap = Cache.picture('hp_bar') and use its width (or height if you want to have it vertical) to calculate the percentage of the bar that will be visible based on the value that it will represent.

3. Once you have that, you use the blt method of the bitmap to "paste" bar_bitmap to the blank bitmap that you created before, setting the percentage in the Rect object that you pass when calling blt (width or height, depending on how you want it to look).
Hm.  If I make it with Sprite.new() it gives a NoMethodError on (...).bitmap.new  (telling me 'new' is undefined).

If I make the bitmap without the Sprite it works -- but then it doesn't have the X,Y,Z,etc. from Sprite.  What am I doing wrong?

The code for the status bar so far ( non-directly-related bits removed )

@status_bar_san_fg = Sprite.new()

@status_bar_san_fg.bitmap.new(138,6)

@status_bar_san_fg.bitmap.blt(  @status_bar_san_fg.x,  @status_bar_san_fg.y,  @status_bar_san_fg_src.rect(  @status_bar_san_fg_src.x,  @status_bar_san_fg_src.y,  138*(@current_val/60),  6  )  )
Your code should be like this:

Code:
@status_bar_san_fg = Sprite.new@status_bar_san_fg.bitmap = Bitmap.new(138, 6)
That fixes that error, yes.  Now I just have to figure out how to do that rect properly.  It seems there's no way for me to do it in-line without method errors that I can think of.

Thank you for all your help so far, Lemony.  I'll be sure to place you in my credits under Special Thanks.
Not to detract from the original asked question, but the script as a whole could use work. As it is laid out in the original post, you wouldn't even need Bitmap#clear (you could simply load a blank bitmap or nil out the bitmap, or simply set the sprite.visible to false so it won't be displaying the wrong bitmap). You only need to clear a bitmap if you are drawing in it and need to start over, like erasing a whiteboard. You can start with an underlying image, as stated, by duping it from the cache, but that doesn't look like what you are doing here.


There are a number of other issues as well, if you would like to discuss.
I'm basically learning as I go here so I'm not surprised there are a plethora of errors.  I've tightened it up a bit since the original posting but it's still over-verbose and over-complicated I'd imagine.

I'd post it here but

Spoiler
doesn't seem to work and it's grown to 331 lines in length.  I'll host it on hastebin so you can see the current ver.  I'm open to suggestions and improvements.
http://hastebin.com/wugenatova.rb

Edit: Still no joy on figuring out how to tell it to copy from the source image.  I have the documentation open and I'm staring at it but I just don't see how I'm supposed to fit in those calculations into the call.  I'm trying to get this down on my own and just puzzle it out but either the documentation is failing to mention that I can approach it in a different manner than what I'm seeing or my lack of knowledge is not permitting me to see the route RGSS3 wants me to take.
Above all, it is 'whatever works for you'. I'll just give some general suggestions on how to have things better organized which should help with debugging.


1. Consider shortening your methods and delegating to different methods. Some of those methods extend across several pages. Whenever you have a huge block of if statements, it would probably be a good idea to have that be its own method. Reducing the amount of if statement nesting would also be a good idea.


2. Consider tightening your use of global variables to the absolutely necessary. If you think it only doesn't work because your scope is too narrow, there is probably another reason you are overlooking that it does not work. $spawned should not be needed here. $dim_hud should be an instance variable for the Scene instead.


3. update should contain the methods to be executed every frame. refresh should contain the methods needed to redraw whatever elements need to be redrawn to get it to match the current data. You should not need to call refresh every frame. Since this 'window' is acting more like a 'spriteset', you should preferably have different refresh methods for the individual elements that would need refreshing, and call them conditionally from update when they need to be.


4. Important. Don't forget to dispose all your sprites. You need a dispose method for your custom window in which you clean up all your sprites. The window itself will be disposed automatically iff it is created as an instance variable within the scene.


5. You are using 'if (var) == true' in several locations, where you can just use 'if (var)' which translates to 'if (var) is anything but nil or false'. In cases where you are using 'if (var) == false' or 'if (var).nil?', these can be replaced with 'unless (var)', which means 'unless (var) is any value other than nil or false'. Values of 1 and 0 both evaluate as 'true' in Ruby.


6. You no longer need to dup bitmaps if you do not intend to clear/draw in them. This is just creating several orphaned bitmaps that will cause your script to drag.


Edit:


For blt, in more explicit terms:


target_bitmap.blt(destination_x_within_target, destination_y_within_target, source_bitmap_to_be_copied_from, rect_object_representing_area_within_source_bitmap_to_copy [, opacity_at_which_to_copy])
Hm.  I'm trying that but it just keeps telling me that rect is undefined.  Is this not right?

#Sanity Bar Processing

    @target_box = rect.new(0,0,138*(@current_val/60),6)

    @status_bar_san_fg.blt(0,0,@status_bar_san_fg_src.bitmap, @target_box)

 

Edit: Oh blast it.  It was just a capitalization error.  rect is nothing, Rect is the object class.

Now I just need to figure out how to get it off from 0,0 and on to 76,32
It would be Rect.new, not rect.new. Case matters. Speaking of, constants should start with caps, you have that down, but methods should always start with lowercase. You have aliases that aren't.


Also, you don't really even need to draw a new bar every time here if it is going to be a different sprite. You could simply modify the src_rect of the sprite so it only shows as much of the bar as you want.


Eg., after loading your 'bar' bitmap into sprite @status_bar_san

Code:
@status_bar_san.src_rect.width = 138*(@current_val/60)
Which would just update the width of the bar to where it should be (based on the code you have supplied), rather than all that redraw business.
It seems like that should work -- and when I give it a width of rand(138) it fluctuates madly as I would expect it to.

However, 138-138*(@current_val/60) gives me a permanently full bar.  

Assuming a value of 15 it should work out to 103.5 -- but when I print it out it equals 138?

I can even get RGSS to do the math with me.  It follows me at every step -- except the result anomalously is still 138 when taken as a formula.

In either case, the FG bar doesn't seem to work
Ruby drops any remainder in integer math. You'd need to introduce a float, eg.,

Code:
138 * (@current_val/60.0)
That way, the result can be a float.
Aye, that was it.  Whew!  It's a big relief now that it's working properly.  You guys are great.

I'll make sure to put you in the Special Thanks as well, Mithran.

Having this thing will help me a lot moving forward with the game.  It's much more helpful to see the variable moving in realtime than to have to check it in debug ever so often.

finally_working.gif

Original thread: https://forums.rpgmakerweb.com/threads/at-wits-end-with-bitmap-clear.21907/