查看: 2487|回复: 4

[已经解决] 存档修改的问题第二部分

[复制链接]
梦石
0
星屑
1748
在线时间
485 小时
注册时间
2006-1-7
回帖
996
发表于 2015-11-5 00:58:06 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
接上一个问题继续问
怎么在Saves文件夹下,提取出有效的(.rxdata)存档文件数量?
又如何提取出有效的(.rxdata)存档文件名字

在Load界面建立一个 command_window
选择的列表中的选项是上面提取到的文件名字?

@RyanBern  

评分

参与人数 1星屑 +35 收起 理由
RyanBern + 35 手动认可奖励

查看全部评分

初从文,三年不中;后习武,校场发一矢,中鼓吏,逐之出;遂学医,有所成。自撰一良方,服之,卒。
梦石
0
星屑
1232
在线时间
1017 小时
注册时间
2011-4-30
回帖
1464
发表于 2015-11-5 08:23:56 | 显示全部楼层
建一个存档保存创建的存档信息就好..

点评

读取数据 load_data(filename) 你就得到你保存的数据,然后操作就可以..如果是手动删除存档可能就不行了.......所以楼下的比较保险  发表于 2015-11-5 12:57
数据看你要读取什么消息了,比如你可以用hash表,然后操作这个hash表就好, 比如 shuju={} ; shuju[1]={} ; shuju[1]["name"]="汪汪" ;shuju[1]["shijian"]="20151105"   发表于 2015-11-5 12:55
save_data(obj, filename)  发表于 2015-11-5 12:51
这个。。。。不会。求指导,怎么建立,怎么添加数据,怎么读取数据,怎么移除数据?  发表于 2015-11-5 09:24
回复

使用道具 举报

梦石
0
星屑
9557
在线时间
5074 小时
注册时间
2013-6-21
回帖
3459

开拓者贵宾剧作品鉴家

发表于 2015-11-5 09:03:39 | 显示全部楼层
本帖最后由 RyanBern 于 2015-11-5 23:53 编辑

有效存档如何定义?
  1. filenames = []
  2. Dir.foreach("Saves") do |s|
  3.   filenames << $1 if /(.+?)\.rxdata/ === s
  4. end
复制代码

这样执行完之后,如果Saves文件夹下有"A.rxdata","B.txt","Aa.rxdata"
那么最终filenames = ["A", "Aa"]

@filenames 前面要加'@'


  1. #-----------------------------------------------------------------------------#
  2. # 脚本设定                                                                    #
  3. #-----------------------------------------------------------------------------#
  4. # 说明:                                                                      #
  5. # ①主要采用了Snap To Bitmap这脚本省去了DLL。                                 #
  6. # ②采用了禁术,各位不用到处改脚本了,不过冲突率也大增==                      #
  7. # ③脚本当中采用的脚本归原作者所有。                                          #
  8. # ④设置区域的变量请自行更改。                                                #
  9. #-----------------------------------------------------------------------------#
  10. #-----------------------------------------------------------------------------#
  11. # 创建存档文件夹                                                              #
  12. #-----------------------------------------------------------------------------#
  13. Dir.mkdir("Saves/") unless FileTest.directory?("Saves/")

  14. #-----------------------------------------------------------------------------#
  15. # 存档界面用窗口                                                              #
  16. #-----------------------------------------------------------------------------#
  17. class Window_Save < Window_Base
  18.   #---------------------------------------------------------------------------#
  19.   # 初始化                                                                    #
  20.   #---------------------------------------------------------------------------#
  21.   def initialize
  22.     super(160,64,640-160,480-64)#(160,64,800,736)
  23.     self.contents = Bitmap.new(width-32,height-32)
  24.     @_index = -1
  25.     #refresh(0, nil)
  26.   end
  27.   #---------------------------------------------------------------------------#
  28.   # 刷新                                                                      #
  29.   #---------------------------------------------------------------------------#
  30.   def refresh(index, filename)
  31.     if index != @_index
  32.       # 清除原有资料
  33.       self.contents.clear
  34.       @sprite.dispose if @sprite != nil
  35.       @sprite = nil      
  36.       # 开始读取资料
  37.       @file_index = index
  38.       @filename = filename
  39.       @time_stamp = Time.at(0)
  40.       @file_exist = FileTest.exist?(@filename)
  41.       if @file_exist
  42.         file = File.open(@filename, "r")
  43.         @time_stamp         = file.mtime
  44.         @characters         = Marshal.load(file)
  45.         @frame_count        = Marshal.load(file)
  46.         @game_system        = Marshal.load(file)
  47.         @game_switches      = Marshal.load(file)
  48.         @game_variables     = Marshal.load(file)
  49.         @game_self_switches = Marshal.load(file)
  50.         @game_screen        = Marshal.load(file)
  51.         @game_actors        = Marshal.load(file)
  52.         @game_party         = Marshal.load(file)
  53.         @game_troop         = Marshal.load(file)
  54.         @game_map           = Marshal.load(file)
  55.         @game_player        = Marshal.load(file)
  56.         @bitmap             = @game_system.snap_bitmap
  57.         @total_sec = @frame_count / Graphics.frame_rate
  58.         file.close
  59.         # 描绘角色
  60.         ([email]0...@characters.size[/email]).each do |i|
  61.           x = i*56 + 24
  62.           bitmap = RPG::Cache.character(@characters[i][0], @characters[i][1])
  63.           self.contents.blt(x,328,bitmap,Rect.new(0,0,bitmap.width/4,bitmap.height/4))
  64.         end
  65.         # 描绘游戏时间
  66.         hour = @total_sec / 60 / 60
  67.         min = @total_sec / 60 % 60
  68.         sec = @total_sec % 60
  69.         time_string = sprintf("%02d:%02d:%02d", hour, min, sec)
  70.         self.contents.font.color = normal_color
  71.         self.contents.draw_text(0,320,448,32,time_string,2)
  72.         # 描绘时间标记
  73.         self.contents.font.color = normal_color
  74.         time_string = @time_stamp.strftime("%Y/%m/%d %H:%M")
  75.         self.contents.draw_text(0,352,448,32,time_string, 2)
  76.       else
  77.         self.contents.draw_text(0,320,448,32,"无存档",1)
  78.       end
  79.       @_index = index
  80.     end
  81.   end
  82.   #---------------------------------------------------------------------------#
  83.   # 释放                                                                      #
  84.   #---------------------------------------------------------------------------#
  85.   def dispose
  86.     super
  87.     @sprite.dispose if @sprite != nil
  88.     @sprite = nil
  89.   end
  90. end

  91. #-----------------------------------------------------------------------------#
  92. # 存档界面                                                                    #
  93. #-----------------------------------------------------------------------------#
  94. class Scene_Load
  95.   #---------------------------------------------------------------------------#
  96.   # 初始化                                                                    #
  97.   #---------------------------------------------------------------------------#
  98.   def initialize
  99.     super("要载入哪个文件?")
  100.   end
  101.   #---------------------------------------------------------------------------#
  102.   # 主执行                                                                    #
  103.   #---------------------------------------------------------------------------#
  104.   def main

  105.     # 再生成临时对像
  106.     $game_temp = Game_Temp.new
  107.     # 选择存档时间最新的文件
  108.     $game_temp.last_file_index = 0
  109.     latest_time = Time.at(0)
  110.     if @filenames == nil
  111.       @filenames = []
  112.       Dir.foreach("Saves") do |s|
  113.         @filenames << $1 if /(.+?)\.rxdata/ === s
  114.       end
  115.       if @filenames.empty?
  116.         @filenames = ["无存档"]
  117.       end
  118.     end

  119.     ([email]0...@filenames.size[/email]).each do |i|
  120.       filename = make_filename(i)
  121.       if FileTest.exist?(filename)
  122.         file = File.open(filename, "r")
  123.         if file.mtime > latest_time
  124.           latest_time = file.mtime
  125.           $game_temp.last_file_index = i
  126.         end
  127.         file.close
  128.       end
  129.     end

  130.     @command_window = Window_Command.new(160,@filenames)
  131.     @command_window.height = 480-64#736
  132.     @command_window.y = 64
  133.     @command_window.active = true
  134.     # 存档资料窗口
  135.     @save_window = Window_Save.new
  136.     # 执行过渡
  137.     Graphics.transition
  138.     # 主循环
  139.     loop do
  140.       # 刷新游戏画面
  141.       Graphics.update
  142.       # 刷新输入信息
  143.       Input.update
  144.       # 刷新画面
  145.       update
  146.       # 如果画面切换的话的就中断循环
  147.       if $scene != self
  148.         break
  149.       end
  150.     end
  151.     # 准备过渡
  152.     Graphics.freeze
  153.     # 释放窗口
  154.     @save_window.dispose
  155.     @command_window.dispose
  156.   end
  157.   #---------------------------------------------------------------------------#
  158.   # 刷新                                                                      #
  159.   #---------------------------------------------------------------------------#
  160.   def update
  161.     update_command   
  162.   end
  163.   #---------------------------------------------------------------------------#
  164.   # 选择存档窗口刷新                                                          #
  165.   #---------------------------------------------------------------------------#
  166.   def update_command
  167.     @command_window.update
  168.     filename = @filenames[@command_window.index]
  169.     @save_window.refresh(@command_window.index, filename)
  170.     # 按下取消键
  171.     if Input.trigger?(Input::B)
  172.       # 演奏取消 SE
  173.       $game_system.se_play($data_system.cancel_se)
  174.       # 切换到标题画面
  175.       $scene = Scene_Title.new
  176.     end
  177.     # 按下确定键
  178.     if Input.trigger?(Input::C)
  179.       # 文件不存在的情况下
  180.       unless FileTest.exist?(filename)
  181.         # 演奏冻结 SE
  182.         $game_system.se_play($data_system.buzzer_se)
  183.         return
  184.       end
  185.       # 演奏读档 SE
  186.       $game_system.se_play($data_system.load_se)
  187.       # 写入存档数据
  188.       file = File.open(filename, "rb")
  189.       read_save_data(file)
  190.       file.close
  191.       # 还原 BGM、BGS
  192.       $game_system.bgm_play($game_system.playing_bgm)
  193.       $game_system.bgs_play($game_system.playing_bgs)
  194.       # 刷新地图 (执行并行事件)
  195.       $game_map.update
  196.       # 切换到菜单界面
  197.       $scene = Scene_Menu.new(0)
  198.     end
  199.   end
  200.   #---------------------------------------------------------------------------#
  201.   # 读档                                                                      #
  202.   #---------------------------------------------------------------------------#
  203.   def read_save_data(file)
  204.     # 读取描绘存档文件用的角色数据
  205.     characters = Marshal.load(file)
  206.     # 读取测量游戏时间用画面计数
  207.     Graphics.frame_count = Marshal.load(file)
  208.     # 读取各种游戏对像
  209.     $game_system        = Marshal.load(file)
  210.     $game_switches      = Marshal.load(file)
  211.     $game_variables     = Marshal.load(file)
  212.     $game_self_switches = Marshal.load(file)
  213.     $game_screen        = Marshal.load(file)
  214.     $game_actors        = Marshal.load(file)
  215.     $game_party         = Marshal.load(file)
  216.     $game_troop         = Marshal.load(file)
  217.     $game_map           = Marshal.load(file)
  218.     $game_player        = Marshal.load(file)
  219.     # 魔法编号与保存时有差异的情况下
  220.     # (加入编辑器的编辑过的数据)
  221.     if $game_system.magic_number != $data_system.magic_number
  222.       # 重新装载地图
  223.       $game_map.setup($game_map.map_id)
  224.       $game_player.center($game_player.x, $game_player.y)
  225.     end
  226.     # 刷新同伴成员
  227.     $game_party.refresh
  228.   end
  229. end
