ezmash

Linked Variables (VX Ace)

Nov 10th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.04 KB | None | 0 0
  1. #============================================================================
  2. # Linked Variables
  3. # v1.0 by Shaz
  4. #----------------------------------------------------------------------------
  5. # This script allows you to 'link' a variable with some other element in
  6. # your game, so whenever you refer to that variable (to SEE the value, not
  7. # to SET it), it goes off to check the link and returns the appropriate
  8. # value.
  9. # It is the equivalent of doing a Control Variables to set a variable, prior
  10. # to using that variable in a calculation, conditional branch, text box, etc.
  11. # Note - the reference does not (currently) work in the other direction (nor
  12. # do I plan to make it) - setting a linked variable to a value will not update
  13. # whatever that link references.  (For example, if you add a link so variable
  14. # 1 references $game_party.members.size, whenever you 'read' variable 1, it
  15. # will return the current party size.  But if you 'change' variable 1, the
  16. # party size will not be affected.
  17. #----------------------------------------------------------------------------
  18. # To Install:
  19. # Copy and paste into a new script slot in Materials.  This script aliases
  20. # existing methods, so can go below all other custom scripts.
  21. #----------------------------------------------------------------------------
  22. # To Use:
  23. # Add an element in the init_links method for the variable id, along with
  24. # the script command to return the required value.
  25. # See comments in that method (at the bottom of this script) for some
  26. # simple examples
  27. #----------------------------------------------------------------------------
  28. # Terms:
  29. # Use in free or commercial games
  30. # Credit Shaz
  31. #============================================================================
  32.  
  33. class Game_Variables
  34.   #--------------------------------------------------------------------------
  35.   # * Object Initialization
  36.   #--------------------------------------------------------------------------
  37.   alias shaz_linked_variables_game_variables_initialize initialize
  38.   def initialize
  39.     shaz_linked_variables_game_variables_initialize
  40.     init_links
  41.   end
  42.   #--------------------------------------------------------------------------
  43.   # * Get Variable
  44.   #--------------------------------------------------------------------------
  45.   alias :data :[]
  46.   def [](variable_id)
  47.     init_links if @links.nil?
  48.     !@links[variable_id].nil? ? eval(@links[variable_id]) : data(variable_id)
  49.   end
  50.   #--------------------------------------------------------------------------
  51.   # * Initialize Links
  52.   #--------------------------------------------------------------------------
  53.   def init_links
  54.     @links = []
  55.    
  56. =begin
  57.     EXAMPLES
  58.     This command tells variable 1 to return the current party size
  59.     @links[1] = '$game_party.members.size'
  60.    
  61.     This command tells variable 2 to return the number of Item[2] in inventory
  62.     @links[2] = '$game_party.item_number($data_items[2])'
  63.    
  64.     This command tells variable 3 to return the leader's HP Rate
  65.     @links[3] = '$game_party.leader.hp_rate'
  66. =end
  67.   end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment