Turn switch on/off during fadein/fadeout

So, I've been learning a bit of Ruby and I decided to try my hand at making a simple script that turns some switches off during screen fadeout and then turns them back on again during fadein. The reason I wanted to do this is because the lighting script I'm using (Casper Gaming's Light Effects script) doesn't turn the lights off when you fade the screen out and I always thought this was a bit annoying and clunky. I went through the code and tried to find some methods to use, but I'm not quite at that level yet. However, all of the lighting effects I am using in my game are bound to switches, so I thought I would use those.

It does work fine (from what I can tell), which is super exciting, because it's the first script I've ever made, but I thought I'd post it here to see if anyone had any suggestions for making it tidier or coding it better. I originally didn't have the editable region, but I made it just to practice.

This is for RPG Maker VX Ace, by the way.

Spoiler
Code:
module AMN_CSCAswitches
#=====================================================================#
#       EDITABLE REGION:                                              
#=====================================================================#

   AMN_CSCAswitch_one      =  [6]
   AMN_CSCAswitch_two      =  [7]
   AMN_CSCAswitch_three    =  [8]
   AMN_CSCAswitch_four     =  [9]


#=====================================================================#
#       Do not edit below this point.                                 
#=====================================================================#
end
#==============================================================================
# ** Game_Screen
#------------------------------------------------------------------------------
#  This class handles screen maintenance data, such as changes in color tone,
# flashes, etc. It's used within the Game_Map and Game_Troop classes.
#==============================================================================

class Game_Screen
  #--------------------------------------------------------------------------
  # * Update Fadeout
  #--------------------------------------------------------------------------
  alias amn_cscalights_updatefadeout update_fadeout
  def update_fadeout
    if @fadeout_duration > 0
      if $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_one]
        @amn_cscalights6on = true
      end
      if $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_two]
        @amn_cscalights7on = true
      end
      if $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_three]
        @amn_cscalights8on = true
      end
      if $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_four]
        @amn_cscalights9on = true
      end
      $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_one] = false
      $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_two] = false
      $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_three] = false
      $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_four] = false     
    end
    amn_cscalights_updatefadeout
  end

   #--------------------------------------------------------------------------
   # * Update Fadein
   #--------------------------------------------------------------------------
   alias amn_cscalights_updatefadein update_fadein
   def update_fadein
     if @fadein_duration != 0
       $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_one] = @amn_cscalights6on
       $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_two] = @amn_cscalights7on
       $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_three] = @amn_cscalights8on
       $game_switches[*AMN_CSCAswitches::AMN_CSCAswitch_four] = @amn_cscalights9on
     end
     amn_cscalights_updatefadein
   end
end
Hmm.... can you just edit the class Game_Switches itself?
Like a certain ID would return true if condition are meet without bound to switching boolean

An example of mine using Galv Visibility Range
Code:
# ----------------------------------------------------------------------------
# Galv visibility patches
# ----------------------------------------------------------------------------
class Game_Map
  def note
    @map.note
  end
end

class Game_Switches
  # --------------------------------------------------------------------------
  # Visibility case for Galv Visibility. Coz I'm too lazy to do event.
  # --------------------------------------------------------------------------
  alias :ed_switch :[]
  def [](id)
    if id == 1
      return false if $game_map.note[/<visible>/i]
      return true
    end
    return ed_switch(id)
  end
 
end

class Game_Variables
  # --------------------------------------------------------------------------
  # Same. Coz I'm too lazy to do event
  # --------------------------------------------------------------------------
  alias :ed_var :[]
  def [](id)
    return 370 if id == 1
    return 247 if id == 2
    return ed_var(id)
  end
 
end
Galv visibility range are bound to switch as well. And since I'm too lazy to do switch ON/OFF, I'd just add <visible> notetag to a map that I wish it's going to be visible. The edit is basically, if a map has <visible> notetag, no matter what happen, it will always return false. And return true to the rest if the map has no <visible> tag. Reason is, my game is a dungeon crawler, most of places need a limited vision
@TheoAllen Okay, I've been thinking a bit more about this, so I thought I'd try delve more into the original script. In the original script, the switches are all assigned in arrays in the editable region, like this:

Code:
module CSCA
  module LIGHTS
    EFFECTS = [] # Don't Touch
    #
    #Set up your EFFECTS like this:
    #EFFECTS[x] = ["NAME", graphic, zoom_x, zoom_y, opacity, blend_type, tone,
    #              switch, rand_opacity, adjust_x, adjust_y, custom_z]
    #
    EFFECTS[0] = ["FIRE", "Light", 3, 3, 100, 1, Tone.new(255,-100,-255), 6, 10, 0, 0, 1000]

I've trimmed some of the excess code, but here is the full script if you want to look at it: https://www.rpgmakercentral.com/topic/8589-csca-light-effects/. So, I'm thinking there must be a way to change the switch in the array, but I'm not sure how to do it. Is there an easy way to access the 7th element of an array inside each array?

I'm guessing that I would do something like:
Code:
$game_switches[*CSCA::LIGHTS::EFFECTS[0][7]]
but I don't want to have to do that for each one ...
I'm not sure what do you actually want. But isn't it something like ... this?
Code:
[0,1,2,3,4,5].each do |eff_id|
  $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]] = true/false
