Script-Based Switches (Written by Peter O.)
This script section is designed to enable advanced control over event handling
by assigning certain switches to an arbitrary script. This script also contains
support for temporary self-switches and event-specific variables.
For script-based switches, the switch is true as long as the condition on the switch's
name remains true. Script-based switches are distinguished from regular switches
using the prefix "s:". Be aware that due to the size limit on a switch's name, it may
be necessary to use this kind of switch to call other methods
that return a true or false value. Methods on script-based switches are called within
the context of the Game_Event class.
Only switches on map events are supported, not those on common events, and
only within the "Conditions" section of an event page.
Temporary Self Switches
Each event in the game comes with a set of temporary self switches. This set
is cleared whenever a map is entered. To check whether a temporary self switch
is on, use a switch named "s:tsOn?(X)" where X is one of "A", "B", "C", or "D".
To check whether it's off, use a switch named "s:tsOff?(X)". To turn a temporary
self switch on or off, use a script like:
setTempSwitchOn("A")
or:
setTempSwitchOff("A")
Event-Specific Variables
Each event in the game comes with its own variable, which can be used for any purpose.Â
To retrieve a variable, call the script function "getVariable", and to set the value of the
current event's variable, call the script function "setVariable(X)", where X is the value
to set to the variable. To check the variable's value in an event page's condition, use the
event function "variable" (or "varAsInt", meaning "variable as integer") to help you. For example, to check whether the event's variable is equal to 1, you would use a switch named: "s:varAsInt==1".
Checking Self Switches
A script-based switch may be useful, among other things, for checking whether
a self-switch is off as one of an event page's conditions. This is possible by using a
switch whose name is: s:isOff?("A")Â etc.
The code for script-based switches follows.
This script section is designed to enable advanced control over event handling
by assigning certain switches to an arbitrary script. This script also contains
support for temporary self-switches and event-specific variables.
For script-based switches, the switch is true as long as the condition on the switch's
name remains true. Script-based switches are distinguished from regular switches
using the prefix "s:". Be aware that due to the size limit on a switch's name, it may
be necessary to use this kind of switch to call other methods
that return a true or false value. Methods on script-based switches are called within
the context of the Game_Event class.
Only switches on map events are supported, not those on common events, and
only within the "Conditions" section of an event page.
Temporary Self Switches
Each event in the game comes with a set of temporary self switches. This set
is cleared whenever a map is entered. To check whether a temporary self switch
is on, use a switch named "s:tsOn?(X)" where X is one of "A", "B", "C", or "D".
To check whether it's off, use a switch named "s:tsOff?(X)". To turn a temporary
self switch on or off, use a script like:
setTempSwitchOn("A")
or:
setTempSwitchOff("A")
Event-Specific Variables
Each event in the game comes with its own variable, which can be used for any purpose.Â
To retrieve a variable, call the script function "getVariable", and to set the value of the
current event's variable, call the script function "setVariable(X)", where X is the value
to set to the variable. To check the variable's value in an event page's condition, use the
event function "variable" (or "varAsInt", meaning "variable as integer") to help you. For example, to check whether the event's variable is equal to 1, you would use a switch named: "s:varAsInt==1".
Checking Self Switches
A script-based switch may be useful, among other things, for checking whether
a self-switch is off as one of an event page's conditions. This is possible by using a
switch whose name is: s:isOff?("A")Â etc.
The code for script-based switches follows.
Code:
SDK.log("Script-Based Switches", "Peter O.", 1.0, "10/23/07")
if SDK.state("Script-Based Switches") == true
class Game_Event
 alias petero_scriptswitches_Game_Event_initialize initialize
 def initialize(map_id, event)
  @tempSwitches={}
  petero_scriptswitches_Game_Event_initialize(map_id,event)
 end
 def name
 return @event.name
 end
 def id
 return @event.id
 end
 def tsOn?(c)
  return @tempSwitches && @tempSwitches[c]==true
 end
 def tsOff?(c)
  return !@tempSwitches || !@tempSwitches[c]
 end
 def setTempSwitchOn(c)
  @tempSwitches[c]=true Â
  refresh
 end
 def setTempSwitchOff(c)
  @tempSwitches[c]=false
  refresh
 end
 def variable
  return nil if !$game_eventvars
  return $game_eventvars[[@map_id,@event.id]]
 end
 def varAsInt
  return 0 if !$game_eventvars
  return $game_eventvars[[@map_id,@event.id]].to_i
 end
 def expired?(secs=86400)
  ontime=self.variable
  return ontime && (Time.now.to_i>ontime+secs)
 end
 def onEvent?
  return @map_id==$game_map.map_id &&
   $game_player.x==self.x && $game_player.y==self.y
 end
 def isOff?(c)
  return !$game_self_switches[[@map_id,@event.id,c]]
 end
 def switchIsOn?(id)
  switchname=$data_system.switches[id]
  return false if !switchname
  if switchname[/^s\:/]
   return eval($~.post_match)
  else
   return $game_switches[id]
  end
 end
 def refresh_trigger_conditions
  # Check in order of large event pages
  for page in @event.pages.reverse
   # Make possible referrence for event condition with c
   c = page.condition
   # Switch 1 condition confirmation
   if c.switch1_valid
    if switchIsOn?(c.switch1_id) == false # Modification
     next
    end
   end
   # Switch 2 condition confirmation
   if c.switch2_valid
    if switchIsOn?(c.switch2_id) == false # Modification
     next
    end
   end
   # Variable condition confirmation
   if c.variable_valid
    if $game_variables[c.variable_id] < c.variable_value
     next
    end
   end
   # Self switch condition confirmation
   if c.self_switch_valid
    key = [@map_id, @event.id, c.self_switch_ch]
    if $game_self_switches[key] != true
     next
    end
   end
   # Set local variable: new_page
   new_page = page
   # Remove loop
   break
  end
  # Return new page
  return new_page
 end
end
class Interpreter
def setEventTime(*arg)
 time=Time.now.to_i
 setSelfSwitch(@event_id,"A",true)
 $game_eventvars[[@map_id,@event_id]]=time
 for otherevt in arg
  setSelfSwitch(otherevt,"A",true)
  $game_eventvars[[@map_id,otherevt]]=time
 end
end
def getVariable
 return $game_eventvars[[@map_id,@event_id]]
end
def getVariableAsInt
 value=$game_eventvars[[@map_id,@event_id]]
 return value ? value.to_i : 0
end
def setVariable(value)
 $game_eventvars[[@map_id,@event_id]]=value
end
def setTempSwitchOn(c)
 get_character(0).setTempSwitchOn(c)
end
def setTempSwitchOff(c)
 get_character(0).setTempSwitchOff(c)
end
def setSelfSwitch(event,swtch,value)
 $game_self_switches[[@map_id,event,swtch]]=value
 $game_map.need_refresh = true
end
end
class Scene_Load < Scene_File
 alias petero_scriptswitches_Scene_Load_read_save_data read_save_data
 def read_save_data(file)
  petero_scriptswitches_Scene_Load_read_save_data(file)
  $game_eventvars=Marshal.load(file)
 end
end
class Scene_Save < Scene_File
 alias petero_scriptswitches_Scene_Save_write_save_data write_save_data
 def write_save_data(file)
  petero_scriptswitches_Scene_Save_write_save_data(file)
  Marshal.dump($game_eventvars,file)
 end
end
class Scene_Title
 alias petero_scriptswitches_Scene_Title_command_new_game command_new_game
 def command_new_game
  $game_eventvars={}
  petero_scriptswitches_Scene_Title_command_new_game
 end
end
end