[VX] Trying to keep sprites "alive" in the background during Menu call

Hey there.


I'm having a bit of trouble figuring out if it's possible / how to keep a Sprite existing in the background while the game menu is open.


For a bit of context, I'm using an ABS (doesn't matter which one) and when an enemy dies I want to create a blood graphic on the ground where it died.


I have written a script (will paste it below), and it WORKS, except that when you open the menu it causes the dreaded GOBJ crash. In order to fix this, it seems I have to dispose the blood sprites when the menu call happens, however this makes the blood graphic disappear before the menu opens and reappear afterwards, a very noticeable problem in my opinion.


This can't be only solution, though, considering things like the floating damage text from the ABS doesn't disappear when the menu opens (it remains graphically frozen in the background, like the rest of the game), and only disappears when the menu is closed (this is fine). I would be happy if the blood graphic had a short flicker when the menu closes or something, but having it fully disappear while the menu remains open and then pop back into existence when it closes is not acceptable.


Anyone have any idea how to make this work?


Thanks in advance for any help you can provide.


-eldorno


Script in the spoiler below.


Should be able to call `$blood_system.add_blood(x,y)` to add a blood graphic at the x,y coordinates (tile/event coords, not pixel).


BLOOD_CHARACTER_SPRITES should be located inside the Graphics/System/ directory..

Spoiler
Code:
BLOOD_CHARACTER_SPRITES = [
  "Blood/Blood1",
  "Blood/Blood2",
  "Blood/Blood3",
  "Blood/Blood4"
]


#-----------------------------------------------------------------------------#
# Maintains a list of blood objects and controls updating and disposal        #
#-----------------------------------------------------------------------------#
class Blood_System
  def initialize
    @bloods = []
  end
  
  def update
    for blood in @bloods
      blood.update
    end
  end
  
  def dispose_blood_sprites
    for blood in @bloods
      blood.dispose_sprite
    end
  end
  
  def clear_blood_sprites
    dispose_blood_sprites
    @bloods = []
  end
  
  def add_blood(x, y)
    @bloods.push(Requiem_Blood.new(x, y))
  end
end


#-----------------------------------------------------------------------------#
# Holds xy coordinates, a bitmap, and manages a sprite                        #
#-----------------------------------------------------------------------------#
class Requiem_Blood
  def initialize(x, y)
    @x = x
    @y = y
    @bmp = BLOOD_CHARACTER_SPRITES[rand(4)]
  end
    
  def update
    create_sprite if @sprite.nil? or @sprite.disposed?
    @sprite.update
  end
   
  def create_sprite
    @sprite = Sprite_Blood.new($scene.get_viewport, @x, @y, @bmp)
  end
  
  def dispose_sprite
    @sprite.bitmap.dispose
    @sprite.dispose
  end
end


#-----------------------------------------------------------------------------#
# Blood sprite objects, drawn at x,y coordinates in the game world            #
#-----------------------------------------------------------------------------#
class Sprite_Blood < Sprite
  # extend existing method
  alias requiem_blood_sprite_initialize initialize
  def initialize(viewport, x, y, bmp)
    requiem_blood_sprite_initialize(viewport)
    @char_x = x * 32
    @char_y = y * 32
    self.bitmap = Cache.system(bmp)
    self.x = x
    self.y = y
  end
  
  # extend existing method
  alias requiem_blood_sprite_update update
  def update
    requiem_blood_sprite_update
    self.x = @char_x - 8
    self.y = @char_y - 8
    self.x -= ($game_map.display_x / 8)
    self.y -= ($game_map.display_y / 8)
  end
end


#-----------------------------------------------------------------------------#
# Update the blood system object every frame                                  #
#-----------------------------------------------------------------------------#
class Game_Map
  alias requiem_blood_game_map_update update
  def update
    requiem_blood_game_map_update
    $blood_system.update
  end
end