end
All right, I've done some stuff, but it's telling me I've got the wrong number of arguments (12 for 1) for this line:
Code:
if $game_switches[*CSCA::LIGHTS::EFFECTS[eff_id][7]]

This is the full script so far:
Code:
class Game_Screen
  #--------------------------------------------------------------------------
  # * Update Fadeout
  #--------------------------------------------------------------------------
  alias amn_cscalights_updatefadeout update_fadeout
  def update_fadeout
    @AMNarr = Array.new
    [0..CSCA::LIGHTS::EFFECTS.size].each do |eff_id|
      if $game_switches[*CSCA::LIGHTS::EFFECTS[eff_id][7]]
        @AMNarr << eff_id
      end
    end
    amn_cscalights_updatefadeout
  end
 
   #--------------------------------------------------------------------------
   # * Update Fadein
   #--------------------------------------------------------------------------
   alias amn_cscalights_updatefadein update_fadein
   def update_fadein
     if @fadein_duration != 0
       @AMNarr.each do |swi_id|
         $game_switches[swi_id] = true
       end
     end
     amn_cscalights_updatefadein
   end
 end
If you put asterisk symbol (*), it means you're putting multiple arguments.
An example usage of asterisk symbol would be
Code:
def a_function(a,b,c,d)
  puts a
  puts b
  puts c
  puts d
end
arguments = ["Text1", "Text2", "Text3", "Text4"]

a_function(*argument)
You're putting multiple argument all in one
If you put more value into arguments array, like
Code:
arguments = ["Text1", "Text2", "Text3", "Text4". "Text5"]
it will throw error wrong number of arguments (5 for 4) or so

Back to the problem, $game_switches only need one argument, so it's not necessary to put asterisk symbol, so try to remove it
Code:
if $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]]
Try if it works

EDIT:
Actually scratch that.

You might actually could just do
Code:
CSCA::LIGHTS::EFFECTS.size.times do |eff_id|
instead of trying with
Code:
[0...array.size]
Oops, I must have left that there from when I was using my own method originally.

Hmmm, now I'm getting an error that says it can't turn an array into an integer.

When I bring up CSCA::LIGHTS::EFFECTS[0][7], for example, in a message box, I get the number of the switch, which is great. I'm not sure why it's saying it's an array.

EDIT: Aha, you ninja'd me. That works fine, thank you! Now I've got an issue where the switches are turning off, but they aren't turning back on. I think it's something to do with how I've done my own array, but I'm not sure what it is. I've got a messagebox telling me that it has put the elements into @AMNarr, but as soon as the screen fades out, it says that @AMNarr has nothing in it.

Code:
class Game_Screen
  #--------------------------------------------------------------------------
  # * Update Fadeout
  #--------------------------------------------------------------------------
  alias amn_cscalights_updatefadeout update_fadeout
  def update_fadeout
    @AMNarr = Array.new
    if @fadeout_duration < 10 && @fadeout_duration > 0
      CSCA::LIGHTS::EFFECTS.size.times do |eff_id|
        if $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]]
          @AMNarr << CSCA::LIGHTS::EFFECTS[eff_id][7]
        end
      end
      msgbox_p(@AMNarr)
      CSCA::LIGHTS::EFFECTS.size.times do |eff_id|
        $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]] = false
      end
    end
    amn_cscalights_updatefadeout
  end
 
   #--------------------------------------------------------------------------
   # * Update Fadein
   #--------------------------------------------------------------------------
   alias amn_cscalights_updatefadein update_fadein
   def update_fadein
     if @fadein_duration != 0
       @AMNarr.each do |swi_id|
         $game_switches[swi_id] = true
       end
     end
     amn_cscalights_updatefadein
   end
 end
but as soon as the screen fades out, it says that @AMNarr has nothing in it.
The problem lies on where you put @AMNarr = Array.new

You see, the update method goes like this
Code:
  def update
    update_fadeout
    update_fadein
    update_tone
    update_flash
    update_shake
    update_weather
    update_pictures
  end
In your update_fadeout, you initialize the @AMNarr as a new array outside the condition check (so it will be always an empty array)
So when it went through to update_fadein, it's already an empty array again.

It should be like this
Code:
  alias amn_cscalights_updatefadeout update_fadeout
  def update_fadeout
   if @fadeout_duration < 10 && @fadeout_duration > 0
     @AMNarr = Array.new # <-- put here
     CSCA::LIGHTS::EFFECTS.size.times do |eff_id|
       if $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]]
         @AMNarr << CSCA::LIGHTS::EFFECTS[eff_id][7]
       end
     end
     msgbox_p(@AMNarr)
     CSCA::LIGHTS::EFFECTS.size.times do |eff_id|
       $game_switches[CSCA::LIGHTS::EFFECTS[eff_id][7]] = false
     end
   end
   amn_cscalights_updatefadeout
  end

Get the idea?
Yep, that makes sense. I also figured out another issue—I was doing everything while the fadeout duration was between 10 and 0. Now I've made it so that putting things into the AMNarray happens when the fadeout duration is 10, and then everything else happens when the fadeout duration is between 10 and 0. Everything is working perfectly! Thanks so much!

Original thread: https://forums.rpgmakerweb.com/threads/turn-switch-on-off-during-fadein-fadeout.88839/