[XP] Scene_File modified

Started by Wecoc, July 06, 2014, 10:53:12 am

Previous topic - Next topic

Wecoc

Scene_File modified
Authors: Wecoc
Version: 1.0
Type: Load / Save System
Key Term: Title / Save / Load / GameOver Add-on



Introduction

Simple modification of Scene_File which shows file windows in a different way so you can use more, and display more information in each one.


Features


  • Easy configurable

  • Displays the chapter, time, gold, map name...




Screenshots

Spoiler: ShowHide




Script

Put over main the entire code:

Spoiler: ShowHide

#==============================================================================
# ** Scene_File modified
#------------------------------------------------------------------------------
#  Author: Wecoc
#==============================================================================

class Window_SaveFile < Window_Base
  attr_reader   :filename
 
  def initialize(file_index, filename)
    super(160, 64, 480, 480 - 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    @file_index = file_index
    @filename = "Save#{@file_index + 1}.rxdata"
    @time_stamp = Time.at(0)
    @file_exist = FileTest.exist?(@filename)
    if @file_exist
      file = File.open(@filename, "r")
      @time_stamp         = file.mtime
      @characters         = Marshal.load(file)
      @frame_count        = Marshal.load(file)
      @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)
      @total_sec = @frame_count / Graphics.frame_rate
      file.close
    end
    refresh
  end

  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    if @file_exist
      for i in 0...@characters.size
        bitmap = RPG::Cache.character(@characters[i][0], @characters[i][1])
        cw = bitmap.rect.width / 4
        ch = bitmap.rect.height / 4
        src_rect = Rect.new(0, 0, cw, ch)
        x = 160 - @characters.size * 32 + i * 100 - cw / 2
        # Character
        self.contents.blt(x + 24, 72 - ch, bitmap, src_rect)
        # Name
        draw_actor_name(@game_party.actors[i], x, 76)
        # Level
        draw_actor_level(@game_party.actors[i], x, 100)
        # State
        draw_actor_state(@game_party.actors[i], x, 124)
      end
     
      # Playing time
      hour = @total_sec / 60 / 60
      min = @total_sec / 60 % 60
      sec = @total_sec % 60
      time_string = sprintf("%02d:%02d:%02d", hour, min, sec)
      self.contents.font.color = normal_color
      self.contents.draw_text(128, 176, 300, 32, time_string, 2)
     
      # Date when saving
      self.contents.font.color = normal_color
      time_string = @time_stamp.strftime("%d/%m/%Y %H:%M")
      self.contents.draw_text(128, 200, 300, 32, time_string, 2)
     
      # Gold
      cx = contents.text_size($data_system.words.gold).width
      self.contents.font.color = normal_color
      self.contents.draw_text(308, 224, 120-cx-2, 32, @game_party.gold.to_s, 2)
      self.contents.font.color = system_color
      self.contents.draw_text(428-cx, 224, cx, 32, $data_system.words.gold, 2)
     
      # Chapter (variable 1)
      self.contents.font.color = system_color
      self.contents.draw_text(4, 176, 120, 32, "Chapter")
      self.contents.font.color = normal_color
      self.contents.draw_text(100, 176, 48, 32, @game_variables[1].to_s)
     
      # Map name
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 200, 320, 32, @game_map.map_name)
     
      # Map description
      self.contents.font.color = system_color
      self.contents.draw_text(4, 272, 320, 32, "Map description")
      self.contents.font.italic = true
      self.contents.font.color = normal_color
      self.contents.font.size -= 2
      for i in 0...@game_map.map_def.size
        self.contents.draw_text(4, 300 + 24*i, 480, 32, @game_map.map_def[i])
      end
      self.contents.font.italic = false
      self.contents.font.size += 2
    end
  end
end

class Game_Map
 
attr_reader   :map_id 
  def map_name
    @mpn = load_data("Data/MapInfos.rxdata")
   return @mpn[@map_id].name
end

  def map_def
    case @map_id
    when 1
      # Example
      return ["A map with anything on it, just grass and the",
              "f*king player alone.",
              "This is just an example"]
    else
      return [""]
    end
  end
end

class Scene_File
 
  FILE_SIZE = 12   # File Size
 
  def main
    @help_window = Window_Help.new
    @help_window.set_text(@help_text)
   
    command_text = "File "
    command_array = []
    for i in 0...FILE_SIZE
      command_array.push("#{command_text + (i+1).to_s}")
    end
    @command_window = Window_Command.new(160, command_array)
    @command_window.y = 64
    @command_window.height = 480 - 64
    @file_index = $game_temp.last_file_index
    @command_window.index = @file_index
   
    @savefile_windows = []
    for i in 0...FILE_SIZE
      @savefile_windows.push(Window_SaveFile.new(i, make_filename(i)))
      @savefile_windows[i].visible = false
    end
   
    @savefile_windows[@file_index].visible = true
   
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @help_window.dispose
    @command_window.dispose
    for i in @savefile_windows
      i.dispose
    end
  end

  def update
    @help_window.update
    @command_window.update
    for i in 0...@savefile_windows.size
      @savefile_windows[i].visible = (i == @file_index)
    end
   
    if Input.trigger?(Input::C)
      on_decision(make_filename(@file_index))
      $game_temp.last_file_index = @file_index
      return
    end

    if Input.trigger?(Input::B)
      on_cancel
      return
    end

    if Input.repeat?(Input::DOWN)
      if Input.trigger?(Input::DOWN) or @file_index < (FILE_SIZE-1)
        $game_system.se_play($data_system.cursor_se)
        @file_index = (@file_index + 1) % FILE_SIZE
        return
      end
    end
    if Input.repeat?(Input::UP)
      if Input.trigger?(Input::UP) or @file_index > 0
        $game_system.se_play($data_system.cursor_se)
        @file_index = (@file_index + (FILE_SIZE-1)) % FILE_SIZE
        return
      end
    end
  end
end



Instructions

You can change the max number of files on this line:

FILE_SIZE = 12

Squall Lionheart

Hi,
I'm using your script, it's really usefull for my project.

But I have a problem. I modified it : instead of chapter stocked in a game_variable,
I use game_switches to display a text about story

In class Game_Map of your script I added
def map_def
if $game_switches[5]== true #20
      return ["text1"]
    else
      return ["text2"]
    end
    end
end

then it tells me script error lign 112 (if game_switches[5]...) undefined method for '[]' in nil:NilClass
when I try to load a file
but there's no problem when I save

Please help

KK20

Unfortunately, that logic won't work. From what I understand, the error takes place in the File scene. It is drawing all the saved file contents (actors in party, map, map description, etc.) based on the file's saved data.

You are using $game_switches, which is only created and initialized when starting or continuing a game. If you run your game and immediately go to the Continue option on the title screen, $game_switches doesn't exist in the game's memory yet. In other words, $game_switches = nil. Hence why the error says "NilClass" (there is no such thing as a method nil[], which is why it says "undefined method for []").
Not to mention, even if it doesn't error when you start a new game and then immediately go to save it, you're accessing the wrong Game_Switches. You want the Game_Switches from the save files, but your code is accessing the currently running game's Game_Switches.

So what you need to do is pass the saved data's Game_Switches variable to Game_Map#map_def. Fortunately, the Game_Switches data is loaded into @game_switches in Window_SaveFile.

Find the lines where it draws the map_def and add the (@game_switches), like so:

      for i in 0...@game_map.map_def(@game_switches).size
        self.contents.draw_text(4, 300 + 24*i, 480, 32, @game_map.map_def(@game_switches)[i])
      end

Then, go to the map_def method and make these changes to your code:
Code: ruby

  def map_def(game_switches)
    if game_switches[5]  #<=== the "== true" is not really necessary, but if it helps you understand the code faster, then add it
      return ['text1']
    else
      return ['text2']
    end
  end

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Squall Lionheart