复制代码

点评

未测试,自己改着改着就改好了。看来是要自己多用用p了,才知道怎么个情况了。还是多谢了。我改好的部分在4楼编辑。  发表于 2015-11-6 00:04
见编辑  发表于 2015-11-5 22:46
另外,filenames << $1 if /Saves\/(.+?)\.rxdata/ === s,这样做后 p filenames 显示是 [];filenames << s if s != "." && s != ".."这样返回的就是[XXXX.rxdata],不含路径。   发表于 2015-11-5 22:36
【@save_window.refresh(@command_window.index, filenames[@command_window.index])】→undeifined local variable or method 'filenames' for #<Scene_load:0x38bb4e0>  发表于 2015-11-5 22:30
@filenames[@command_window.index] 这样可以取出选中存档的文件名  发表于 2015-11-5 13:57

评分

参与人数 1星屑 +150 收起 理由
无忧谷主幻 + 150 塞糖

查看全部评分

回复

使用道具 举报

梦石
0
星屑
1748
在线时间
485 小时
注册时间
2006-1-7
回帖
996
 楼主| 发表于 2015-11-5 10:43:14 | 显示全部楼层
本帖最后由 白鬼 于 2015-11-6 00:06 编辑

自己修改好了。主要是多用p,看看获得的值对不对,差在哪里就找到了。
多谢了!@RyanBern