#-----------------------------------------------------------------------------#
# Get the viewport that draws sprites "Below Character"                       #
#-----------------------------------------------------------------------------#
class Scene_Map < Scene_Base
  # new method
  def get_viewport
    return @spriteset.get_viewport
  end
end

class Spriteset_Map
  # new method
  def get_viewport
    return @viewport1
  end
end


#-----------------------------------------------------------------------------#
# Dispose blood sprites on menu call                                          #
#-----------------------------------------------------------------------------#
class Scene_Map < Scene_Base
  # extend existing method
  alias requiem_blood_scene_map_call_menu call_menu
  def call_menu
    $blood_system.dispose_blood_sprites
    requiem_blood_scene_map_call_menu
  end
end


#-----------------------------------------------------------------------------#
# Fully dispose and clear blood objects on transfer                           #
#-----------------------------------------------------------------------------#
class Game_Player < Game_Character
  # extend existing method
  alias requiem_blood_game_player_perform_transfer perform_transfer
  def perform_transfer
    $blood_system.clear_blood_sprites
    requiem_blood_game_player_perform_transfer
  end
end


#-----------------------------------------------------------------------------#
# Create a global $blood_system object on startup                             #
#-----------------------------------------------------------------------------#
class Scene_Title < Scene_Base
  alias requiem_blood_scene_title_cgo create_game_objects
  def create_game_objects
    requiem_blood_scene_title_cgo
    $blood_system = Blood_System.new
  end
end
Which maker are you using in this case?


Your profile claims that you're using VX Ace, but the title of the topic claims you're using the only VX


And that makes a difference as VX uses RGSS2 and VXA uses RGSS3, in addition to having different tileset and database structures...
Which maker are you using in this case?


Your profile claims that you're using VX Ace, but the title of the topic claims you're using the only VX


And that makes a difference as VX uses RGSS2 and VXA uses RGSS3, in addition to having different tileset and database structures...

It's VX - I'm revisiting one of my very old projects, trying to clean it up and add some wishlist features now that I have more time and experience.
I am not familiar with RGSS2, but if it is written the same way as RGSS3, you should call the dispose method in the dispose_all_windows method in the scene instead.


This ensures that the sprites are disposed before their viewport (avoiding the random crash), and it happens after the background mapshot is taken, so the sprites will be visible in the menu background.


If there is no such method in VX (and I have a slight suspicion it doesn't, seeing that windows need to be disposed manually there), search the place where the windows of the scene are disposed, and dispose your sprites there.


The method where you chose to dispose your sprites happens before the snapshot is taken for the menu background, that is why you can't see them in the menu.
I am not familiar with RGSS2, but if it is written the same way as RGSS3, you should call the dispose method in the dispose_all_windows method in the scene instead.


This ensures that the sprites are disposed before their viewport (avoiding the random crash), and it happens after the background mapshot is taken, so the sprites will be visible in the menu background.


If there is no such method in VX (and I have a slight suspicion it doesn't, seeing that windows need to be disposed manually there), search the place where the windows of the scene are disposed, and dispose your sprites there.


The method where you chose to dispose your sprites happens before the snapshot is taken for the menu background, that is why you can't see them in the menu.



Aaahh.. It hadn't occurred to me that the background was a snapshot and not just frozen/non-updating objects. I'll definitely look into this in a couple of hours when I'm free.


Thanks!


-- edit --


Thanks, Sixth, this solved my issue!


Moving the dispose from Scene_Map.call_menu to Scene_Map.terminate left the blood graphics in the menu background instead of removing them early. Awesome!



It does disappear for a short moment when the menu closes, but as I said the original post, this is at least tolerable.


Thanks again ^^,


-- edit 2 --


Adding a $blood_system.update to the Scene_Map.start function resolved the blood-disappearing-for-a-moment issue, too.


I might post the completed script once I'm finished, if anyone is interested.

Original thread: https://forums.rpgmakerweb.com/threads/vx-trying-to-keep-sprites-alive-in-the-background-during-menu-call.71650/