March 23, 2015, 12:26:03 pm #3 Last Edit: March 23, 2015, 12:27:13 pm by Squall Lionheart
Thank you so much for your quick answer
I perfectly understood the matter with your explanations
I made the changes but it still doesn't work

here's my modified code
#==============================================================================
# ** Scene_File modified
#------------------------------------------------------------------------------
#  Author: Wecoc
#==============================================================================

class Window_SaveFile < Window_Base
  attr_reader   :filename
 
  def initialize(file_index, filename)
    super(160, 64, 480, 480 - 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    @file_index = file_index
    @filename = "Save#{@file_index + 1}.rxdata"
    @time_stamp = Time.at(0)
    @file_exist = FileTest.exist?(@filename)
    if @file_exist
      file = File.open(@filename, "r")
      @time_stamp         = file.mtime
      @characters         = Marshal.load(file)
      @frame_count        = Marshal.load(file)
      @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)
      @total_sec = @frame_count / Graphics.frame_rate
      file.close
    end
    refresh
  end

    def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    if @file_exist
      for i in 0...@characters.size
        bitmap = RPG::Cache.character(@characters[i][0], @characters[i][1])
        cw = bitmap.rect.width / 4
        ch = bitmap.rect.height / 4
        src_rect = Rect.new(0, 0, cw, ch)
        x = 160 - @characters.size * 32 + i * 100 - cw / 2
        # Character
        self.contents.blt(x + 24, 72 - ch, bitmap, src_rect)
        # Name
        draw_actor_name(@game_party.actors[i], x, 76)
        # Level
        draw_actor_level(@game_party.actors[i], x, 100)
        # State
        draw_actor_state(@game_party.actors[i], x, 124)
      end
     
      # Playing time
      hour = @total_sec / 60 / 60
      min = @total_sec / 60 % 60
      sec = @total_sec % 60
      time_string = sprintf("%02d:%02d:%02d", hour, min, sec)
      self.contents.font.color = normal_color
      self.contents.draw_text(128, 176, 300, 32, time_string, 2)
     
      # Date when saving
      self.contents.font.color = normal_color
      time_string = @time_stamp.strftime("%d/%m/%Y %H:%M")
      self.contents.draw_text(128, 200, 300, 32, time_string, 2)
     
      # Gold
      cx = contents.text_size($data_system.words.gold).width
      self.contents.font.color = normal_color
      self.contents.draw_text(308, 224, 120-cx-2, 32, @game_party.gold.to_s, 2)
      self.contents.font.color = system_color
      self.contents.draw_text(428-cx, 224, cx, 32, $data_system.words.gold, 2)
     
      # Chapter (variable 1)
      #self.contents.font.color = system_color
      #self.contents.draw_text(4, 176, 120, 32, "Chapter")
      #self.contents.font.color = normal_color
      #self.contents.draw_text(100, 176, 48, 32, @game_variables[1].to_s)
     
      # Map name
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 200, 320, 32, @game_map.map_name)
     
      # Map description
      self.contents.font.color = system_color
      self.contents.draw_text(4, 272, 320, 32, "Que faire ?")
      self.contents.font.italic = true
      self.contents.font.color = normal_color
      self.contents.font.size -= 2
      for i in 0...@game_map.map_def(@game_switches).size
        self.contents.draw_text(4, 300 + 24*i, 480, 32, @game_map.map_def(@game_switches)[i])
      end
      self.contents.font.italic = false
      self.contents.font.size += 2
    end
  end
end

class Game_Map
 
attr_reader   :map_id 
  def map_name
    @mpn = load_data("Data/MapInfos.rxdata")
   return @mpn[@map_id].name
