Advertisement
Zeriab

[RMXP] Dev tool - Track variable/switch changes

Oct 20th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.56 KB | None | 0 0
  1. module Commentary
  2.   LOGFILE = 'Tracklog.txt'
  3.  
  4.   LIMIT_TRACKED_SWITCHES  = false #true
  5.   LIMIT_TRACKED_VARIABLES = false #true
  6.  
  7.   # Define which variable changes to track
  8.   TRACKED_SWITCHES = []
  9.   TRACKED_SWITCHES << 127
  10.   TRACKED_SWITCHES << 5
  11.  
  12.   # Define which switch changes to track
  13.   TRACKED_VARIABLES = []
  14.   TRACKED_VARIABLES << 157
  15.   TRACKED_VARIABLES << 17
  16.   TRACKED_VARIABLES << 49
  17.  
  18.  
  19.   BACK_COLOR = Color.new(0,0,0,200)
  20.   OLD_TEXT_COLOR = Color.new(200,200,200,200)
  21.   NEW_TEXT_COLOR = Color.new(255,255,255,255)
  22.   NEW_TEXT_OPACITY =  210
  23.   NEW_TEXT_WAIT = 60
  24.   MIN_OPACITY = 100
  25.   OPACITY_DIFF = NEW_TEXT_OPACITY - MIN_OPACITY
  26.   BOX_WIDTH = 580
  27.  
  28.   module_function
  29.  
  30.   def initialize
  31.     @sprite = Sprite.new
  32.     @sprite.x, @sprite.y = 640 - (BOX_WIDTH + 20), 20
  33.     @sprite.z = 0xFFFFFF
  34.     @sprite.bitmap = Bitmap.new(BOX_WIDTH,100)
  35.     @sprite.bitmap.font.size = 14
  36.     @sprite.bitmap.font.bold = true
  37.     @list = []
  38.     add_comment('') # For multiple runs in the log file
  39.     add_comment('Initialization done')
  40.   end
  41.  
  42.   def add_comment(text)
  43.     @list.unshift(text.to_s)
  44.     @list.pop if @list.size > 5
  45.     @need_refresh = true
  46.    
  47.     File.open(LOGFILE, 'ab') {|f|
  48.       f.print "#{text}\r\n"
  49.     }
  50.   end
  51.  
  52.   def refresh
  53.     @sprite.opacity = NEW_TEXT_OPACITY
  54.     @sprite.bitmap.fill_rect(0, 0, BOX_WIDTH, 100, BACK_COLOR)
  55.     @sprite.bitmap.font.color = OLD_TEXT_COLOR
  56.     for i in 1...@list.size
  57.       @sprite.bitmap.draw_text(8, 4+18*(4-i), BOX_WIDTH - 20, 20, @list[i], 2)
  58.     end
  59.     @sprite.bitmap.font.color = NEW_TEXT_COLOR
  60.     @sprite.bitmap.draw_text(8, 76, BOX_WIDTH - 20, 20, @list[0], 2)
  61.     @need_refresh = false
  62.     @update_wait = NEW_TEXT_WAIT
  63.   end
  64.  
  65.   def anim_update
  66.     if @update_wait > 0
  67.       @update_wait -= 1
  68.       return
  69.     end
  70.     if @sprite.opacity > MIN_OPACITY
  71.       o = (OPACITY_DIFF-(@sprite.opacity - MIN_OPACITY))/10
  72.       o = 4 if o <= 1
  73.       @sprite.opacity -= o
  74.       @update_wait = 2
  75.     end
  76.   end
  77.  
  78.   def update
  79.     if @sprite.nil?
  80.       initialize
  81.     end
  82.     if !$game_map.nil?
  83.       if @map.nil? || @map != $game_map.map_id
  84.         @map = $game_map.map_id
  85.       end
  86.      
  87.     end
  88.     refresh if @need_refresh
  89.     anim_update
  90.   end
  91.  
  92.   def variable_change(variable_id, old_value, new_value)
  93.     if (!LIMIT_TRACKED_VARIABLES || TRACKED_VARIABLES.include?(variable_id))
  94.       add_comment("Map #{$game_map.map_id}: Variable #{variable_id} changed to #{new_value} - #{Time.now}")
  95.     end
  96.   end
  97.   def switch_change(switch_id, old_value, new_value)
  98.     if (!LIMIT_TRACKED_SWITCHES || TRACKED_SWITCHES.include?(switch_id))
  99.       add_comment("Map #{$game_map.map_id}: Switch #{switch_id} changed to #{new_value} - #{Time.now}")
  100.     end
  101.   end
  102. end
  103.  
  104.  
  105. class Game_Switches
  106.   def []=(switch_id, value)
  107.     if switch_id <= 5000
  108.       # Register change
  109.       if (value != @data[switch_id])
  110.         Commentary.switch_change(switch_id, @data[switch_id], value)
  111.       end
  112.       # Normal functionality
  113.       @data[switch_id] = value
  114.     end
  115.   end
  116. end
  117. class Game_Variables
  118.   def []=(variable_id, value)
  119.     if variable_id <= 5000
  120.       # Register change
  121.       if (value != @data[variable_id])
  122.         Commentary.variable_change(variable_id, @data[variable_id], value)
  123.       end
  124.       # Normal functionality
  125.       @data[variable_id] = value
  126.     end
  127.   end
  128. end
  129.  
  130.  
  131. class << Graphics
  132.   unless self.method_defined?(:commentary_update)
  133.     alias :commentary_update :update
  134.   end
  135.   def update(*args)
  136.     commentary_update(*args)
  137.     Commentary.update
  138.   end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement