Project1

标题: 如何记录角色使用物品的数量 [打印本页]

作者: jiushiainilip19    时间: 2017-2-23 10:08
标题: 如何记录角色使用物品的数量
本帖最后由 jiushiainilip19 于 2017-2-23 10:11 编辑

例子: 角色1使用了编号XX的物品XX次  然后记录下来,单独记录。

1: 不考虑使用自带的变量。 理由:角色比较多。

2:需要记录所使用的物品有50个 或者更多

3:用处:在于重生角色的时候将角色使用的那些物品全部归还玩家。

4:我原本设定物品是不可使用的,是在脚本中$game_party.lose_item来消耗物品的,所以不可在物品公共事件中输入代码
---------------------------------------------------------------------------------------

{:2_249:} ! 求求大神帮个忙。。{:2_286:}
作者: 夜狠简单    时间: 2017-2-23 11:47
本帖最后由 夜狠简单 于 2017-2-23 14:10 编辑

RUBY 代码复制
  1. =begin
  2. $game_system.items_record={角色id=>{物品id=>该物品使用次数}}
  3. 取5号角色的3号物品使用次数 $game_system.items_record[5][3]
  4. =end
  5. #==============================================================================
  6. # ■ Game_System
  7. #------------------------------------------------------------------------------
  8. #  处理系统附属数据的类。也可执行诸如 BGM 管理之类的功能。本类的实例请参考
  9. # $game_system 。
  10. #==============================================================================
  11.  
  12. class Game_System
  13.   attr_accessor :items_record             # 物品使用记录
  14.   #--------------------------------------------------------------------------
  15.   # ● 初始化对像
  16.   #--------------------------------------------------------------------------
  17.   alias rk_20170223_initialize initialize
  18.   def initialize
  19.     rk_20170223_initialize
  20.     @items_record = {}
  21.   end
  22. end
  23.  
  24. #==============================================================================
  25. # ■ Game_Party
  26. #------------------------------------------------------------------------------
  27. #  处理同伴的类。包含金钱以及物品的信息。本类的实例
  28. # 请参考 $game_party。
  29. #==============================================================================
  30.  
  31. class Game_Party  
  32.   #--------------------------------------------------------------------------
  33.   # ● 减少物品
  34.   #     item_id : 物品 ID
  35.   #     n       : 个数
  36.   #--------------------------------------------------------------------------
  37.   alias rk_20170223_lose_item lose_item
  38.   def lose_item(item_id, n,id=0)
  39.    $game_system.items_record||={};$game_system.items_record[id]||={}
  40.    $game_system.items_record[id][item_id]||=0
  41.    $game_system.items_record[id][item_id]+=n
  42.    $game_system.items_record["总次数"]||={}
  43.    $game_system.items_record["总次数"][item_id]||=0
  44.    $game_system.items_record["总次数"][item_id]+=n if id != 0
  45.    n=id ==0 ?n:0
  46.    rk_20170223_lose_item(item_id, n)
  47.   end
  48. end
  49.  
  50. #==============================================================================
  51. # ■ Scene_Battle (分割定义 4)
  52. #------------------------------------------------------------------------------
  53. #  处理战斗画面的类。
  54. #==============================================================================
  55.  
  56. class Scene_Battle
  57.   #--------------------------------------------------------------------------
  58.   # ● 生成物品行动结果
  59.   #--------------------------------------------------------------------------
  60.   alias rk_20170223_make_item_action_result make_item_action_result
  61.   def make_item_action_result
  62.     rk_20170223_make_item_action_result
  63.     # 消耗品的情况下  后面的判断有点多余,但是先加上。
  64.     if @item.consumable && @active_battler.is_a?(Game_Actor)
  65.       # 使用的物品减 1
  66.       $game_party.lose_item(@item.id, 1,@active_battler.id)
  67.     end
  68.   end
  69. end

作者: jiushiainilip19    时间: 2017-2-23 12:56
夜狠简单 发表于 2017-2-23 11:47
=begin
$game_system.items_record={角色id=>{物品id=>该物品使用次数}}
取5号角色的3号物品使用次数 $game ...

p $game_system.items_record  这个读取的{0=>{10=>1}}
角色ID都是0  我把我脚本贴出来

  1. #$scene =Scene_Tian.new
  2.   $任脉天赋 = [10,11,12,13,14]
  3.   $督脉天赋 = [20,21,22,23,24]
  4.   $冲脉天赋 = [30,31,32,33,34]
  5.   $带脉天赋 = [40,41,42,43,44]

  6.   $消耗记录 = [10,11,12,13,14]
  7.   
  8. class Scene_Tian
  9.   #--------------------------------------------------------------------------
  10.   # ● 初始化对像
  11.   #     actor_index : 角色索引
  12.   #--------------------------------------------------------------------------
  13.   def initialize(actor_index = 0, menu_index = 0)
  14.     @actor_index = actor_index
  15.     @menu_index = menu_index
  16.   end
  17.   #--------------------------------------------------------------------------
  18.   # ● 主处理
  19.   #--------------------------------------------------------------------------
  20.   def main
  21.     # 获取角色
  22.     @actor = $game_party.actors[@actor_index]
  23.     # 生成状态窗口
  24.     @status_window = Window_Tian.new(@actor)
  25.     s1 = "天赋进阶:任脉:"
  26.     s2 = "天赋进阶:督脉:"
  27.     s3 = "天赋进阶:冲脉:"
  28.     s4 = "天赋进阶:带脉:"
  29.     s5 = "天赋重生:慎用"
  30.     @commandy_window = Window_Aommand.new(175, [s1,s2,s3,s4,s5])
  31.     @commandy_window.index = @menu_index
  32.     @commandy_window.opacity = 0
  33.     @commandy_window.active = true
  34.     @commandy_window.x+=90
  35.     @commandy_window.y+=150
  36.    
  37.     @item_window = Window_Etem.new(@actor)
  38.     @item_window.active = false
  39.    # @item_window.visible = false
  40.    # @item_window.opacity = 0
  41.     # 执行过渡
  42.     Graphics.transition
  43.     # 主循环
  44.     loop do
  45.       # 刷新游戏画面
  46.       Graphics.update
  47.       # 刷新输入信息
  48.       Input.update
  49.       # 刷新画面
  50.       update
  51.       # 如果画面被切换的话就中断循环
  52.       if $scene != self
  53.         break
  54.       end
  55.     end
  56.     # 准备过渡
  57.     Graphics.freeze
  58.     # 释放窗口
  59.     @status_window.dispose
  60.     @commandy_window.dispose   
  61.     @item_window.dispose
  62.   end
  63.   #--------------------------------------------------------------------------
  64.   # ● 刷新画面
  65.   #--------------------------------------------------------------------------
  66.   def update
  67.     @item_window.update
  68.     @commandy_window.update
  69.     if @commandy_window.active
  70.       update_commandy
  71.       return
  72.     end
  73.    
  74.     # 按下 B 键的情况下
  75.     if Input.trigger?(Input::B)
  76.       # 演奏取消 SE
  77.       $game_system.se_play($data_system.cancel_se)
  78.       # 切换到菜单画面
  79.       $scene = Scene_Menu.new(3)
  80.       return
  81.     end
  82.   end
  83.   
  84.   def update_commandy
  85.     if Input.trigger?(Input::B)
  86.       # 演奏取消 SE
  87.       $game_system.se_play($data_system.cancel_se)
  88.       # 切换到菜单画面
  89.       $scene = Scene_Menu.new(3)
  90.       return
  91.     end
  92.    
  93.     if Input.repeat?(Input::C)
  94.       $game_system.se_play($data_system.decision_se)
  95.       @actor = $game_party.actors[@actor_index]
  96.       @item = @item_window.item
  97.       case @commandy_window.index
  98.        when 0 #任脉
  99. #=======================================  
  100.        if @actor.renlv == 4 && @actor.ren==6
  101.          p "全部完成"
  102.        else
  103.          if @actor.ren==6
  104.            @actor.ren = 0
  105.            @actor.renlv += 1
  106.            Audio.se_play("Audio/SE/Y_se_item02")
  107.            @item_window.refresh
  108.            @status_window.refresh
  109.          else
  110.            if $game_party.item_number($任脉天赋[@actor.renlv])>=1
  111.              @actor.ren+=1
  112.              $game_party.lose_item($任脉天赋[@actor.renlv],1)
  113.             p $game_system.items_record
  114.              @status_window.refresh
  115.              @item_window.refresh
  116.              Audio.se_play("Audio/SE/006-System06")
  117.            else
  118.              Audio.se_play("Audio/SE/004-System04")
  119.              p "缺少升级所需要的对应天赋"
  120.            end
  121.          end
  122.        end
  123.       
  124. #=======================================     
  125.        when 1 #督脉
  126.        if @actor.dulv == 4 && @actor.du==6
  127.          p "全部完成"
  128.        else
  129.          if @actor.du==6
  130.            @actor.du = 0
  131.            @actor.dulv += 1
  132.            Audio.se_play("Audio/SE/Y_se_item02")
  133.            @item_window.refresh
  134.            @status_window.refresh
  135.          else
  136.            if $game_party.item_number($督脉天赋[@actor.dulv])>=1
  137.              @actor.du+=1
  138.              $game_party.lose_item($督脉天赋[@actor.dulv],1)
  139.              @status_window.refresh
  140.              @item_window.refresh
  141.              Audio.se_play("Audio/SE/006-System06")
  142.            else
  143.              Audio.se_play("Audio/SE/004-System04")
  144.              p "缺少升级所需要的对应天赋"
  145.            end
  146.          end
  147.        end
  148.        when 2 #冲脉
  149.        if @actor.chonglv == 4 && @actor.chong==6
  150.          p "全部完成"
  151.        else
  152.          if @actor.chong==6
  153.            @actor.chong = 0
  154.            @actor.chonglv += 1
  155.            Audio.se_play("Audio/SE/Y_se_item02")
  156.            @status_window.refresh
  157.            @item_window.refresh
  158.          else
  159.            if $game_party.item_number($冲脉天赋[@actor.chonglv])>=1
  160.              @actor.chong+=1
  161.              $game_party.lose_item($冲脉天赋[@actor.chonglv],1)
  162.              @status_window.refresh
  163.              @item_window.refresh
  164.              Audio.se_play("Audio/SE/006-System06")
  165.            else
  166.              Audio.se_play("Audio/SE/004-System04")
  167.              p "缺少升级所需要的对应天赋"
  168.            end
  169.          end
  170.        end
  171.        when 3 #带脉
  172.        if @actor.dailv == 4 && @actor.dai==6
  173.          p "全部完成"
  174.        else
  175.          if @actor.dai==6
  176.            @actor.dai = 0
  177.            @actor.dailv += 1
  178.            Audio.se_play("Audio/SE/Y_se_item02")
  179.            @item_window.refresh
  180.            @status_window.refresh
  181.          else
  182.            if $game_party.item_number($带脉天赋[@actor.dailv])>=1
  183.              @actor.dai+=1
  184.              $game_party.lose_item($带脉天赋[@actor.dailv],1)
  185.              @status_window.refresh
  186.              @item_window.refresh
  187.              Audio.se_play("Audio/SE/006-System06")
  188.            else
  189.              Audio.se_play("Audio/SE/004-System04")
  190.              p "缺少升级所需要的对应天赋"
  191.            end
  192.          end
  193.        end
  194.        when 4 #重生
  195.          
  196.         $game_system.items_record[@actor.id].each_pair{|key, value|$game_party.gain_item(key, value)}   
  197.            
  198.          @actor.ren = 0
  199.          @actor.du = 0
  200.          @actor.chong = 0
  201.          @actor.dai = 0
  202.          @actor.renlv = 0
  203.          @actor.dulv = 0
  204.          @actor.chonglv = 0
  205.          @actor.dailv = 0
  206.          Audio.se_play("Audio/SE/Y_se_item06")
  207.          @status_window.refresh
  208.          @item_window.refresh
  209.          p "重生成功,返还所有资源,退化英雄经脉"
  210.          
  211.        end
  212.      end
  213.      
  214.     # 按下 R 键的情况下
  215.     if Input.trigger?(Input::R)
  216.       # 演奏光标 SE
  217.       $game_system.se_play($data_system.cursor_se)
  218.       # 移至下一位角色
  219.       @actor_index += 1
  220.       @actor_index %= $game_party.actors.size
  221.       # 切换到别的状态画面
  222.       $scene = Scene_Tian.new(@actor_index)
  223.       return
  224.     end
  225.     # 按下 L 键的情况下
  226.     if Input.trigger?(Input::L)
  227.       # 演奏光标 SE
  228.       $game_system.se_play($data_system.cursor_se)
  229.       # 移至上一位角色
  230.       @actor_index += $game_party.actors.size - 1
  231.       @actor_index %= $game_party.actors.size
  232.       # 切换到别的状态画面
  233.       $scene = Scene_Tian.new(@actor_index)
  234.       return
  235.     end
  236.   end
  237.   #-----------------------
  238. end
复制代码


作者: 夜狠简单    时间: 2017-2-23 13:18
本帖最后由 夜狠简单 于 2017-2-23 13:23 编辑

    when 4 #重生
       begin
         $game_system.items_record[@actor.id].each_pair{|key, value|
         $game_party.gain_item(key, value)}  
         @actor.ren = 0
         @actor.du = 0
         @actor.chong = 0
         @actor.dai = 0
         @actor.renlv = 0
         @actor.dulv = 0
         @actor.chonglv = 0
         @actor.dailv = 0
         Audio.se_play("Audio/SE/Y_se_item06")
         @status_window.refresh
         @item_window.refresh
         p "重生成功,返还所有资源,退化英雄经脉"
       rescue
         @actor.ren = 0
         @actor.du = 0
         @actor.chong = 0
         @actor.dai = 0
         @actor.renlv = 0
         @actor.dulv = 0
         @actor.chonglv = 0
         @actor.dailv = 0
         Audio.se_play("Audio/SE/Y_se_item06")
         @status_window.refresh
         @item_window.refresh
         p "重生成功,退化英雄经脉"
       end
            
       end
     end
     
    # 按下 R 键的情况下
作者: jiushiainilip19    时间: 2017-2-23 13:44
夜狠简单 发表于 2017-2-23 13:18
when 4 #重生
       begin
         $game_system.items_record[@actor.id].each_pair{|key, value|

RUBY 代码复制
  1. =begin
  2. $game_system.items_record={角色id=>{物品id=>该物品使用次数}}
  3. 取15号角色的10号物品使用次数 $game_system.items_record[15][10]
  4. =end
  5. class Game_System
  6.   attr_accessor :items_record             # 物品使用记录
  7.   #--------------------------------------------------------------------------
  8.   # ● 初始化对像
  9.   #--------------------------------------------------------------------------
  10.   alias rk_20170223_initialize initialize
  11.   def initialize
  12.     rk_20170223_initialize
  13.     @items_record = {}
  14.   end
  15. end
  16. class Game_Party  
  17.   #--------------------------------------------------------------------------
  18.   # ● 减少物品
  19.   #     item_id : 物品 ID
  20.   #     n       : 个数
  21.   #--------------------------------------------------------------------------
  22.   alias rk_20170223_lose_item lose_item
  23.   def lose_item(item_id, n,id=0)
  24.    #case item_id
  25.     #when 10..50
  26.    $game_system.items_record||={};$game_system.items_record[id]||={}
  27.    $game_system.items_record[id][item_id]||=0
  28.    $game_system.items_record[id][item_id]+=n
  29.    $game_system.items_record["总次数"][item_id]+=n if id != 0
  30.    n=id ==0 ?n:0
  31.    rk_20170223_lose_item(item_id, n)
  32.    #end
  33.   end
  34. end
  35. class Scene_Battle
  36.   #--------------------------------------------------------------------------
  37.   # ● 生成物品行动结果
  38.   #--------------------------------------------------------------------------
  39.   alias rk_20170223_make_item_action_result make_item_action_result
  40.   def make_item_action_result
  41.     rk_20170223_make_item_action_result
  42.     # 消耗品的情况下  后面的判断有点多余,但是先加上。
  43.     if @item.consumable && @active_battler.is_a?(Game_Actor)
  44.       # 使用的物品减 1
  45.       $game_party.lose_item(@item.id, 1,@active_battler.id)
  46.     end
  47.   end
  48. end

QQ截图20170223134410.png (2.6 KB, 下载次数: 10)

QQ截图20170223134410.png

作者: yang1zhi    时间: 2017-2-23 14:03
你一定误会了。以为自带的一个变量只能存一个角色一个数。你没发现自带的这么多变量都只是一个变量吗。
都是$game_variables
默认有存档的变量就是下面这些


Marshal.dump($game_system, file)
    Marshal.dump($game_switches, file)
    Marshal.dump($game_variables, file)
    Marshal.dump($game_self_switches, file)
    Marshal.dump($game_screen, file)
    Marshal.dump($game_actors, file)
    Marshal.dump($game_party, file)
    Marshal.dump($game_troop, file)
    Marshal.dump($game_map, file)
    Marshal.dump($game_player, file)




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1