ezmash

Linked Switches (VX Ace)

Nov 26th, 2013
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #============================================================================
  2. # Linked Switches
  3. # v1.0 by Shaz
  4. #----------------------------------------------------------------------------
  5. # This script allows you to 'link' a switch with some other element in
  6. # your game, so whenever you refer to that switch (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 Switches to set a switch, prior
  10. # to using that switch in a conditional branch.
  11. # Note - the reference does not (currently) work in the other direction (nor
  12. # do I plan to make it) - setting a linked switch to a value will not update
  13. # whatever that link references.  (For example, if you add a link so switch
  14. # 1 references $game_actors[1].battle_member?, whenever you 'read' switch 1, it
  15. # will return true or false, depending on whether actor 1 is a battler.  
  16. # But if you 'change' switch 1, this will not add or remove actor 1 from
  17. # your battlers
  18. #----------------------------------------------------------------------------
  19. # To Install:
  20. # Copy and paste into a new script slot in Materials.  This script aliases
  21. # existing methods, so can go below all other custom scripts.
  22. #----------------------------------------------------------------------------
  23. # To Use:
  24. # Add an element in the init_links method for the switch id, along with
  25. # the script command to return the required value (it must return a true/false
  26. # value).
  27. # See comments in that method (at the bottom of this script) for some
  28. # simple examples
  29. #----------------------------------------------------------------------------
  30. # Terms:
  31. # Use in free or commercial games
  32. # Credit Shaz
  33. #============================================================================
  34.  
  35. class Game_Switches
  36.   #--------------------------------------------------------------------------
  37.   # * Object Initialization
  38.   #--------------------------------------------------------------------------
  39.   alias shaz_linked_switches_game_switches_initialize initialize
  40.   def initialize
  41.     shaz_linked_switches_game_switches_initialize
  42.     init_links
  43.   end
  44.   #--------------------------------------------------------------------------
  45.   # * Get Switch
  46.   #--------------------------------------------------------------------------
  47.   alias :data :[]
  48.   def [](switch_id)
  49.     init_links if @links.nil?
  50.     !@links[switch_id].nil? ? eval(@links[switch_id]) : data(switch_id)
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # * Initialize Links
  54.   #--------------------------------------------------------------------------
  55.   def init_links
  56.     @links = []
  57.  
  58. =begin
  59.     EXAMPLES
  60.     This command sets switch 1 to true when the party leader's hp is below 50%
  61.     @links[1] = '$game_party.leader.hp_rate < 0.5'
  62.    
  63.     This command sets switch 2 to true when actor 1 is the leader
  64.     @links[2] = '$game_party.leader.actor_id == 1'
  65.    
  66.     This command sets switch 3 to true when the player is on map 5, 8 or 10
  67.     @links[3] = '[5, 8, 10].include?($game_map.map_id)'
  68. =end
  69.   end
  70. end
RAW Paste Data