Project1
标题: 请教一个关于变量储存的问题 [打印本页]
作者: 939034448 时间: 2021-8-10 19:10
标题: 请教一个关于变量储存的问题
由于做了一个强化池,每个精灵都有自己的上限,这样的话,因为精灵有6项基本属性,每个精灵就要占用6个变量,很耗变量,有没有什么办法让这些数据以txt的形式储存到外部而不占用内部的变量呢?
即建立一个TXT,把精灵强化后的积累量放到这个txt里,并且拥有编号,就好像 :
“1号项目
20
2号项目
30”
这样子,有点类似“物品合成简易优化版”的那个脚本,但是它不仅要能读取,还要能写入,并且能给写入的数据分组。(我的说法可能不太清晰,请大佬们见谅,我会尽量讲清楚我的问题的)
作者: SailCat 时间: 2021-8-10 19:10
本帖最后由 SailCat 于 2021-8-13 11:00 编辑
我再写详细一点:
1. 独立开关用做独立变量,
将Game_SelfSwitches的第20行
由return @data[key] == true ? true : false
return @data[key] == true ? true : false
改成return @data[key] || false
return @data[key] || false
2. 精灵数据的存储
首先,建立一个空地图,这个地图不需要任何图块,只用来存储数据及占位用,假设这个地图的编号为200。
然后,在地图上粘贴空事件,每个空事件对应一个精灵(因为独立开关需要依附地图事件而存在)——此步可选,取决于精灵是不是需要行走图等,如果需要行走图,就这样做,如果精灵只是一堆数据,并没有图像,可以跳过这一步。
再次,插入一个插件脚本,内容如下:
class Interpreter
def plugin_buff_pixie(id, key, value = nil)
$game_self_switches[[200, id, key]] ||= 0
$game_self_switches[[200, id, key]] += (value || 0)
end
end
class Interpreter
def plugin_buff_pixie(id, key, value = nil)
$game_self_switches[[200, id, key]] ||= 0
$game_self_switches[[200, id, key]] += (value || 0)
end
end
3. 每当需要强化精灵时,用事件脚本调用:
plugin_buff_pixie(精灵的ID,强化的类型,强化值),如 plugin_buff_pixie(1, :str, 7) 即为将1号精灵的str值增加7
需要获取精灵的强化能力,则调用plugin_buff_pixie(精灵的ID,强化的类型),如plugin_buff_pixie(1, :str) 即为获取1号精灵的str值,未经设置过的值都是0。
2和3两步其实不是必须的,只是实现一个事件脚本中的插件命令,目的是为了简化操作,方便书写,你也可以每次都用$game_self_switches开头的生代码,但那样会很烦。
这个做法和硬改Game_System的好处相比就是:由于套了独立开关的默认壳子,旧存档100%兼容(这一个好处就足够了)
作者: Im剑侠客 时间: 2021-8-10 20:04
提供一个思路,比如建一个temp数组变量,然后,a代表精灵属性ID关系,b代表精灵ID关系,写法a = temp[精灵ID]、b = temp[属性ID],也可以有temp[属性ID][精灵ID]的简化方式。
只是前提是,需要自建一个temp变量。这个让楼下来写吧。(手机码字见谅。
作者: guoxiaomi 时间: 2021-8-12 13:35
所以你可能是想要一个自定义的数据结构,并且保存在存档里?这样可以吗:
- class MyData
- attr_accessor :prop1
- attr_accessor :prop2
- attr_accessor :prop3
- # 各属性的初始值
- def initialize
- @prop1 = 0
- @prop2 = 0
- @prop3 = 0
- end
- end
复制代码
脚本里调用:
- # 存储到变量1里
- $game_variables[1] = MyData.new
- # 获取属性1
- $game_variables[1].prop1
- # 修改属性1
- $game_variables[1].prop1 = 10
- $game_variables[1].prop1 += 10
复制代码
作者: 939034448 时间: 2021-8-12 14:16
噗,评分里居然显示不完全,我再发一下吧,我是嫌每个精灵占用6个变量位太多了,想把数据储存到外部,就不用占用数据库里的变量位了,毕竟有效变量只有5000,效果的话应该和RyanBern大佬的物品合成简易优化版脚本差不多,但是多了一个写入的功能,物品合成的话只能在外部修改
作者: guoxiaomi 时间: 2021-8-12 19:29
嫌变量位太多不一定要把变量的数据储存在外部,RB的脚本你发一下,我看看
作者: KB.Driver 时间: 2021-8-12 22:02
实际上你可以给Game_System加东西,很多脚本都是这么做的。
举个例子
class Game_System
attr_accessor :fairies # 精灵
alias initialize_for_init_fairies initialize
def initialize
initialize_for_init_fairies
self.fairies = []
end
end
class Game_System
attr_accessor :fairies # 精灵
alias initialize_for_init_fairies initialize
def initialize
initialize_for_init_fairies
self.fairies = []
end
end
这样以后,就可以在任何能敲脚本的地方用$game_system.fairies[n]来读写这个数组的第n号元素
由于是在Game_System里,你不用修改存读档的代码。
由于引入了脚本,需要你懂一点基本语法才能驾驭,不过这个学习是值得的。
作者: SailCat 时间: 2021-8-12 23:07
独立开关当独立变量了解一下?改一行脚本就可以
作者: 939034448 时间: 2021-8-13 01:31
本帖最后由 939034448 于 2021-8-13 01:36 编辑
这个脚本只有打开脚本编辑器界面一次才会生效TXT的变动,不过考虑到要做写入的话好像确实是任人修改了......话说有没有给变量破限的,不过听说破限了也是无效变量....
- #=============================================================================
- # 物品合成简易优化版 Ver 1.0
- #-----------------------------------------------------------------------------
- # By : RyanBern
- #-----------------------------------------------------------------------------
- # 功能介绍:
- # 这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
- # 通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
- # 改成了外部文件的形式,下面就说明一下如何使用。
- #-----------------------------------------------------------------------------
- # 使用方法:
- # 1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
- # 再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
- # <recipe=1>
- # <product>kind=0,id=3</product>
- # <ingredients>
- # kind=0,id=1,number=2
- # kind=0,id=2,number=2
- # </ingredients>
- # </recipe>
- # 其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
- # 第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
- # 就是对应的ID
- # 第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
- # 每一行代表一种原料的分类,id,和数量,注意不要写重复了。
- # 如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
- # 2.有关学习配方:
- # $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
- # $game_party.learn_recipe(配方ID) 学习该ID的配方
- # $game_party.forget_recipe(配方ID) 失去该ID的配方
- # 3.有关场景调用:
- # $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
- # $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
- # $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
- #-----------------------------------------------------------------------------
- # 注意事项:
- # 1.Recipes.txt里面的缩进不是必须的。
- # 2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
- # 以便日后做出更改。
- #-----------------------------------------------------------------------------
- # 更新记录:
- #=============================================================================
- module RB
- Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m
- Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m
- Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m
- Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
- end
- module RPG
- class Recipe
- attr_accessor :id
- attr_accessor :product
- attr_accessor :ingredients
- def initialize
- @id = 0
- @product = Ingredient.new
- @ingredients = []
- end
- class Ingredient
- attr_accessor :kind
- attr_accessor :id
- attr_accessor :number
- def initialize(kind = 0, id = 0, number = 0)
- @kind = kind
- @id = id
- @number = number
- end
- end
- end
- def self.extract_recipe
- filename = "Recipes.txt"
- outfile_name = "Data/Recipes.rxdata"
- data = []
- begin
- file = File.open(filename, "r")
- str = file.read
- file.close
- while str.slice!(RB::Regex_Recipe)
- id = $1.to_i
- contents = $2
- recipe = Recipe.new
- recipe.id = id
- if RB::Regex_Product =~ contents
- recipe.product.kind = $1.to_i
- recipe.product.id = $2.to_i
- end
- if RB::Regex_Ingredient =~ contents
- ingredient_contents = $1
- ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|
- recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
- end
- end
- data[id] = recipe
- end
- outfile = File.open(outfile_name, "wb")
- Marshal.dump(data, outfile)
- outfile.close
- rescue
- p 1
- end
- end
- end
- class Game_Party
- alias rb_initialize_20141226 initialize
- def initialize
- rb_initialize_20141226
- @recipes = {}
- end
- def recipe_learn?(recipe_id)
- @recipes ||= {}
- return @recipes[recipe_id]
- end
- def learn_recipe(recipe_id)
- @recipes ||= {}
- @recipes[recipe_id] = true
- end
- def forget_recipe(recipe_id)
- @recipes ||= {}
- @recipes[recipe_id] = nil
- end
- def product_max_num(recipe_id)
- max = -1
- recipe = $data_recipes[recipe_id]
- return 0 if recipe.nil?
- recipe.ingredients.each do |ingredient|
- num_need = ingredient.number
- case ingredient.kind
- when 0
- num_current = item_number(ingredient.id)
- when 1
- num_current = weapon_number(ingredient.id)
- when 2
- num_current = armor_number(ingredient.id)
- else
- num_current = 0
- end
- max = num_current / num_need if max == -1 || num_current / num_need < max
- end
- return max == -1 ? 0 : max
- end
- end
- class Window_Product < Window_Selectable
- def initialize(mode = -1)
- super(0, 64, 240, 480 - 64)
- self.index = 0
- @mode = mode
- refresh
- end
- def recipe
- return @data[self.index]
- end
- def item
- recipe = self.recipe
- return nil if recipe.nil?
- product = recipe.product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- return item
- end
- def refresh
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- @data = []
- (1...$data_recipes.size).each do |i|
- if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
- @data << $data_recipes[i] if $data_recipes[i] != nil
- end
- end
- @item_max = @data.size
- if @item_max > 0
- self.contents = Bitmap.new(width - 32, @item_max * 32)
- @data.each_index{|i| draw_item(i)}
- else
- self.contents = Bitmap.new(width - 32, 32)
- self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
- end
- end
- def draw_item(index)
- x = 4
- y = 32 * index
- product = @data[index].product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- if item == nil
- return
- end
- disabled = $game_party.product_max_num(@data[index].id) == 0
- bitmap = RPG::Cache.icon(item.icon_name)
- opacity = disabled ? 128 : 255
- self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 176, 32, item.name)
- end
- def update_help
- @help_window.set_text(self.item == nil ? "" : self.item.description)
- end
- end
- class Window_Ingredients < Window_Base
- def initialize
- super(240, 288, 400, 192)
- @waiting = 0
- @dy = 1
- @recipe = nil
- refresh
- end
- def recipe=(recipe)
- if @recipe != recipe
- @recipe = recipe
- @waiting = 40
- @dy = 1
- self.oy = 0
- refresh
- end
- end
- def refresh
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- if @recipe == nil
- return
- end
- h = 32 * @recipe.ingredients.size
- if h > 0
- self.contents = Bitmap.new(width - 32, h)
- @recipe.ingredients.each_with_index do |ingredient, index|
- draw_item(ingredient, index)
- end
- end
- end
- def draw_item(ingredient, index)
- x = 4
- y = index * 32
- case ingredient.kind
- when 0
- item = $data_items[ingredient.id]
- num = $game_party.item_number(ingredient.id)
- when 1
- item = $data_weapons[ingredient.id]
- num = $game_party.weapon_number(ingredient.id)
- when 2
- item = $data_armors[ingredient.id]
- num = $game_party.armor_number(ingredient.id)
- else
- item = nil
- end
- if item == nil
- return
- end
- bitmap = RPG::Cache.icon(item.icon_name)
- disabled = num < ingredient.number
- opacity = disabled ? 128 : 255
- self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 192, 32, item.name)
- self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
- self.contents.font.color = normal_color
- self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
- end
- def update
- super
- if self.contents != nil && self.contents.height > self.height - 32
- if @waiting > 0
- @waiting -= 1
- return
- end
- self.oy += @dy
- if self.oy + self.height - 32 >= self.contents.height
- @dy = -1
- @waiting = 60
- end
- if self.oy <= 0
- @dy = 1
- @waiting = 60
- end
- end
- end
- end
- class Window_Craft_Number < Window_Base
- def initialize
- super(240, 64, 400, 160)
- self.contents = Bitmap.new(width - 32, height - 32)
- @recipe = nil
- @max = 1
- @number = 1
- @number_current = 0
- end
- def recipe=(recipe)
- if @recipe != recipe
- @recipe = recipe
- reset
- refresh
- end
- end
- def reset
- if @recipe == nil
- @number = 0
- @number_current = 0
- return
- end
- product = @recipe.product
- case product.kind
- when 0
- @number_current = $game_party.item_number(product.id)
- when 1
- @number_current = $game_party.weapon_number(product.id)
- when 2
- @number_current = $game_party.armor_number(product.id)
- else
- @number_current = 0
- end
- @max = $game_party.product_max_num(@recipe.id)
- @max = [@max, 99 - @number_current].min
- @number = @max == 0 ? 0 : 1
- end
- def number
- return @number
- end
- #--------------------------------------------------------------------------
- # ● 刷新
- #--------------------------------------------------------------------------
- def refresh
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(4, 0, 364, 32, "所持数")
- self.contents.draw_text(4, 32, 364, 32, "最大可合成数")
- self.contents.draw_text(4, 64, 364, 32, "当前合成数量")
- self.contents.font.color = normal_color
- self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
- self.contents.draw_text(4, 32, 364, 32, @max.to_s, 2)
- self.contents.draw_text(252, 96, 32, 32, "×")
- self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
- end
- def update
- super
- if self.active
- # 光标右 (+1)
- if Input.repeat?(Input::RIGHT) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number += 1
- refresh
- end
- # 光标左 (-1)
- if Input.repeat?(Input::LEFT) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number -= 1
- refresh
- end
- # 光标上 (+10)
- if Input.repeat?(Input::UP) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number = [@number + 10, @max].min
- refresh
- end
- # 光标下 (-10)
- if Input.repeat?(Input::DOWN) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number = [@number - 10, 1].max
- refresh
- end
- end
- end
- end
- class Window_Craft_Label < Window_Base
- def initialize
- super(240, 224, 400, 64)
- self.contents = Bitmap.new(width - 32, height - 32)
- refresh
- end
- def refresh
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(0, 0, width - 32, 32, "材料名称 现有 需要")
- end
- end
- class Scene_Title
- alias rb_main_20141226 main
- def main
- if $DEBUG
- RPG.extract_recipe
- end
- $data_recipes = load_data("Data/Recipes.rxdata")
- rb_main_20141226
- end
- end
- class Scene_Craft
- def initialize(mode = -1)
- @mode = mode
- end
- def main
- @product_window = Window_Product.new(@mode)
- @help_window = Window_Help.new
- @product_window.help_window = @help_window
- @ingredient_window = Window_Ingredients.new
- @number_window = Window_Craft_Number.new
- @label_window = Window_Craft_Label.new
- Graphics.transition
- loop do
- Graphics.update
- Input.update
- update
- if $scene != self
- break
- end
- end
- Graphics.freeze
- @product_window.dispose
- @help_window.dispose
- @ingredient_window.dispose
- @number_window.dispose
- @label_window.dispose
- end
- def update
- @product_window.update
- @ingredient_window.update
- @ingredient_window.recipe = @product_window.recipe
- @number_window.recipe = @product_window.recipe
- if @product_window.active
- update_product
- return
- end
- if @number_window.active
- update_number
- end
- end
- def update_product
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- $scene = Scene_Map.new
- return
- end
- if Input.trigger?(Input::C)
- recipe = @product_window.recipe
- if recipe == nil || $game_party.product_max_num(recipe.id) == 0
- $game_system.se_play($data_system.buzzer_se)
- return
- end
- $game_system.se_play($data_system.decision_se)
- @number_window.cursor_rect.set(304, 96, 32, 32)
- @product_window.active = false
- @number_window.active = true
- return
- end
- end
- def update_number
- @number_window.update
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- @number_window.active = false
- @product_window.active = true
- @number_window.cursor_rect.empty
- return
- end
- if Input.trigger?(Input::C)
- $game_system.se_play($data_system.shop_se)
- recipe = @product_window.recipe
- case recipe.product.kind
- when 0
- $game_party.gain_item(recipe.product.id, @number_window.number)
- when 1
- $game_party.gain_weapon(recipe.product.id, @number_window.number)
- when 2
- $game_party.gain_armor(recipe.product.id, @number_window.number)
- end
- recipe.ingredients.each do |ingredient|
- case ingredient.kind
- when 0
- $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
- when 1
- $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
- when 2
- $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
- end
- end
- @product_window.refresh
- @ingredient_window.refresh
- @number_window.reset
- @number_window.refresh
- @product_window.active = true
- @number_window.active = false
- @number_window.cursor_rect.empty
- return
- end
- end
- end
复制代码
作者: soulsaga 时间: 2021-8-14 09:25
实际上你可以给Game_System加东西,很多脚本都是这么做的。
class Game_System
attr_accessor :精灵
def 精灵;return @精灵||=[];end
end
class Game_System
attr_accessor :精灵
def 精灵;return @精灵||=[];end
end
只要不进行初始化就可以兼容旧存档...
作者: 939034448 时间: 2022-2-7 05:42
又回来啦,我找到一条新的思路,用六个变量当成数组,就可以用一个变量来储存所有精灵的对应属性加成啦,六个变量正好储存六种属性
欢迎光临 Project1 (https://rpg.blue/) |
Powered by Discuz! X3.1 |