Group Switches v1.1
#1
Group Switches
Version: 1.1

Introduction
This small code allows game makers to turn on, off, "reverse" or "random" the values of a group of switches.

Features
  • Turn on/off a group of switches through script call.
  • Flip values of certain switches through script call.
  • Sets certain switches on/off at random through script call.

Screenshots
No screenshots.

Demo
No demo.

Script
Code:
=begin
????????????????????????????????????????????????????????????????????????????????
? Group Switches v1.1 by PK8, November 9th-11th, 2009.                         ?
? http://rmvxp.com                                                             ?
????????????????????????????????????????????????????????????????????????????????
? ? Table of Contents                                                          ?
? ?? Author's Notes                - Line 14?16                                ?
? ?? Introduction & Description    - Line 18?20                                ?
? ?? Features                      - Line 22?25                                ?
? ?? How to Use                    - Line 27?51                                ?
? ?? Credit & Thanks               - Line 53?57                                ?
? ?? Changelog                     - Line 59?61                                ?
????????????????????????????????????????????????????????????????????????????????
? ? Author's Notes                                                             ?
? Boredom + running out of ideas = this script.                                ?
? The "random" and range features were last minute additions to this script.   ?
????????????????????????????????????????????????????????????????????????????????
? ? Introduction & Description                                                 ?
? This small code allows game creators to turn on, off or flip the values of a ?
? group of switches with a script call.                                        ?
????????????????????????????????????????????????????????????????????????????????
? ? Features                                                                   ?
? ? Turn on/off a group of switches through script call.                       ?
? ? Flip values of certain switches through script call.                       ?
? ? Sets certain switches on/off at random through script call.                ?
????????????????????????????????????????????????????????????????????????????????
? ? How to Use                                                                 ?
?   Call script: group_switch(bool, any number of switches)                    ?
?     bool:                                                                    ?
?      true  / "on":    turns on a group of switches.                          ?
?      false / "off":   turns off a group of switches.                         ?
?      "reverse":       flips values of a group switches.                      ?
?      "random" / "?":  Either sets a group of switches on or off at random.*  ?
?                                                                              ?
?   Example:                                                                   ?
?     group_switch(true, 1,12,18,20,8)                                         ?
?       The above script call turns on switches 1, 12, 18, 20 and 8.           ?
?                                                                              ?
?     group_switch(false, 5,20,8,10,1,32,64,99)                                ?
?       The above script call turns off switches 5, 20, 8, 10, 1, 32, 64 and 99?
?                                                                              ?
?     group_switch("reverse", 1,2,3,4,5,6,7,8)                                 ?
?       The above script call flips values of switches: 1, 2, 3, 4, 5, 6, 7 & 8?
?       The switches that were turned on before would be turned off.           ?
?       The opposite applies to switches that were turned off.                 ?
?                                                                              ?
?   Also: You could use ranges in the arguments. Here's an example: *          ?
?     group_switch(true, 1..10, 18, 12..15)                                    ?
?     The above call will turn on switches 18, 1 through 10 and 12 through 15. ?
?                                                                              ?
? * denotes last minute feature added to the script.                           ?
????????????????????????????????????????????????????????????????????????????????
? ? Credits & Thanks                                                           ?
? DerVVulfman: Requested allowing users to use ranges as an argument.          ?
? EJlol: Saw that I was using strings for ranges and asked why didn't I use    ?
?        ranges. From there, I learned a bit about ranges and went on to use   ?
?        ranges instead of strings. :)                                         ?
????????????????????????????????????????????????????????????????????????????????
? ? Changelog                                                                  ?
? v1.1 (Nov. 12th, 2009): Some users preferred the method name being           ?
?                         group_switches so here it is!                        ?
????????????????????????????????????????????????????????????????????????????????
=end

#==============================================================================
# IMPORTANT! RMVX users, set this to false!
#==============================================================================
class PK8
  Group_Switch_RMXP = true # true if using RMXP, false if using RMVX.
end

#==============================================================================
# ** Interpreter
#------------------------------------------------------------------------------
#  This interpreter runs event commands. This class is used within the
#  Game_System class and the Game_Event class.
#==============================================================================

if PK8::Group_Switch_RMXP == true
  class Interpreter
    def group_switch(bool, *switches)
      switches.each_index { | index |
        # Pushes everything in the range to the switches array.
        if switches[index].is_a?(Range) == true
          switches[index].each {| n | switches.push(n) }
          switches[index] = nil
        end
      }
      switches.compact!
     
      # This is where it does it's business.
      case bool
      when true, 'on'
        switches.each { | i | $game_switches[i] = true }
      when false, 'off'
        switches.each { | i | $game_switches[i] = false }
      when 'reverse', 'flip', 'invert'
        switches.each { | i |
          if $game_switches[i] == false
            $game_switches[i] = true
          else
            $game_switches[i] = false
          end
        }
      when 'random', '?'
        switches.each { | i |
          p = rand(2)
          $game_switches[i] = true  if p == 0
          $game_switches[i] = false if p == 1
        }
      end
      return
    end
   
    # Alternate Call.
    def group_switches(bool, *switches)
      group_switch(bool, *switches)
    end
  end
end

#==============================================================================
# ** Game_Interpreter
#------------------------------------------------------------------------------
#  An interpreter for executing event commands. This class is used within the
# Game_Map, Game_Troop, and Game_Event classes.
#==============================================================================

if PK8::Group_Switch_RMXP == false
  class Game_Interpreter
    def group_switch(bool, *switches)
      switches.each_index { | index |
        # Pushes everything in the range to the switches array.
        if switches[index].is_a?(Range) == true
          switches[index].each {| n | switches.push(n) }
          switches[index] = nil
        end
      }
      switches.compact!
     
      # This is where it does it's business.
      case bool
      when true, 'on'
        switches.each { | i | $game_switches[i] = true }
      when false, 'off'
        switches.each { | i | $game_switches[i] = false }
      when 'reverse', 'flip', 'invert'
        switches.each { | i |
          if $game_switches[i] == false
            $game_switches[i] = true
          else
            $game_switches[i] = false
          end
        }
      when 'random', '?'
        switches.each { | i |
          p = rand(2)
          $game_switches[i] = true  if p == 0
          $game_switches[i] = false if p == 1
        }
      end
      return
    end
   
    # Alternate Call.
    def group_switches(bool, *switches)
      group_switch(bool, *switches)
    end
  end
end

Instructions
Instructions are in the script but I'll post some here. To change the values of a group of switches, use the "Script..." event command. Here's how the call works.
Code:
group_switch(bool, switch ids here)
bool can be one of the following:
  • true / "on": Turns on switches.
  • false / "off": Turns off switches.
  • "reverse" / "flip" / "invert": Flips the value of switches.

    Example:
    If Switch ID 1 and 2 was on, and Switch ID 3 was off, and if the user decided to use the call: group_switch("reverse", 1,2,3) then Switch IDs 1 and 2 would be turned off while 3 would turn on.

  • "random" / "?": Either turns the switch on or off at random.

In the second argument, you could list as many switches as you can. You could use a number or use ranges.

Examples:
group_switch(true, 1..8) would turn on switches 1 to 8.
group_switch(true, 1...8) would turn on switches 1 to 7.
group_switch(true, 1,2,3,4,5,6,7,8) would turn on switches 1 to 8.
group_switch(true, 1..5, 7, 10..14) would turn on switches 1 to 5, 7, and 10 to 14.


FAQ
Awaiting question.

Compatibility
It doesn't mess with any methods of existing classes. :o

Credits and Thanks
DerVVulfman: Requested allowing users to use ranges as an argument.
EJlol: Saw that I was using strings for ranges and asked why didn't I use ranges. From there, I learned a bit about ranges and went on to use ranges instead of strings. :)

Author's Notes
Boredom + running out of ideas = this script. The "random" and range features were last minute additions to this script.

Terms and Conditions
I'd like credit for it. :p
Reply }
#2
Will make sure I use this for my next game. Nice work.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Troop : Self-Switches Kain Nobel 0 4,457 06-03-2016, 09:40 AM
Last Post: Kain Nobel
   Troop : Self Switches Kain Nobel 1 5,851 02-02-2013, 01:07 PM
Last Post: yamina-chan
   Group Variables v1.1 PK8 1 6,771 11-12-2009, 05:35 AM
Last Post: earthboy
   [Unsupported] Actor & Party's Self Switches XP PK8 3 8,812 09-17-2009, 01:46 AM
Last Post: Yin



Users browsing this thread: 2 Guest(s)