adiktuzmiko

Game Custom Data 1.26

Nov 24th, 2013
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.77 KB | None | 0 0
  1. =begin
  2.  Game Custom Data v1.26
  3.  by AdiktuzMiko
  4.  --- Date Created: 11/25/2013
  5.  --- Last Date Updated: 07/18/2015
  6.  --- Level: Easy
  7.  
  8.  This is a simple script which provides you with the capability of having custom data
  9.  for your game that is saved outside of the save files. Since it is outside of save
  10.  files, it is shared by all. You can even make multiple games share a single custom
  11.  data file.
  12.  
  13.  You can utilize this for having persistent data between your game/games.
  14.  
  15.  You can specify where to put the custom data file, what name will it have and even
  16.  what file extension.
  17.  
  18. ==============================================================================
  19.    ++ Installation ++
  20. ==============================================================================
  21.  Install this script in the Materials section in your project's
  22.  script editor.
  23.  
  24.  Modify the ADIK::DATA module to your liking
  25.  
  26. ==============================================================================
  27.    ++ Script Calls ++
  28. ==============================================================================
  29.  
  30.  CustomData.get(key) to get a certain value
  31.  CustomData.set(key,value) to set the value saved in "key" equal to "value"
  32.  CustomData.add(key,value) to add value to the current value of the data
  33.  CustomData.sub(key,value) to subtract value to the current value of the data
  34.  CustomData.mul(key,value) to multiply the current value of the data by the given value
  35.  CustomData.div(key,value) to divide the current value of the data by the given value
  36.  CustomData.push(key,value) to add the given value into an array saved as custom data
  37.  CustomData.delete(key,value) to remove the given value from an array saved as custom data
  38.  CustomData.save to save the current contents of custom data
  39.  
  40.  Note: Custom Data is loaded automatically upon running the game
  41.  
  42. ==============================================================================
  43.    ++ Compatibility ++
  44. ==============================================================================
  45.  This script aliases the following default VXA methods:
  46.  
  47.    DataManager#load_database
  48.      DataManager#load_game
  49.      Scene_Title#start
  50.      DataManager#save_game_without_rescue
  51. ==============================================================================
  52.    ++ Terms and Conditions ++
  53. ==============================================================================
  54.  
  55.  Read this: http://lescripts.wordpress.com/terms-and-conditions/
  56.  
  57. ==============================================================================
  58.  
  59.  
  60. =end
  61.  
  62. module ADIK
  63.   module DATA
  64.  
  65.   # Primary folder where the save data will be placed
  66.   # based on the default paths in your computer
  67.   # set to nil if you don't want to use this
  68.   DATA_PATH = 'AppData'
  69.    
  70. #~  Heres a list of parameters you can set the DATA_PATH into
  71. #~  
  72. #~  AppData = Contains the full path to the Application Data directory of the logged-in user
  73. #~  LOCALAPPDATA = This variable is the temporary files of Applications.
  74. #~      CURRENT = Folder where the game is run from
  75.  
  76. #~  !!!The following might require your game to run in admin mode!!!
  77.  
  78. #~  ProgramFiles = This variable points to Program Files directory
  79. #~  CommonProgramFiles = This variable points to Common Files directory
  80.    
  81.   #Enter each subfolder paths after the primary folder here
  82.   #The script automatically creates the folder path specified
  83.  
  84.   FOLDER = ["RPGMaker","Games"]
  85.      
  86.   #File name of the custom data file, include file extension
  87.   FILENAME = "Data.rvdata2"
  88.     #Does the system save the custom data every time that you change it
  89.     AUTOSAVE = true
  90.   #On this example settings given, the file will be saved into
  91.   #C:\Users\USER\AppData\Roaming\RPGMaker\Games\Data.rvdata2
  92.   end
  93. end
  94.  
  95. #==============================================================================
  96. # DO NOT EDIT BELOW THIS LINE
  97. #==============================================================================
  98.  
  99. module DataManager
  100.  
  101.   class <<self; alias load_database_customdata load_database; end
  102.   def self.load_database
  103.     load_database_customdata
  104.     if not ADIK::DATA::DATA_PATH == 'CURRENT'
  105.       @spath = ENV[ADIK::DATA::DATA_PATH] + "\\"
  106.     else
  107.       @spath = ""
  108.     end
  109.     ADIK::DATA::FOLDER.each do |path|
  110.       @spath += path + "\\"
  111.       Dir.mkdir(@spath) if !FileTest.exist?(@spath)
  112.     end
  113.     @path = @spath + ADIK::DATA::FILENAME
  114.     begin
  115.       load_customdata
  116.     rescue
  117.       $adik_custom_data = {}
  118.       save_customdata
  119.     end
  120.   end
  121.  
  122.   class <<self; alias load_game_customdata load_game; end
  123.   def self.load_game(index)
  124.     if load_game_customdata(index)
  125.       load_customdata unless ADIK::DATA::AUTOSAVE
  126.       return true
  127.     else
  128.       return false
  129.     end
  130.   end
  131.  
  132.   def self.customdata_filename
  133.     return  @path
  134.   end
  135.  
  136.   def self.customdata_file_exists?
  137.     FileTest.exist?(customdata_filename)
  138.   end
  139.  
  140.   class <<self; alias save_game_without_rescue_customdata save_game_without_rescue; end
  141.   def self.save_game_without_rescue(index)
  142.     save_game_without_rescue_customdata(index)
  143.     DataManager.save_customdata
  144.     return true
  145.   end
  146.  
  147.   def self.load_customdata
  148.     File.open(customdata_filename, "rb") do |file|
  149.       contents = Marshal.load(file)
  150.       $adik_custom_data = contents[:adik_custom_data]
  151.     end
  152.     return true
  153.   end
  154.  
  155.   def self.save_customdata
  156.     File.open(customdata_filename, "wb") do |file|
  157.       contents = {}
  158.       contents[:adik_custom_data] = $adik_custom_data
  159.       Marshal.dump(contents, file)
  160.     end
  161.     return true
  162.   end
  163.  
  164. end
  165.  
  166. module CustomData
  167.  
  168.   def self.get(key)
  169.     return $adik_custom_data[key]
  170.   end
  171.  
  172.   def self.set(key,value)
  173.     $adik_custom_data[key] = value
  174.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  175.   end
  176.  
  177.   def self.save
  178.     DataManager.save_customdata
  179.   end
  180.  
  181.   def self.add(key,value)
  182.     $adik_custom_data[key] += value
  183.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  184.   end
  185.  
  186.   def self.sub(key,value)
  187.     $adik_custom_data[key] -= value
  188.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  189.   end
  190.  
  191.   def self.mul(key,value)
  192.     $adik_custom_data[key] *= value
  193.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  194.   end
  195.  
  196.   def self.div(key,value)
  197.     $adik_custom_data[key] /= value
  198.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  199.   end
  200.  
  201.   def self.push(key,value)
  202.     $adik_custom_data[key] = [] if $adik_custom_data[key].nil?
  203.     $adik_custom_data[key].push(value)
  204.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  205.   end
  206.  
  207.   def self.delete(key,value)
  208.     $adik_custom_data[key].delete(value)
  209.     DataManager.save_customdata if ADIK::DATA::AUTOSAVE
  210.   end
  211.  
  212. end
  213.  
  214. class Scene_Title
  215.   alias custom_data_start start
  216.   def start
  217.    custom_data_start
  218.    DataManager.load_customdata unless ADIK::DATA::AUTOSAVE
  219.  end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment