Advertisement
mikb89

[Ace] Permanent Switches & Variables v1.2

Jan 6th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.66 KB | None | 0 0
  1. # Permanent Switches & Variables v. 1.2
  2. # VX Ace version
  3. # by mikb89
  4.  
  5. # Dettagli:
  6. #  Questo script permette di memorizzare determinate switch e variabili in un
  7. #   file a parte, facendo sì che i loro valori rimangano gli stessi per ogni
  8. #   nuova partita e caricamento.
  9. #  Utile per esempio se si vuole aggiungere un Nuovo gioco +, cambiando una
  10. #   switch appena finito il gioco, oppure un sistema di punteggio alto tramite
  11. #   variabile o magari per memorizzare delle opzioni.
  12.  
  13. # Configurazioni:
  14. module PERMASV
  15.   PERMANENT_SWITCHES = [1...10, 20]
  16.   PERMANENT_VARIABLES = [50..10]
  17.    # Elencare le switch e variabili che si vogliono conservare.
  18.    # È possibile separare i valori con virgole (1, 2, 30) oppure scegliere un
  19.    #  range di valori multipli usando due o tre puntini:
  20.    # 1...10 -> (3 puntini) prende tutti i valori da 1 a 9;
  21.    # 50..100 -> (2 puntini) prende tutti i valori da 50 a 100.
  22.    # Come mostrato sopra i tre metodi sono combinabili fra loro ^^
  23.   FILE_NAME = "Data.rvdata2"
  24.    # Qui il nome del file salvato con switch e variabili permanenti.
  25.    # Mantenendo l'estensione .rvdata2, si confonderà di più con gli altri file
  26.    #  di salvataggio.
  27.    # Attenti a non mettere Save nel nome perché altrimenti lo considererà come
  28.    #  un salvataggio normale e abiliterà il comando Continua dal menu anche se
  29.    #  non sono effettivamente presenti salvataggi tradizionali.
  30. end
  31.  
  32. #Codename: permasv
  33.  
  34. ($imported ||= {})[:mikb89_permasv] = true
  35.  
  36. # License:
  37. # - You can ask me to include support for other scripts as long as these scripts
  38. #   use the $imported[script] = true;
  39. # - You can modify and even repost my scripts, after having received a response
  40. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  41. #   you can't do a post with the script as is;
  42. # - You can use my scripts for whatever you want, from free to open to
  43. #   commercial games. I'd appreciate by the way if you let me know about what
  44. #   you're doing;
  45. # - You must credit me, if you use this script or part of it.
  46.  
  47. module DataManager
  48.   def self.save_permasv
  49.     File.open(PERMASV::FILE_NAME, "wb") do |file|
  50.       Marshal.dump(make_save_permasv, file)
  51.     end
  52.   end
  53.   def self.load_permasv
  54.     begin
  55.       File.open(PERMASV::FILE_NAME, "rb") do |file|
  56.         extract_save_permasv(Marshal.load(file))
  57.       end
  58.     rescue
  59.     end
  60.   end
  61.   def self.make_save_permasv
  62.     cont = {}
  63.     for sw in PERMASV::PERMANENT_SWITCHES
  64.       cont["s#{sw}".to_sym] = $game_switches[sw]
  65.     end
  66.     for va in PERMASV::PERMANENT_VARIABLES
  67.       cont["v#{va}".to_sym] = $game_variables[va]
  68.     end
  69.     return cont
  70.   end
  71.   def self.extract_save_permasv(cont)
  72.     for sw in PERMASV::PERMANENT_SWITCHES
  73.       $game_switches[sw] = (cont["s#{sw}".to_sym] rescue false)
  74.     end
  75.     for va in PERMASV::PERMANENT_VARIABLES
  76.       $game_variables[va] = (cont["v#{va}".to_sym] rescue 0)
  77.     end
  78.   end
  79. end
  80.  
  81. class Game_Switches
  82.   alias_method(:getS_b4_permasv, :[]) unless method_defined?(:getS_b4_permasv)
  83.   alias_method(:setS_b4_permasv, :[]=) unless method_defined?(:setS_b4_permasv)
  84.   def [](switch_id)
  85.     DataManager.load_permasv
  86.     getS_b4_permasv(switch_id)
  87.   end
  88.   def []=(switch_id, value)
  89.     setS_b4_permasv(switch_id, value)
  90.     DataManager.save_permasv
  91.   end
  92. end
  93.  
  94. class Game_Variables
  95.   alias_method(:getV_b4_permasv, :[]) unless method_defined?(:getV_b4_permasv)
  96.   alias_method(:setV_b4_permasv, :[]=) unless method_defined?(:setV_b4_permasv)
  97.   def [](variable_id)
  98.     DataManager.load_permasv
  99.     getV_b4_permasv(variable_id)
  100.   end
  101.   def []=(variable_id, value)
  102.     setV_b4_permasv(variable_id, value)
  103.     DataManager.save_permasv
  104.   end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement