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.

save/load screen edit help

Status
Not open for further replies.
I changed the save/load screen so that it displays the map name instead of the time etc, but its erroring

This is the error:
undefined method `name' for nil:NilClass


this is the new code
Code:
      # Draw play 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(4, 8, 600, 32, time_string, 2)
      # Map Location
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 40, 600, 32, $game_map.name.to_s)
    end
 
This should get the map name in the PlayTime window:
#==============================================================================
# ** Window_PlayTime or MapName
#------------------------------------------------------------------------------
# This window displays play time on the menu screen.
#==============================================================================
# Defines $game_map.mapname
class Game_Map
attr_accessor :map_name
def mapname
map_infos = load_data("Data/MapInfos.rxdata")
@map_name = map_infos[@map_id].name

end
end
class Window_PlayTime < Window_Base
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(0, 0, 160, 96)
self.contents = Bitmap.new(width - 32, height - 32)
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.font.color = system_color
self.contents.draw_text(4, 0, 120, 32, "Play Time")
@total_sec = Graphics.frame_count / Graphics.frame_rate
hour = @total_sec / 60 / 60
min = @total_sec / 60 % 60
sec = @total_sec % 60
text = sprintf("%02d:%02d:%02d", hour, min, sec)
self.contents.font.color = normal_color
self.contents.draw_text(4, 32, 120, 32, text, 2)
#draw map name
self.contents.draw_text(x, y, 160, 120, $game_map.mapname, 3)# set x and y to your coordinates
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
if Graphics.frame_count / Graphics.frame_rate != @total_sec
refresh
end
end
end
But I can't get it to fit in there with the playtime.
This should replace the PlayTime window with a MapName window:
#==============================================================================
# ** Window_MapName
#------------------------------------------------------------------------------
# This window displays the map name on the menu screen.
#==============================================================================
# Define $game_map.mapname variable
class Game_Map
attr_accessor :map_name
def mapname
map_infos = load_data("Data/MapInfos.rxdata")
@map_name = map_infos[@map_id].name

end
end
class Window_PlayTime < Window_Base
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(0, 0, 160, 96)
self.contents = Bitmap.new(width - 32, height - 32)
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.font.color = system_color
self.contents.draw_text(4, 0, 120, 32, "Area")# you can change this to 'Map' or whatever
#draws map name
self.contents.font.color = normal_color
self.contents.draw_text(4, -8, 160, 120, $game_map.mapname, 0)# set x and y to your coordinates
end
# The number after $game_map.mapname, specifies alignment.
# I don't really know what number means what but it's only 0-3
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
if Graphics.frame_count / Graphics.frame_rate != @total_sec
refresh
end
end
end
Either of these would be replacing Window_PlayTime. Enjoy:)
By the way, I've based this on a script by someone's who's post I can't find that displays the map name on the map through spriting. All I did was use the $game_map.mapname variable and definition.

EDIT:: On second thought, you may have only wanted a little tweaking help and not for me to give you the whole thing sorry.
you need:
Code:
class Game_Map
attr_accessor :map_name 
def mapname 
map_infos = load_data("Data/MapInfos.rxdata") 
@map_name = map_infos[@map_id].name
above your script and call the map's name with
Code:
$game_map.mapname
as the text in a draw text command.
 
thats what i had before.

ITs fine if i save the file, then go back to title and load it, but it errors if i try to load it without playing the game first.

Im not 100% sure on the correct script to edit for the loading
 

Mac

Member

Would you mind posting the full script for it?

Code:
      # Draw play 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(4, 8, 600, 32, time_string, 2)
      # Map Location
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 40, 600, 32, $game_map.name.to_s)
    end

From what i see in this little bit is that there is no defining of the game map name global variable....please post the scripts you have for it and i will see what i can do.
 
whole script. its just an edit of the default window_save

Code:
#==============================================================================
# ** Window_SaveFile
#------------------------------------------------------------------------------
#  This window displays save files on the save and load screens.
#==============================================================================

class Window_SaveFile < Window_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :filename                 # file name
  attr_reader   :selected                 # selected
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     file_index : save file index (0-3)
  #     filename   : file name
  #--------------------------------------------------------------------------
  def initialize(file_index, filename)
    super(0, 64 + file_index % 4 * 104, 640, 104)
    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)
      @total_sec = @frame_count / Graphics.frame_rate
      file.close
    end
    refresh
    @selected = false
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    # Draw file number
    self.contents.font.color = normal_color
    name = "File#{@file_index + 1}"
    self.contents.draw_text(4, 0, 600, 32, name)
    @name_width = contents.text_size(name).width
    # If save file exists
    if @file_exist
      # Draw character
      for i in 0...@characters.size
        bitmap = RPG::Cache.picture(@characters[i][0])
        src_rect = Rect.new(0, 0, 80, 80)
        x = 75 + i * 90
        self.contents.blt(x, 1, bitmap, src_rect)
      end
      # Draw play 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(4, 8, 600, 32, time_string, 2)
      # Map Location
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 40, 600, 32, $game_map.name.to_s)
    end
  end
  #--------------------------------------------------------------------------
  # * Set Selected
  #     selected : new selected (true = selected, false = unselected)
  #--------------------------------------------------------------------------
  def selected=(selected)
    @selected = selected
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  # * Cursor Rectangle Update
  #--------------------------------------------------------------------------
  def update_cursor_rect
    if @selected
      self.cursor_rect.set(0, 0, @name_width + 8, 32)
    else
      self.cursor_rect.empty
    end
  end
end

class Game_Map
  #--------------------------------------------------------------------------
  # * Get Map Name
  #--------------------------------------------------------------------------
  def name
    load_data("Data/MapInfos.rxdata")[@map_id].name
  end 
end
 
Actually, I have same problem(though I don't mean to hijack this topic). Sometimes that works(especially modified save window alone), but when you use map name show window with that, sometimes it shows error. I always wondered why.
 

Mac

Member

Done!

Sorry it took so long i have lots on my hands atm, the problem was you needed an Attr Reader cause its refers to the data when saving but it isn't read when it comes to loading.

Code:
#==============================================================================
# ** Window_SaveFile
#------------------------------------------------------------------------------
#  This window displays save files on the save and load screens.
#==============================================================================

class Window_SaveFile < Window_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :filename                 # file name
  attr_reader   :selected    
  attr_reader   :map_name_id
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     file_index : save file index (0-3)
  #     filename   : file name
  #--------------------------------------------------------------------------
  def initialize(file_index, filename)
    super(0, 64 + file_index % 4 * 104, 640, 104)
    self.contents = Bitmap.new(width - 32, height - 32)
    @map_name_id = $game_map.name
    @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)
      @total_sec = @frame_count / Graphics.frame_rate
      file.close
    end
    refresh
    @selected = false
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    # Draw file number
    self.contents.font.color = normal_color
    name = "File#{@file_index + 1}"
    self.contents.draw_text(4, 0, 600, 32, name)
    @name_width = contents.text_size(name).width
    # If save file exists
    if @file_exist
      # Draw character
      for i in 0...@characters.size
        bitmap = RPG::Cache.picture(@characters[i][0])
        src_rect = Rect.new(0, 0, 80, 80)
        x = 75 + i * 90
        self.contents.blt(x, 1, bitmap, src_rect)
      end
      # Draw play 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(4, 8, 600, 32, time_string, 2)
      # Map Location
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 40, 600, 32, $game_map.name.to_s)
    end
    end
  end
  #--------------------------------------------------------------------------
  # * Set Selected
  #     selected : new selected (true = selected, false = unselected)
  #--------------------------------------------------------------------------
  def selected=(selected)
    @selected = selected
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  # * Cursor Rectangle Update
  #--------------------------------------------------------------------------
  def update_cursor_rect
    if @selected
      self.cursor_rect.set(0, 0, @name_width + 8, 32)
    else
      self.cursor_rect.empty
    end
  end

class Game_Map
  #--------------------------------------------------------------------------
  # * Get Map Name
  #--------------------------------------------------------------------------
  def name
    load_data("Data/MapInfos.rxdata")[@map_id].name
  end 
end
 

Mac

Member

Hmmm ahhh i see now, if you save then press F12 it will load it up with the location displayed but if you shut down and try it causes the same error...i'll look into this again...its cause it needs to read map.rxdata
 
This should work:
Code:
#==============================================================================
# ** Window_SaveFile
#------------------------------------------------------------------------------
#  This window displays save files on the save and load screens.
#==============================================================================

class Window_SaveFile < Window_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :filename                 # file name
  attr_reader   :selected                 # selected
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     file_index : save file index (0-3)
  #     filename   : file name
  #--------------------------------------------------------------------------
  def initialize(file_index, filename)
    super(0, 64 + file_index % 4 * 104, 640, 104)
    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)
      Marshal.load(file)
      Marshal.load(file)
      Marshal.load(file)
      Marshal.load(file)
      Marshal.load(file)
      @map = Marshal.load(file)
      @total_sec = @frame_count / Graphics.frame_rate
      file.close
    end
    refresh
    @selected = false
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    # Draw file number
    self.contents.font.color = normal_color
    name = "File#{@file_index + 1}"
    self.contents.draw_text(4, 0, 600, 32, name)
    @name_width = contents.text_size(name).width
    # If save file exists
    if @file_exist
      # Draw character
      for i in 0...@characters.size
        bitmap = RPG::Cache.picture(@characters[i][0])
        src_rect = Rect.new(0, 0, 80, 80)
        x = 75 + i * 90
        self.contents.blt(x, 1, bitmap, src_rect)
      end
      # Draw play 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(4, 8, 600, 32, time_string, 2)
      # Map Location
      self.contents.font.color = normal_color
      @map_name = load_data("Data/MapInfos.rxdata")[@map.map_id].name
      self.contents.draw_text(4, 40, 600, 32, @map_name)
    end
  end
  #--------------------------------------------------------------------------
  # * Set Selected
  #     selected : new selected (true = selected, false = unselected)
  #--------------------------------------------------------------------------
  def selected=(selected)
    @selected = selected
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  # * Cursor Rectangle Update
  #--------------------------------------------------------------------------
  def update_cursor_rect
    if @selected
      self.cursor_rect.set(0, 0, @name_width + 8, 32)
    else
      self.cursor_rect.empty
    end
  end
end

class Game_Map
  #--------------------------------------------------------------------------
  # * Get Map Name
  #--------------------------------------------------------------------------
  def name
    load_data("Data/MapInfos.rxdata")[@map_id].name
  end 
end

Actually you have to load Game_Map through the Marshal.load method.
 
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