By default, in RPG maker XP the Self Switches are unlimited and can have any name, but their access via script can be a bit confusing, and via "engine" (event commands) you can only access four: A, B, C, D. Also, there's no such thing as Self Variables.
That's why I implemented my own Self Switches and Self Variables (they don't overwrite the default ones, you can use both types). They are very easy to use; they're common Switches and Variables, but their name starts with [Self]
Just like that, they will work like self switches, so their value will depend on the current event.
Note: Bear in mind this is script is a bit more incompatible than others since it modifies some methods on Interpreter and also a few on Game_Character and Game_Event. Those incompatibilities aren't hard to fix, but you'll need minimum knowledge on scripting to be able to do that.
Terms of use
- Giving credits (Wecoc) for using this script is optional, but appreciated.
- You can repost this code.
- You can edit this code freely, and distribute the edits as well.
- This code can be used on commercial games.
Basic instructions
All event commands work like always, they treat them as normal switches and variables, but their value is different in each event. It's that easy!
On Scene_Debug the self switches are always OFF and self variables are always 0, and trying to change them from there won't have any effect. Also you can't use them as Switch Condition on Common Events. In summary, they are 'local' for each event in each and every aspect, they simply don't have a global value.
Advanced instructions
You can get or modify its value on a specific event externally using:
$game_map.events[EVENT ID].self_switches[SWITCH ID]
$game_map.events[EVENT ID].self_switches[SWITCH ID] = VALUE
$game_map.events[EVENT ID].self_variables[VARIABLE ID]
$game_map.events[EVENT ID].self_variables[VARIABLE ID] = VALUE
You can check if a switch or variable are local using
There are shortcuts for each one of these calls when applied on the current event; they will work using only the last part, for example self_switches[SWITCH ID] = VALUE
Code
Ruby:
#==============================================================================# ** CSV for Events - Custom Self Switches & Self Variables v1.1#------------------------------------------------------------------------------# Author: Wecoc (no credits required)#==============================================================================class Game_Temp #-------------------------------------------------------------------------- # * Initialize #-------------------------------------------------------------------------- alias custom_self_ini initialize unless $@ def initialize custom_self_ini data = load_data("Data/System.rxdata") @switches = [] switches = data.switches for i in 1...switches.size if switches.upcase.include?("[SELF]") @switches.push(i) end end @variables = [] variables = data.variables for i in 1...variables.size if variables.upcase.include?("[SELF]") @variables.push(i) end end end #-------------------------------------------------------------------------- # * Check if a switch is a Self Switch #-------------------------------------------------------------------------- def self_switch?(id) return @switches.include?(id) end #-------------------------------------------------------------------------- # * Check if a variable is a Self Variable #-------------------------------------------------------------------------- def self_variable?(id) return @variables.include?(id) endend#==============================================================================# * Game_Event#==============================================================================class Game_Event attr_reader :self_switches attr_reader :self_variables #-------------------------------------------------------------------------- # * Initialize #-------------------------------------------------------------------------- alias custom_self_ini initialize unless $@ def initialize(*args) @self_switches = {} @self_switches.default = false @self_variables = {} @self_variables.default = 0 custom_self_ini(*args) end #-------------------------------------------------------------------------- # * Check if a switch is a Self Switch #-------------------------------------------------------------------------- def self_switch?(id) return $game_temp.self_switch?(id) end #-------------------------------------------------------------------------- # * Check if a variable is a Self Variable #-------------------------------------------------------------------------- def self_variable?(id) return $game_temp.self_variable?(id) end #-------------------------------------------------------------------------- # * Move Type : Custom #-------------------------------------------------------------------------- def move_type_custom return if jumping? or moving? while @move_route_index < @move_route.list.size command = @move_route.list[@move_route_index] if command.code == 0 if @move_route.repeat @move_route_index = 0 end unless @move_route.repeat if @move_route_forcing and not @move_route.repeat @move_route_forcing = false @move_route = @original_move_route @move_route_index = @original_move_route_index @original_move_route = nil end @stop_count = 0 end return end if command.code <= 14 case command.code when 1 ; move_down # Move down when 2 ; move_left # Move left when 3 ; move_right # Move right when 4 ; move_up # Move up when 5 ; move_lower_left # Move lower left when 6 ; move_lower_right # Move lower right when 7 ; move_upper_left # Move upper left when 8 ; move_upper_right # Move upper right when 9 ; move_random # Move at random when 10 ; move_toward_player # Move toward player when 11 ; move_away_from_player # Move away from player when 12 ; move_forward # 1 step forward when 13 ; move_backward # 1 step backward when 14 ; jump(command.parameters[0], command.parameters[1]) # Jump end return if not @move_route.skippable and not moving? and not jumping? @move_route_index += 1 return end if command.code == 15 @wait_count = command.parameters[0] * 2 - 1 @move_route_index += 1 return end if command.code >= 16 and command.code <= 26 case command.code when 16 ; turn_down # Turn down when 17 ; turn_left # Turn left when 18 ; turn_right # Turn right when 19 ; turn_up # Turn up when 20 ; turn_right_90 # Turn 90° right when 21 ; turn_left_90 # Turn 90° left when 22 ; turn_180 # Turn 180° when 23 ; turn_right_or_left_90 # Turn 90° right or left when 24 ; turn_random # Turn at Random when 25 ; turn_toward_player # Turn toward player when 26 ; turn_away_from_player # Turn away from player end @move_route_index += 1 return end # If other command if command.code >= 27 # Branch by command code case command.code when 27 # Switch ON if self.self_switch?(command.parameters[0]) @self_switches[command.parameters[0]] = true else $game_switches[command.parameters[0]] = true end $game_map.need_refresh = true when 28 # Switch OFF if self.self_switch?(command.parameters[0]) @self_switches[command.parameters[0]] = false else $game_switches[command.parameters[0]] = false end $game_map.need_refresh = true when 29 # Change speed @move_speed = command.parameters[0] when 30 # Change freq @move_frequency = command.parameters[0] when 31 # Move animation ON @walk_anime = true when 32 # Move animation OFF @walk_anime = false when 33 # Stop animation ON @step_anime = true when 34 # Stop animation OFF @step_anime = false when 35 # Direction fix ON @direction_fix = true when 36 # Direction fix OFF @direction_fix = false when 37 # Through ON @through = true when 38 # Through OFF @through = false when 39 # Always on top ON @always_on_top = true when 40 # Always on top OFF @always_on_top = false when 41 # Change Graphic @tile_id = 0 @character_name = command.parameters[0] @character_hue = command.parameters[1] if @original_direction != command.parameters[2] @direction = command.parameters[2] @original_direction = @direction @prelock_direction = 0 end if @original_pattern != command.parameters[3] @pattern = command.parameters[3] @original_pattern = @pattern end when 42 # Change Opacity @opacity = command.parameters[0] when 43 # Change Blending @blend_type = command.parameters[0] when 44 # Play SE $game_system.se_play(command.parameters[0]) when 45 # Script result = eval(command.parameters[0]) end @move_route_index += 1 end end end #-------------------------------------------------------------------------- # * Refresh #-------------------------------------------------------------------------- def refresh new_page = nil unless @erased for page in @event.pages.reverse c = page.condition if c.switch1_valid if self.self_switch?(c.switch1_id) next if @self_switches[c.switch1_id] == false else next if $game_switches[c.switch1_id] == false end end if c.switch2_valid if self.self_switch?(c.switch2_id) next if @self_switches[c.switch2_id] == false else next if $game_switches[c.switch2_id] == false end end if c.variable_valid if self.self_variable?(c.variable_id) next if @self_variables[c.variable_id] < c.variable_value else next if $game_variables[c.variable_id] < c.variable_value end end if c.self_switch_valid key = [@map_id, @event.id, c.self_switch_ch] next if $game_self_switches[key] != true end new_page = page break end end return if new_page == @page @page = new_page clear_starting if @page == nil @tile_id = 0 @character_name = "" @character_hue = 0 @move_type = 0 @through = true @trigger = nil @list = nil @interpreter = nil return end @tile_id = @page.graphic.tile_id @character_name = @page.graphic.character_name @character_hue = @page.graphic.character_hue if @original_direction != @page.graphic.direction @direction = @page.graphic.direction @original_direction = @direction @prelock_direction = 0 end if @original_pattern != @page.graphic.pattern @pattern = @page.graphic.pattern @original_pattern = @pattern end @opacity = @page.graphic.opacity @blend_type = @page.graphic.blend_type @move_type = @page.move_type @move_speed = @page.move_speed @move_frequency = @page.move_frequency @move_route = @page.move_route @move_route_index = 0 @move_route_forcing = false @walk_anime = @page.walk_anime @step_anime = @page.step_anime @direction_fix = @page.direction_fix @through = @page.through @always_on_top = @page.always_on_top @trigger = @page.trigger @list = @page.list @interpreter = nil if @trigger == 4 @interpreter = Interpreter.new end check_event_trigger_auto endend#==============================================================================# * Interpreter#==============================================================================class Interpreter #-------------------------------------------------------------------------- # * Conditional Branch #-------------------------------------------------------------------------- def command_111 result = false case @parameters[0] when 0 # switch switch_id = @parameters[1] value = (@parameters[2] == 0) if self_switch?(switch_id) result = (self_switches[switch_id] == value) else result = ($game_switches[switch_id] == value) end when 1 # variable if self_variable?(@parameters[1]) value1 = self_variables[@parameters[1]] else value1 = $game_variables[@parameters[1]] end if @parameters[2] == 0 value2 = @parameters[3] else if self_variable?(@parameters[3]) value2 = self_variables[@parameters[3]] else value2 = $game_variables[@parameters[3]] end end result = case @parameters[4] when 0 ; (value1 == value2) when 1 ; (value1 >= value2) when 2 ; (value1 <= value2) when 3 ; (value1 > value2) when 4 ; (value1 < value2) when 5 ; (value1 != value2) end when 2 # self switch if @event_id > 0 key = [$game_map.map_id, @event_id, @parameters[1]] if @parameters[2] == 0 result = ($game_self_switches[key] == true) else result = ($game_self_switches[key] != true) end end when 3 # timer if $game_system.timer_working sec = $game_system.timer / Graphics.frame_rate if @parameters[2] == 0 result = (sec >= @parameters[1]) else result = (sec <= @parameters[1]) end end when 4 # actor actor = $game_actors[@parameters[1]] if actor != nil case @parameters[2] when 0 # in party result = ($game_party.actors.include?(actor)) when 1 # name result = (actor.name == @parameters[3]) when 2 # skill result = (actor.skill_learn?(@parameters[3])) when 3 # weapon result = (actor.weapon_id == @parameters[3]) when 4 # armor result = (actor.armor1_id == @parameters[3] or actor.armor2_id == @parameters[3] or actor.armor3_id == @parameters[3] or actor.armor4_id == @parameters[3]) when 5 # state result = (actor.state?(@parameters[3])) end end when 5 # enemy enemy = $game_troop.enemies[@parameters[1]] if enemy != nil case @parameters[2] when 0 # appear result = (enemy.exist?) when 1 # state result = (enemy.state?(@parameters[3])) end end when 6 # character character = get_character(@parameters[1]) if character != nil result = (character.direction == @parameters[2]) end when 7 # gold if @parameters[2] == 0 result = ($game_party.gold >= @parameters[1]) else result = ($game_party.gold <= @parameters[1]) end when 8 # item result = ($game_party.item_number(@parameters[1]) > 0) when 9 # weapon result = ($game_party.weapon_number(@parameters[1]) > 0) when 10 # armor result = ($game_party.armor_number(@parameters[1]) > 0) when 11 # button result = (Input.press?(@parameters[1])) when 12 # script result = eval(@parameters[1]) end @branch[@list[@index].indent] = result if @branch[@list[@index].indent] == true @branch.delete(@list[@index].indent) return true end return command_skip end #-------------------------------------------------------------------------- # * Control Switches #-------------------------------------------------------------------------- def command_121 value = (@parameters[2] == 0) for i in @parameters[0]..@parameters[1] if self_switch?(i) self_switches = value else $game_switches = value end end $game_map.need_refresh = true return true end #-------------------------------------------------------------------------- # * Control Variables #-------------------------------------------------------------------------- def command_122 value = 0 case @parameters[3] when 0 # invariable value = @parameters[4] when 1 # variable if self_variable?(@parameters[4]) value = self_variables[@parameters[4]] else value = $game_variables[@parameters[4]] end when 2 # random number value = @parameters[4] + rand(@parameters[5] - @parameters[4] + 1) when 3 # item value = $game_party.item_number(@parameters[4]) when 4 # actor actor = $game_actors[@parameters[4]] if actor != nil case @parameters[5] when 0 # level value = actor.level when 1 # EXP value = actor.exp when 2 # HP value = actor.hp when 3 # SP value = actor.sp when 4 # MaxHP value = actor.maxhp when 5 # MaxSP value = actor.maxsp when 6 # strength value = actor.str when 7 # dexterity value = actor.dex when 8 # agility value = actor.agi when 9 # intelligence value = actor.int when 10 # attack power value = actor.atk when 11 # physical defense value = actor.pdef when 12 # magic defense value = actor.mdef when 13 # evasion value = actor.eva end end when 5 # enemy enemy = $game_troop.enemies[@parameters[4]] if enemy != nil case @parameters[5] when 0 # HP value = enemy.hp when 1 # SP value = enemy.sp when 2 # MaxHP value = enemy.maxhp when 3 # MaxSP value = enemy.maxsp when 4 # strength value = enemy.str when 5 # dexterity value = enemy.dex when 6 # agility value = enemy.agi when 7 # intelligence value = enemy.int when 8 # attack power value = enemy.atk when 9 # physical defense value = enemy.pdef when 10 # magic defense value = enemy.mdef when 11 # evasion correction value = enemy.eva end end when 6 # character character = get_character(@parameters[4]) if character != nil case @parameters[5] when 0 # x-coordinate value = character.x when 1 # y-coordinate value = character.y when 2 # direction value = character.direction when 3 # screen x-coordinate value = character.screen_x when 4 # screen y-coordinate value = character.screen_y when 5 # terrain tag value = character.terrain_tag end end when 7 # other case @parameters[4] when 0 # map ID value = $game_map.map_id when 1 # number of party members value = $game_party.actors.size when 2 # gold value = $game_party.gold when 3 # steps value = $game_party.steps when 4 # play time value = Graphics.frame_count / Graphics.frame_rate when 5 # timer value = $game_system.timer / Graphics.frame_rate when 6 # save count value = $game_system.save_count end end for i in @parameters[0]..@parameters[1] iterator = case @parameters[2] when 0 then "=" # substitute when 1 then "+=" # add when 2 then "-=" # subtract when 3 then "*=" # multiply when 4 then "/=" # divide when 5 then "%=" # remainder end if self_variable?(i) eval("self_variables #{iterator} #{value}") rescue nil else eval("$game_variables #{iterator} #{value}") rescue nil end end $game_map.need_refresh = true return true end #-------------------------------------------------------------------------- # * Button Input #-------------------------------------------------------------------------- def input_button n = 0 keys = [] for k in Input.constants keys.push eval("Input::#{k}") end for i in keys next if !i.is_a?(Integer) if Input.trigger?(i) n = i end end if n > 0 if self_variable?(@button_input_variable_id) self_variables[@button_input_variable_id] = n else $game_variables[@button_input_variable_id] = n end $game_map.need_refresh = true @button_input_variable_id = 0 end end #-------------------------------------------------------------------------- # * Calculate Operated Value #-------------------------------------------------------------------------- def operate_value(operation, operand_type, operand) if operand_type == 0 value = operand else if self_variable?(operand) value = self_variables[operand] else value = $game_variables[operand] end end if operation == 1 value = -value end return value end #-------------------------------------------------------------------------- # * Transfer Player (Command) #-------------------------------------------------------------------------- def command_201 return true if $game_temp.in_battle if $game_temp.player_transferring or $game_temp.message_window_showing or $game_temp.transition_processing return false end if @parameters[0] == 0 new_map_id = @parameters[1] new_x = @parameters[2] new_y = @parameters[3] else if self_variable?(@parameters[1]) new_map_id = self_variables[@parameters[1]] else new_map_id = $game_variables[@parameters[1]] end if self_variable?(@parameters[2]) new_x = self_variables[@parameters[2]] else new_x = $game_variables[@parameters[2]] end if self_variable?(@parameters[3]) new_y = self_variables[@parameters[3]] else new_y = $game_variables[@parameters[3]] end end new_direction = @parameters[4] fade = (@parameters[5] == 0) transfer(new_map_id, new_x, new_y, new_direction, fade) @index += 1 return false end #-------------------------------------------------------------------------- # * Transfer Player (Method) #-------------------------------------------------------------------------- def transfer(map_id, x, y, direction=$game_player.direction, fade=false) $game_temp.player_transferring = true $game_temp.player_new_map_id = map_id $game_temp.player_new_x = x $game_temp.player_new_y = y $game_temp.player_new_direction = direction if fade == true Graphics.freeze $game_temp.transition_processing = true $game_temp.transition_name = "" end return true end #-------------------------------------------------------------------------- # * Set Event Location #-------------------------------------------------------------------------- def command_202 return true if $game_temp.in_battle character = get_character(@parameters[0]) return true if character == nil if @parameters[1] == 0 new_x = @parameters[2] new_y = @parameters[3] character.moveto(new_x, new_y) elsif @parameters[1] == 1 if self_variable?(@parameters[2]) new_x = self_variables[@parameters[2]] else new_x = $game_variables[@parameters[2]] end if self_variable?(@parameters[3]) new_y = self_variables[@parameters[3]] else new_y = $game_variables[@parameters[3]] end character.moveto(new_x, new_y) else old_x = character.x old_y = character.y character2 = get_character(@parameters[2]) if character2 != nil character.moveto(character2.x, character2.y) character2.moveto(old_x, old_y) end end case @parameters[4] when 2 ; character.turn_down # down when 4 ; character.turn_left # left when 6 ; character.turn_right # right when 8 ; character.turn_up # up end return true end #-------------------------------------------------------------------------- # * Show Picture #-------------------------------------------------------------------------- def command_231 number = @parameters[0] + ($game_temp.in_battle ? 50 : 0) name = @parameters[1] origin = @parameters[2] if @parameters[3] == 0 x = @parameters[4] y = @parameters[5] else if self_variable?(@parameters[4]) x = self_variables[@parameters[4]] else x = $game_variables[@parameters[4]] end if self_variable?(@parameters[5]) y = self_variables[@parameters[5]] else y = $game_variables[@parameters[5]] end end zoom_x = @parameters[6] zoom_y = @parameters[7] opacity = @parameters[8] blend_type = @parameters[9] $game_screen.pictures[number].show(name, origin, x, y, zoom_x, zoom_y, opacity, blend_type) return true end #-------------------------------------------------------------------------- # * Move Picture #-------------------------------------------------------------------------- def command_232 number = @parameters[0] + ($game_temp.in_battle ? 50 : 0) frames = @parameters[1] * 2 origin = @parameters[2] if @parameters[3] == 0 x = @parameters[4] y = @parameters[5] else if self_variable?(@parameters[4]) x = self_variables[@parameters[4]] else x = $game_variables[@parameters[4]] end if self_variable?(@parameters[5]) y = self_variables[@parameters[5]] else y = $game_variables[@parameters[5]] end end zoom_x = @parameters[6] zoom_y = @parameters[7] opacity = @parameters[8] blend_type = @parameters[9] $game_screen.pictures[number].move(frames, origin, x, y, zoom_x, zoom_y, opacity, blend_type) return true end #-------------------------------------------------------------------------- # * Shortcuts #-------------------------------------------------------------------------- def self_switches $game_map.events[@event_id].self_switches end def self_variables $game_map.events[@event_id].self_variables end def self_switch?(switch_id) return false if $game_map.events[@event_id].nil? return $game_temp.self_switch?(switch_id) end def self_variable?(variable_id) return false if $game_map.events[@event_id].nil? return $game_temp.self_variable?(variable_id) endend
Spoiler: ADDON - MESSAGE SUPPORT
With only the base script, you can't use local variables on messages using messages codes like \v[1]. This one below fixes that, but it changes the main message methods so sadly it's not compatible with custom message systems.
Ruby:
#==============================================================================# ** CSV for Events - Message Support (Add-On)#------------------------------------------------------------------------------# Put this script under Custom Self Switches & Self Variables v1.1#------------------------------------------------------------------------------# Autor: Wecoc (no credits required)#==============================================================================class Game_Temp attr_accessor :message_event_id #-------------------------------------------------------------------------- # * Initialize #-------------------------------------------------------------------------- alias message_event_ini initialize unless $@ def initialize message_event_ini @message_event_id = 0 endend#==============================================================================# * Window_Message#==============================================================================class Window_Message def refresh self.contents.clear self.contents.font.color = normal_color x = y = 0 @cursor_width = 0 if $game_temp.choice_start == 0 x = 8 end event_id = $game_temp.message_event_id event = $game_map.events[event_id] if $game_temp.message_text != nil text = $game_temp.message_text begin last_text = text.clone text.gsub!(/\\[Vv]\[([0-9]+)\]/) do if !event.nil? && $game_temp.self_variable?($1.to_i) event.self_variables[$1.to_i] else $game_variables[$1.to_i] end end end until text == last_text text.gsub!(/\\[Nn]\[([0-9]+)\]/) do $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : "" end text.gsub!(/\\\\/) { "\000" } text.gsub!(/\\[Cc]\[([0-9]+)\]/) { "\001[#{$1}]" } text.gsub!(/\\[Gg]/) { "\002" } while ((c = text.slice!(/./m)) != nil) if c == "\000" c = "\\" end if c == "\001" text.sub!(/\[([0-9]+)\]/, "") color = $1.to_i if color >= 0 and color <= 7 self.contents.font.color = text_color(color) end next end if c == "\002" if @gold_window == nil @gold_window = Window_Gold.new @gold_window.x = 560 - @gold_window.width if $game_temp.in_battle @gold_window.y = 192 else @gold_window.y = self.y >= 128 ? 32 : 384 end @gold_window.opacity = self.opacity @gold_window.back_opacity = self.back_opacity end next end if c == "\n" if y >= $game_temp.choice_start @cursor_width = [@cursor_width, x].max end y += 1 x = 0 x = 8 if y >= $game_temp.choice_start next end self.contents.draw_text(4 + x, 32 * y, 40, 32, c) x += self.contents.text_size(c).width end end if $game_temp.choice_max > 0 @item_max = $game_temp.choice_max self.active = true self.index = 0 end if $game_temp.num_input_variable_id > 0 digits_max = $game_temp.num_input_digits_max if !event.nil? && $game_temp.self_variable?($1.to_i) number = event.self_variables[$game_temp.num_input_variable_id] else number = $game_variables[$game_temp.num_input_variable_id] end @input_number_window = Window_InputNumber.new(digits_max) @input_number_window.number = number @input_number_window.x = self.x + 8 @input_number_window.y = self.y + $game_temp.num_input_start * 32 end endend#==============================================================================# * Interpreter#==============================================================================class Interpreter #-------------------------------------------------------------------------- # * Show Text #-------------------------------------------------------------------------- def command_101 return false if $game_temp.message_text != nil @message_waiting = true $game_temp.message_proc = Proc.new { @message_waiting = false } $game_temp.message_text = @list[@index].parameters[0] + "\n" $game_temp.message_event_id = @event_id line_count = 1 loop do if @list[@index+1].code == 401 $game_temp.message_text += @list[@index+1].parameters[0] + "\n" line_count += 1 else if @list[@index+1].code == 102 if @list[@index+1].parameters[0].size <= 4 - line_count @index += 1 $game_temp.choice_start = line_count setup_choices(@list[@index].parameters) end elsif @list[@index+1].code == 103 if line_count < 4 @index += 1 $game_temp.num_input_start = line_count $game_temp.num_input_variable_id = @list[@index].parameters[0] $game_temp.num_input_digits_max = @list[@index].parameters[1] end end return true end @index += 1 end end #-------------------------------------------------------------------------- # * Show Choices #-------------------------------------------------------------------------- def command_102 return false if $game_temp.message_text != nil @message_waiting = true $game_temp.message_proc = Proc.new { @message_waiting = false } $game_temp.message_text = "" $game_temp.message_event_id = @event_id $game_temp.choice_start = 0 setup_choices(@parameters) return true end #-------------------------------------------------------------------------- # * Input Number #-------------------------------------------------------------------------- def command_103 return false if $game_temp.message_text != nil @message_waiting = true $game_temp.message_proc = Proc.new { @message_waiting = false } $game_temp.message_text = "" $game_temp.message_event_id = @event_id $game_temp.num_input_start = 0 $game_temp.num_input_variable_id = @parameters[0] $game_temp.num_input_digits_max = @parameters[1] return true endend