Load界面Load界面

  1. #-----------------------------------------------------------------------------#
  2. # 存档界面用窗口                                                              #
  3. #-----------------------------------------------------------------------------#
  4. class Window_Save < Window_Base
  5.   #---------------------------------------------------------------------------#
  6.   # 初始化                                                                    #
  7.   #---------------------------------------------------------------------------#
  8.   def initialize
  9.     super(160,0,640-160,480)#(160,64,800,736)
  10.     self.contents = Bitmap.new(width-32,height-32)
  11.     @_index = -1
  12.     refresh(0)
  13.   end
  14.   #---------------------------------------------------------------------------#
  15.   # 刷新                                                                      #
  16.   #---------------------------------------------------------------------------#
  17.   def refresh(index)
  18.     if index != @_index
  19.       # 清除原有资料
  20.       self.contents.clear
  21.       @sprite.dispose if @sprite != nil
  22.       @sprite = nil
  23.       
  24.       # 开始读取资料
  25.       @file_index = index
  26.       
  27.       #@filename = FILES[index]#BYAKKI
  28.       ##########################################
  29.       filenames = []
  30.       Dir.foreach("Saves") do |s|
  31.         filenames << $1 if /(.+?)\.rxdata/ === s
  32.       end
  33.       filename = filenames[index]
  34.       @filename = "Saves/" + filename + ".rxdata"
  35.       ##########################################
  36.       @time_stamp = Time.at(0)
  37.       @file_exist = FileTest.exist?(@filename)
  38.       if @file_exist
  39.         file = File.open(@filename, "r")
  40.         @time_stamp         = file.mtime
  41.         @characters         = Marshal.load(file)
  42.         @frame_count        = Marshal.load(file)
  43.         @game_system        = Marshal.load(file)
  44.         @game_switches      = Marshal.load(file)
  45.         @game_variables     = Marshal.load(file)
  46.         @game_self_switches = Marshal.load(file)
  47.         @game_screen        = Marshal.load(file)
  48.         @game_actors        = Marshal.load(file)
  49.         @game_party         = Marshal.load(file)
  50.         @game_troop         = Marshal.load(file)
  51.         @game_map           = Marshal.load(file)
  52.         @game_player        = Marshal.load(file)
  53.         @total_sec = @frame_count / Graphics.frame_rate
  54.         file.close
  55.         
  56.         # 描绘角色
  57.         #行走图
  58.         #bitmap = RPG::Cache.character(@characters[0].character_name, @characters[0].character_hue)
  59.         #filename = @filename.sub!(/Saves\/(.+?)\.rxdata/){$1} #去掉两头的内容显示
  60.         #self.contents.draw_text(4, 0, 600, 32, filename)
  61.         self.contents.draw_text(4, 32, 600, 32, @characters[0].name)
  62.         
  63.       else
  64.         self.contents.draw_text(0,320,448,32,"无存档",1)
  65.       end
  66.     #else
  67.       #self.contents.draw_text(0,320,448,32,"无存档",1)
  68.     end
  69.     @_index = index
  70.   end
  71.   #---------------------------------------------------------------------------#
  72.   # 释放                                                                      #
  73.   #---------------------------------------------------------------------------#
  74.   def dispose
  75.     super
  76.     @sprite.dispose if @sprite != nil
  77.     @sprite = nil
  78.   end
  79. end
  80. #==============================================================================
  81. # ■ Scene_Load
  82. #------------------------------------------------------------------------------
  83. #  处理读档画面的类。
  84. #==============================================================================

  85. class Scene_Load
  86.   #--------------------------------------------------------------------------
  87.   # ● 初始化对像
  88.   #--------------------------------------------------------------------------
  89.   def initialize

  90.   end
  91.   #--------------------------------------------------------------------------
  92.   # ● 主处理
  93.   #--------------------------------------------------------------------------
  94.   def main
  95.     filenames = []
  96.     Dir.foreach("Saves") do |s|
  97.       filenames << s if s != "." && s != ".."
  98.       #filenames << $1 if /Saves\/(.+?)\.rxdata/ === s#这个方法数组就是空的[]
  99.     end
  100.     # 存档列表选项窗口
  101.     @command_window = Window_Command.new(160,filenames)
  102.     @command_window.index = 0
  103.     # 存档信息窗口
  104.     @save_window = Window_Save.new
  105.     # 执行过渡
  106.     Graphics.transition
  107.     # 主循环
  108.     loop do
  109.       # 刷新游戏画面
  110.       Graphics.update
  111.       # 刷新输入信息
  112.       Input.update
  113.       # 刷新画面
  114.       update
  115.       # 如果画面切换的话的就中断循环
  116.       if $scene != self
  117.         break
  118.       end
  119.     end
  120.     # 准备过渡
  121.     Graphics.freeze
  122.     # 释放窗口
  123.     @save_window.dispose
  124.     @command_window.dispose
  125.   end
  126.   #---------------------------------------------------------------------------#
  127.   # 刷新                                                                      #
  128.   #---------------------------------------------------------------------------#
  129.   def update
  130.     update_command
  131.   end
  132.   #---------------------------------------------------------------------------#
  133.   # 选择存档窗口刷新                                                          #
  134.   #---------------------------------------------------------------------------#
  135.   def update_command
  136.     @command_window.update
  137.    
  138.     @save_window.refresh(@command_window.index)#, filenames[@command_window.index])
  139.     ##################################################
  140.     filenames = []
  141.     Dir.foreach("Saves") do |s|
  142.       filenames << $1 if /(.+?)\.rxdata/ === s
  143.     end
  144.    
  145.     filename = "Saves/" + filenames[@command_window.index] + ".rxdata"
  146.    
  147.     ##################################################
  148.     #filename = FILES[@command_window.index]
  149.     # 按下取消键
  150.     if Input.trigger?(Input::B)
  151.       # 演奏取消 SE
  152.       $game_system.se_play($data_system.cancel_se)
  153.       # 切换到标题画面
  154.       $scene = Scene_Title.new
  155.     end
  156.     # 按下确定键
  157.     if Input.trigger?(Input::C)
  158.       # 文件不存在的情况下
  159.       unless FileTest.exist?(filename)
  160.         # 演奏冻结 SE
  161.         $game_system.se_play($data_system.buzzer_se)
  162.         return
  163.       end
  164.       # 演奏读档 SE
  165.       $game_system.se_play($data_system.load_se)
  166.       # 写入存档数据
  167.       file = File.open(filename, "rb")
  168.       read_save_data(file)
  169.       file.close
  170.       # 还原 BGM、BGS
  171.       $game_system.bgm_play($game_system.playing_bgm)
  172.       $game_system.bgs_play($game_system.playing_bgs)
  173.       # 刷新地图 (执行并行事件)
  174.       $game_map.update
  175.       # 切换到菜单界面
  176.       $scene = Scene_Menu.new(0)
  177.     end
  178.   end
  179.   #---------------------------------------------------------------------------#
  180.   # 读档                                                                      #
  181.   #---------------------------------------------------------------------------#
  182.   def read_save_data(file)
  183.     # 读取描绘存档文件用的角色数据
  184.     characters = Marshal.load(file)
  185.     # 读取测量游戏时间用画面计数
  186.     Graphics.frame_count = Marshal.load(file)
  187.     # 读取各种游戏对像
  188.     $game_system        = Marshal.load(file)
  189.     $game_switches      = Marshal.load(file)
  190.     $game_variables     = Marshal.load(file)
  191.     $game_self_switches = Marshal.load(file)
  192.     $game_screen        = Marshal.load(file)
  193.     $game_actors        = Marshal.load(file)
  194.     $game_party         = Marshal.load(file)
  195.     $game_troop         = Marshal.load(file)
  196.     $game_map           = Marshal.load(file)
  197.     $game_player        = Marshal.load(file)
  198.     # 魔法编号与保存时有差异的情况下
  199.     # (加入编辑器的编辑过的数据)
  200.     if $game_system.magic_number != $data_system.magic_number
  201.       # 重新装载地图
  202.       $game_map.setup($game_map.map_id)
  203.       $game_player.center($game_player.x, $game_player.y)
  204.     end
  205.     # 刷新同伴成员
  206.     $game_party.refresh
  207.   end
  208. end
复制代码

  

还在优化
初从文,三年不中;后习武,校场发一矢,中鼓吏,逐之出;遂学医,有所成。自撰一良方,服之,卒。
回复

使用道具 举报

梦石
0
星屑
1748
在线时间
485 小时
注册时间
2006-1-7
回帖
996
 楼主| 发表于 2015-11-5 23:44:37 | 显示全部楼层
RyanBern 发表于 2015-11-5 09:03
有效存档如何定义?
filenames = []
Dir.foreach("Saves") do |s|

前面加 '@' 就是一个空的数组。我真的搞不定。

点评

正则表达式我已经改了啊,难道还有问题?  发表于 2015-11-5 23:46
初从文,三年不中;后习武,校场发一矢,中鼓吏,逐之出;遂学医,有所成。自撰一良方,服之,卒。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部