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.

Turning off a script with a switch & Critical Error

Acera

Sponsor

1.
I'm currently using this script to create a HUD;
Code:
#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================
class Scene_Map
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------
  alias raz_hud_map_main main
  alias raz_hud_map_update update
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    @hud_sprite = Window_HUDFace.new
    raz_hud_map_main
    @hud_sprite.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    @hud_sprite.update
    raz_hud_map_update
  end
end

#==============================================================================
# ** Window_HUDFace
#------------------------------------------------------------------------------
#  This class draws the actor's face, his HP and SP
#==============================================================================
class Window_HUDFace < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(-20, -10, 320, 160)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 0
    @actor = $game_party.actors[0]
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @old_hp = @actor.hp
    @old_sp = @actor.sp
    bitmap = RPG::Cache.picture("HUD1")
    self.contents.blt(-20, -10, bitmap, Rect.new(-20, -10, bitmap.width, bitmap.height))
    draw_bar(60, 17, @actor.hp, @actor.maxhp, 135, 8, Color.new(150, 0, 0, 255), Color.new(255, 0, 0, 255))
    draw_bar(60, 33, @actor.sp, @actor.maxsp, 135, 8, Color.new(0, 0, 150, 255), Color.new(0, 0, 255, 255))
    self.contents.font.size = 18
    self.contents.font.italic = true
    self.contents.font.color = Color.new(0, 0, 0)
    self.contents.draw_text(48, -10, 150, 32, "#{@actor.name} Lvl #{@actor.level}") 
    self.contents.font.color = Color.new(255, 255, 255)
    self.contents.draw_text(50, -12, 150, 32, "#{@actor.name} Lvl #{@actor.level}")
    self.contents.font.color = Color.new(0, 0, 0)
    self.contents.draw_text(130, 11, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(132, 11, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(130, 9, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(132, 9, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(130, 27, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(132, 27, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(130, 25, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(132, 25, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.font.color = @actor.hp <= @actor.maxhp / 4 ? crisis_color : 
    @actor.hp == 0 ? knockout_color : normal_color
    self.contents.draw_text(131, 10, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.font.color = @actor.sp == 0 ? crisis_color : normal_color
    self.contents.draw_text(131, 26, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
  end


  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    if @old_hp != $game_party.actors[0].hp or
       @old_sp != $game_party.actors[0].sp
       refresh
       @old_hp = $game_party.actors[0].hp
       @old_sp = $game_party.actors[0].sp
     end
   end
   end

 
#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This class is for all in-game windows.
#==============================================================================
class Window_Base < Window
  #--------------------------------------------------------------------------
  # * Draw Slant Bar(by SephirothSpawn)
  #--------------------------------------------------------------------------
  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

But I'm also using a script to make the player start on a map, but this will show the HUD during the 'titlescreen'. I also like to have it turned off during certain cut-scenes and a few other (special) maps. So, I'd like to have a small edit of this so I can turn off the HUD with the use of a switch. I've tried quite some combinations and asked a friend to help me a bit, but I guess it takes more then a little;
Code:
if $game_switches[14] == true
script here
else
end

2.
The script I'm using for allowing me to start on a map is this one;
Code:
#==============================================================================
# ** Main
#------------------------------------------------------------------------------
#  After defining each class, actual processing begins here.
#==============================================================================


begin
 $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_troops        = load_data("Data/Troops.rxdata")
    $data_states        = load_data("Data/States.rxdata")
    $data_animations    = load_data("Data/Animations.rxdata")
    $data_tilesets      = load_data("Data/Tilesets.rxdata")
    $data_common_events = load_data("Data/CommonEvents.rxdata")
    $data_system        = load_data("Data/System.rxdata")
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_screen        = Game_Screen.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    $game_party.setup_starting_members
    $game_map.setup($data_system.start_map_id)
    $game_player.moveto($data_system.start_x, $data_system.start_y)
    $game_player.refresh
    $game_map.autoplay
    $game_map.update
    $scene = Scene_Map.new
  Graphics.freeze
  while $scene != nil
    $scene.main
  end
  Graphics.transition(20)
rescue Errno::ENOENT
  filename = $!.message.sub("No such file or directory - ", "")
  print("File #{filename} not found.")
end
On the starting map I'm using events to make an animated title screen. I sorta duplicated the New Game, Load Game and Quit, but whenever I press on Load Game I get an error (NoMethodError). I can call the Load Game scene, that's no problem. When I press Load Game I get to the loading screen, but when I press on Esc I want to have a switch to turn on. But this switch doesn't seem to get turned on at all. When I press on f9 to see if it did turn on (which it probably didn't, but anyway), I get an error; "Critical Error C0000005 at adress 00D27612". I just can't seem to get my finger on what's happening, the last time I had this I forgot to add a spriteset.dispose...
 
1. Basically add your switch inside the refresh block. So the code would be as follows:
Code:
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
  if $game_switches[14]
    self.contents.clear
    @old_hp = @actor.hp
    @old_sp = @actor.sp
    bitmap = RPG::Cache.picture("HUD1")
    self.contents.blt(-20, -10, bitmap, Rect.new(-20, -10, bitmap.width, bitmap.height))
    draw_bar(60, 17, @actor.hp, @actor.maxhp, 135, 8, Color.new(150, 0, 0, 255), Color.new(255, 0, 0, 255))
    draw_bar(60, 33, @actor.sp, @actor.maxsp, 135, 8, Color.new(0, 0, 150, 255), Color.new(0, 0, 255, 255))
    self.contents.font.size = 18
    self.contents.font.italic = true
    self.contents.font.color = Color.new(0, 0, 0)
    self.contents.draw_text(48, -10, 150, 32, "#{@actor.name} Lvl #{@actor.level}") 
    self.contents.font.color = Color.new(255, 255, 255)
    self.contents.draw_text(50, -12, 150, 32, "#{@actor.name} Lvl #{@actor.level}")
    self.contents.font.color = Color.new(0, 0, 0)
    self.contents.draw_text(130, 11, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(132, 11, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(130, 9, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(132, 9, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.draw_text(130, 27, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(132, 27, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(130, 25, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.draw_text(132, 25, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
    self.contents.font.color = @actor.hp <= @actor.maxhp / 4 ? crisis_color : 
    @actor.hp == 0 ? knockout_color : normal_color
    self.contents.draw_text(131, 10, 150, 32, "#{@actor.hp}/#{@actor.maxhp}")
    self.contents.font.color = @actor.sp == 0 ? crisis_color : normal_color
    self.contents.draw_text(131, 26, 150, 32, "#{@actor.sp}/#{@actor.maxsp}")
  end
end

2. Do I get to see your load script where this switch is supposed to be turned ON? Currently the details given won't lead me to a solution for this problem.
 

Acera

Sponsor

Apologies, I thought that would've been enough. I'll go into it on more detail.
When you start the game you will get a screen with some pictures (which move and such, bla bla bla). The most important event there is this common event:

http://img528.imageshack.us/img528/9758 ... ently4.png[/img]
This common event basically tells what you do when you press Enter (once your cursor is at the wanted screen)
once I press on Load Game (which is MenuLocatie == 2) I let that happen. I basically delete some pictures (which make up the pictures for New Game, Load Game, Quit and the cursor) and call for scene_load.

Once I press on Esc (to go back to the scene) I need a switch to be turned off (switch 15 to be exact). This way the menu will come back again so you can select something else. So, without that switch the menu will not come back (if I press Enter now I will get that Critical Error).

My Scene_Load is the standard one, with the exceptions of a few extremely minor changes.
Code:
#==============================================================================
# ** Scene_Load
#------------------------------------------------------------------------------
#  This class performs load screen processing.
#==============================================================================

class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    # Remake temporary object
    $game_temp = Game_Temp.new
    # Timestamp selects new file
    $game_temp.last_file_index = 0
    latest_time = Time.at(0)
    for i in 0..3
      filename = make_filename(i)
      if FileTest.exist?(filename)
        file = File.open(filename, "r")
        if file.mtime > latest_time
          latest_time = file.mtime
          $game_temp.last_file_index = i
        end
        file.close
      end
    end
    super("Which file would you like to load?")
  end
  #--------------------------------------------------------------------------
  # * Decision Processing
  #--------------------------------------------------------------------------
  def on_decision(filename)
    # If file doesn't exist
    unless FileTest.exist?(filename)
      # Play buzzer SE
      $game_system.se_play($data_system.buzzer_se)
      return
    end
    # Play load SE
    $game_system.se_play($data_system.load_se)
    # Read save data
    file = File.open(filename, "rb")
    read_save_data(file)
    file.close
    # Restore BGM and BGS
    $game_system.bgm_play($game_system.playing_bgm)
    $game_system.bgs_play($game_system.playing_bgs)
    # Update map (run parallel process event)
    $game_map.update
    # Switch to map screen
    $scene = Scene_Map.new
  end
  #--------------------------------------------------------------------------
  # * Cancel Processing
  #--------------------------------------------------------------------------
  def on_cancel
    # Play cancel SE
    $game_system.se_play($data_system.cancel_se)
    # Switch to title screen
    scene = Scene_Map.new
  end
  #--------------------------------------------------------------------------
  # * Read Save Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    # Read character data for drawing save file
    characters = Marshal.load(file)
    # Read frame count for measuring play time
    Graphics.frame_count = Marshal.load(file)
    # Read each type of game object
    $game_system        = Marshal.load(file)
    $game_switches      = Marshal.load(file)
    $game_variables     = Marshal.load(file)
    $game_self_switches = Marshal.load(file)
    $game_screen        = Marshal.load(file)
    $game_actors        = Marshal.load(file)
    $game_party         = Marshal.load(file)
    $game_troop         = Marshal.load(file)
    $game_map           = Marshal.load(file)
    $game_player        = Marshal.load(file)
    # If magic number is different from when saving
    # (if editing was added with editor)
    if $game_system.magic_number != $data_system.magic_number
      # Load map
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
    end
    # Refresh party members
    $game_party.refresh
  end
end

P.S. thanks a bunch for the first one, it works perfectly now *writes down a note for future reference*
 
I suppose what you're trying to do is to turn switch 15 ON when leaving Scene_Load? If I got that correctly, all you have to do, is replace the on_cancel block, into this:
Code:
  def on_cancel
    $game_switches[15] = true
    # Play cancel SE
    $game_system.se_play($data_system.cancel_se)
    # Switch to title screen
    $scene = Scene_Map.new # or whatever scene you want here
  end
If I understood that wrong, then I might have serious problems understanding what you are pointing out.

Hope this helps.
 

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