end

  def map_def(game_switches)
  if $game_switches[187]== true #1
      return ["J'ai trouvé la clé du gros portail du manoir.",
      "Il faut retourner là-bas, mais ce sera dangereux.",
      "Méta-Yolanda, les servantes et les chiens de Rodrigue",
      "me pousuivent, et il y a des monstres partout."]
    else#1
    if $game_switches[164]== true #2
      return ["Galicien est un savant-fou à l'origine de tout ce",
      "qui se passe ici. Je l'ai éliminé. Sielle est apparue",
      "et s'est transformée en Méta-Yolanda. Je dois fuir !"]
    else#2
    if $game_switches[154]== true #3
      return ["Je suis enfin sortie du cimetierre et me voilà",
      "à devoir explorer tout le village de Ste Yolanda.",
      "J'ai toujours mal à ma jambe, il faut me soigner,",
      "sinon je ne pourrais pas fuir devant un danger."]
    else#3
    if $game_switches[145]== true #4
      return ["J'ai fait une immense chute et je suis blessée.",
      "Je suis dans une sorte de crypte.",
      "Il faut que je sorte de cet endroit lugubre."]
    else#4
    if $game_switches[146]== true #5
      return ["L'ange Sielle est apparue. C'est une chasseuse d'âmes.",
      "Elle a voulu me tuer pour récupérer la mienne.",
      "J'ai trouvé son auréole. Cela ressemble à un rouage..."]
    else#5
    if $game_switches[137]== true #6
      return ["Le brasero a allumé les chandeliers du choeur",
      "Je devrais essayer d'allumer toutes les bougies.",
      "Peut-être que ça fera encore une chose étrange, qui sait ?",
      "Il y a des allumettes dans la réserve du monastère."]
    else#6
    if $game_switches[143]== true #7
      return ["J'ai trouvé un levier. Il pourrait m'aider à bouger",
      "certains obstacles."]
    else#7
    if $game_switches[187]== true #8
      return ["J'ai tué Rodrigue, j'y étais obligée.",
      "Il avait la clé de l'entrée du monastère.",
      "Il faut rester prudente, ses chiens rôdent toujours."]
    else#8
    if $game_switches[112]== true #9
      return ["Les dortoirs sont la dernière pièce à explorer."]
    else#9
    if $game_switches[97]== true #10
      return ["Sielle est un ange qui cherche à me tuer.",
      "Elle contrôle les servantes et Rodrigue, le gardien.",
      "Lui et ses chiens sont à ma poursuite, il faut fuir."]
    else#10
    if $game_switches[111]== true #11
      return ["J'ai trouvé la clé de la bibliothèque.",
      "Il doit y avoir des choses intéressantes là-bas."]
    else#11
    if $game_switches[103]== true #12
      return ["J'ai trouvé la clé de la cuisine du monastère.",
      "Je vais pouvoir explorer cette partie.",
      "Mais restons sur nos gardes, on ne sait pas ce qu'on va trouver."]
    else#12
    if $game_switches[89]== true #13
      return ["Quelqu'un contrôle les servantes avec un pouvoir étrange.",
      "On m'a livrée à l'une d'elles. Je dois fuir vers ce nouveau",
      "bâtiment. On dirait un monastère"]
    else#13
    if $game_switches[80]== true #14
      return ["Il y avait un animal mort, j'ai récupéré de sa viande.",
      "Peut-être que ça pourra faire distraction pour le chien",
      "qui garde la cuisine de la ferme."]
    else#14
    if $game_switches[75]== true #15
      return ["J'ai tué une des servantes. Elle avait la clé",
      "de l'antichambre. Je vais voir ce qui se cache derrière."]
    else#15
    if $game_switches[63]== true #16
      return ["Je peux désormais ouvrir la plus grande salle",
      "de cet endroit souterrain. Méfiance, je ne sais pas",
      "ce que je vais y découvrir."]
    else#16
    if $game_switches[53]== true #17
      return ["J'ai découvert un nouvel ange. Il a été attaqué",
      "par la servante du manoir. Elle me réservera le même sort.",
      "J'ai la clé de la grille du souterrain."]
    else#17
    if $game_switches[23]== true #18
      return ["J'ai trouvé un piolet. Il pourra m'aider",
      "à défoncer la caisse qui bloque les escaliers",
      "sur le palier du manoir."]
    else#18
    if $game_switches[15]== true #19
      return ["J'ai accès à la cuisine.",
      "Je dois explorer cette nouvelle partie du manoir."]
    else#19
    if $game_switches[5]== true #20
      return ["Je me suis réveillée dans un manoir abandonné.",
      "Je dois l'explorer et trouver une issue.",
      "Des anges sont là pour me protèger, il faut les trouver."]
    else#20
      return ["Je me suis réveillée dans un manoir abandonné.",
      "Je dois l'explorer et trouver une issue."]
    end#20
    end#19
    end#18
    end#17
    end#16
    end#15
    end#14
    end#13
    end#12
    end#11
    end#10
    end#9
    end#8
    end#7
    end#6
    end#5
    end#4
    end#3
    end#2
  end#1

  end
end

class Scene_File
 
  FILE_SIZE = 12   # File Size
 
  def main
    @help_window = Window_Help.new
    @help_window.set_text(@help_text)
   
    command_text = "Cauchemar "
    command_array = []
    for i in 0...FILE_SIZE
      command_array.push("#{command_text + (i+1).to_s}")
    end
    @command_window = Window_Command.new(160, command_array)
    @command_window.y = 64
    @command_window.height = 480 - 64
    @file_index = $game_temp.last_file_index
    @command_window.index = @file_index
   
    @savefile_windows = []
    for i in 0...FILE_SIZE
      @savefile_windows.push(Window_SaveFile.new(i, make_filename(i)))
      @savefile_windows[i].visible = false
    end
   
    @savefile_windows[@file_index].visible = true
   
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @help_window.dispose
    @command_window.dispose
    for i in @savefile_windows
      i.dispose
    end
  end

  def update
    @help_window.update
    @command_window.update
    for i in 0...@savefile_windows.size
      @savefile_windows[i].visible = (i == @file_index)
    end
   
    if Input.trigger?(Input::C)
      on_decision(make_filename(@file_index))
      $game_temp.last_file_index = @file_index
      return
    end

    if Input.trigger?(Input::B)
      on_cancel
      return
    end

    if Input.repeat?(Input::DOWN)
      if Input.trigger?(Input::DOWN) or @file_index < (FILE_SIZE-1)
        $game_system.se_play($data_system.cursor_se)
        @file_index = (@file_index + 1) % FILE_SIZE
        return
      end
    end
    if Input.repeat?(Input::UP)
      if Input.trigger?(Input::UP) or @file_index > 0
        $game_system.se_play($data_system.cursor_se)
        @file_index = (@file_index + (FILE_SIZE-1)) % FILE_SIZE
        return
      end
    end
  end
end

KK20

March 23, 2015, 12:32:42 pm #4 Last Edit: March 23, 2015, 01:23:08 pm by KK20
Don't put dollar signs ($) in front of game_switches in map_def. My previous post showed exactly that.
Also, that if-else block is ridiculous. You should use the elsif keyword instead.

    if game_switches[187] == true #1
      return ["J'ai trouvé la clé du gros portail du manoir.",
      "Il faut retourner là-bas, mais ce sera dangereux.",
      "Méta-Yolanda, les servantes et les chiens de Rodrigue",
      "me pousuivent, et il y a des monstres partout."]
    elsif game_switches[164] == true #2
      return ["Galicien est un savant-fou à l'origine de tout ce",
      "qui se passe ici. Je l'ai éliminé. Sielle est apparue",
      "et s'est transformée en Méta-Yolanda. Je dois fuir !"]
    elsif game_switches[154] == true #3
      return ["Je suis enfin sortie du cimetierre et me voilà",
      "à devoir explorer tout le village de Ste Yolanda.",
      "J'ai toujours mal à ma jambe, il faut me soigner,",
      "sinon je ne pourrais pas fuir devant un danger."]
    end #<= Only needs one 'end'! Not the 20 that you currently have right now

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Squall Lionheart

Thank you !! It works !
Be sure I mention your name in my project ;)