Custom Self Switches & Self Variables
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
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
$game_temp.self_switch?(SWITCH ID)
$game_temp.self_variable?(VARIABLE ID)
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?("") @switches.push(i) end end @variables = [] variables = data.variables for i in 1...variables.size if variables.upcase.include?("") @variables.push(i) end endend#--------------------------------------------------------------------------# * 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_switchesattr_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, command.parameters) # 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 * 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) @self_switches] = true else $game_switches] = true end $game_map.need_refresh = true when 28# Switch OFF if self.self_switch?(command.parameters) @self_switches] = false else $game_switches] = false end $game_map.need_refresh = true when 29# Change speed @move_speed = command.parameters when 30# Change freq @move_frequency = command.parameters 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 @character_hue = command.parameters if @original_direction != command.parameters @direction = command.parameters @original_direction = @direction @prelock_direction = 0 end if @original_pattern != command.parameters @pattern = command.parameters @original_pattern = @pattern end when 42# Change Opacity @opacity = command.parameters when 43# Change Blending @blend_type = command.parameters when 44# Play SE $game_system.se_play(command.parameters) when 45# Script result = eval(command.parameters) end @move_route_index += 1 end endend#--------------------------------------------------------------------------# * 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 == false else next if $game_switches == false end end if c.switch2_valid if self.self_switch?(c.switch2_id) next if @self_switches == false else next if $game_switches == false end end if c.variable_valid if self.self_variable?(c.variable_id) next if @self_variables < c.variable_value else next if $game_variables < c.variable_value end end if c.self_switch_valid key = [@map_id, @event.id, c.self_switch_ch] next if $game_self_switches != 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_autoendend#==============================================================================# * Interpreter#==============================================================================class Interpreter#--------------------------------------------------------------------------# * Conditional Branch#--------------------------------------------------------------------------def command_111 result = false case @parameters when 0# switch switch_id = @parameters value = (@parameters == 0) if self_switch?(switch_id) result = (self_switches == value) else result = ($game_switches == value) end when 1# variable if self_variable?(@parameters) value1 = self_variables[@parameters] else value1 = $game_variables[@parameters] end if @parameters == 0 value2 = @parameters else if self_variable?(@parameters) value2 = self_variables[@parameters] else value2 = $game_variables[@parameters] end end result = case @parameters 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] if @parameters == 0 result = ($game_self_switches == true) else result = ($game_self_switches != true) end end when 3# timer if $game_system.timer_working sec = $game_system.timer / Graphics.frame_rate if @parameters == 0 result = (sec >= @parameters) else result = (sec <= @parameters) end end when 4# actor actor = $game_actors[@parameters] if actor != nil case @parameters when 0# in party result = ($game_party.actors.include?(actor)) when 1# name result = (actor.name == @parameters) when 2# skill result = (actor.skill_learn?(@parameters)) when 3# weapon result = (actor.weapon_id == @parameters) when 4# armor result = (actor.armor1_id == @parameters or actor.armor2_id == @parameters or actor.armor3_id == @parameters or actor.armor4_id == @parameters) when 5# state result = (actor.state?(@parameters)) end end when 5# enemy enemy = $game_troop.enemies[@parameters] if enemy != nil case @parameters when 0# appear result = (enemy.exist?) when 1# state result = (enemy.state?(@parameters)) end end when 6# character character = get_character(@parameters) if character != nil result = (character.direction == @parameters) end when 7# gold if @parameters == 0 result = ($game_party.gold >= @parameters) else result = ($game_party.gold <= @parameters) end when 8# item result = ($game_party.item_number(@parameters) > 0) when 9# weapon result = ($game_party.weapon_number(@parameters) > 0) when 10# armor result = ($game_party.armor_number(@parameters) > 0) when 11# button result = (Input.press?(@parameters)) when 12# script result = eval(@parameters) end @branch[@list[@index].indent] = result if @branch[@list[@index].indent] == true @branch.delete(@list[@index].indent) return true end return command_skipend#--------------------------------------------------------------------------# * Control Switches#--------------------------------------------------------------------------def command_121 value = (@parameters == 0) for i in @parameters..@parameters if self_switch?(i) self_switches = value else $game_switches = value end end $game_map.need_refresh = true return trueend#--------------------------------------------------------------------------# * Control Variables#--------------------------------------------------------------------------def command_122 value = 0 case @parameters when 0# invariable value = @parameters when 1# variable if self_variable?(@parameters) value = self_variables[@parameters] else value = $game_variables[@parameters] end when 2# random number value = @parameters + rand(@parameters - @parameters + 1) when 3# item value = $game_party.item_number(@parameters) when 4# actor actor = $game_actors[@parameters] if actor != nil case @parameters 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] if enemy != nil case @parameters 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) if character != nil case @parameters 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 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..@parameters iterator = case @parameters 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 trueend#--------------------------------------------------------------------------# * 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 endend#--------------------------------------------------------------------------# * Calculate Operated Value#--------------------------------------------------------------------------def operate_value(operation, operand_type, operand) if operand_type == 0 value = operand else if self_variable?(operand) value = self_variables else value = $game_variables end end if operation == 1 value = -value end return valueend#--------------------------------------------------------------------------# * 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 new_map_id = @parameters new_x = @parameters new_y = @parameters else if self_variable?(@parameters) new_map_id = self_variables[@parameters] else new_map_id = $game_variables[@parameters] end if self_variable?(@parameters) new_x = self_variables[@parameters] else new_x = $game_variables[@parameters] end if self_variable?(@parameters) new_y = self_variables[@parameters] else new_y = $game_variables[@parameters] end end new_direction = @parameters fade = (@parameters == 0) transfer(new_map_id, new_x, new_y, new_direction, fade) @index += 1 return falseend#--------------------------------------------------------------------------# * 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 trueend#--------------------------------------------------------------------------# * Set Event Location#--------------------------------------------------------------------------def command_202 return true if $game_temp.in_battle character = get_character(@parameters) return true if character == nil if @parameters == 0 new_x = @parameters new_y = @parameters character.moveto(new_x, new_y) elsif @parameters == 1 if self_variable?(@parameters) new_x = self_variables[@parameters] else new_x = $game_variables[@parameters] end if self_variable?(@parameters) new_y = self_variables[@parameters] else new_y = $game_variables[@parameters] end character.moveto(new_x, new_y) else old_x = character.x old_y = character.y character2 = get_character(@parameters) if character2 != nil character.moveto(character2.x, character2.y) character2.moveto(old_x, old_y) end end case @parameters 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 trueend#--------------------------------------------------------------------------# * Show Picture#--------------------------------------------------------------------------def command_231 number = @parameters + ($game_temp.in_battle ? 50 : 0) name = @parameters origin = @parameters if @parameters == 0 x = @parameters y = @parameters else if self_variable?(@parameters) x = self_variables[@parameters] else x = $game_variables[@parameters] end if self_variable?(@parameters) y = self_variables[@parameters] else y = $game_variables[@parameters] end end zoom_x = @parameters zoom_y = @parameters opacity = @parameters blend_type = @parameters $game_screen.pictures.show(name, origin, x, y, zoom_x, zoom_y, opacity, blend_type) return trueend#--------------------------------------------------------------------------# * Move Picture#--------------------------------------------------------------------------def command_232 number = @parameters + ($game_temp.in_battle ? 50 : 0) frames = @parameters * 2 origin = @parameters if @parameters == 0 x = @parameters y = @parameters else if self_variable?(@parameters) x = self_variables[@parameters] else x = $game_variables[@parameters] end if self_variable?(@parameters) y = self_variables[@parameters] else y = $game_variables[@parameters] end end zoom_x = @parameters zoom_y = @parameters opacity = @parameters blend_type = @parameters $game_screen.pictures.move(frames, origin, x, y, zoom_x, zoom_y, opacity, blend_type) return trueend#--------------------------------------------------------------------------# * Shortcuts#--------------------------------------------------------------------------def self_switches $game_map.events[@event_id].self_switchesenddef self_variables $game_map.events[@event_id].self_variablesenddef self_switch?(switch_id) return false if $game_map.events[@event_id].nil? return $game_temp.self_switch?(switch_id)enddef self_variable?(variable_id) return false if $game_map.events[@event_id].nil? return $game_temp.self_variable?(variable_id)endend
Spoiler: ADDON - MESSAGE SUPPORTWith only the base script, you can't use local variables on messages using messages codes like \v. 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_Tempattr_accessor :message_event_id#--------------------------------------------------------------------------# * Initialize#--------------------------------------------------------------------------alias message_event_ini initialize unless $@def initialize message_event_ini @message_event_id = 0endend#==============================================================================# * Window_Message#==============================================================================class Window_Messagedef 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 if $game_temp.message_text != nil text = $game_temp.message_text begin last_text = text.clone text.gsub!(/\\\[(+)\]/) 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!(/\\\[(+)\]/) do $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : "" end text.gsub!(/\\\\/) { "\000" } text.gsub!(/\\\[(+)\]/) { "\001[#{$1}]" } text.gsub!(/\\/) { "\002" } while ((c = text.slice!(/./m)) != nil) if c == "\000" c = "\\" end if c == "\001" text.sub!(/\[(+)\]/, "") 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 endendend#==============================================================================# * 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 + "\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 + "\n" line_count += 1 else if @list[@index+1].code == 102 if @list[@index+1].parameters.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 $game_temp.num_input_digits_max = @list[@index].parameters end end return true end @index += 1 endend#--------------------------------------------------------------------------# * 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 trueend#--------------------------------------------------------------------------# * 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 $game_temp.num_input_digits_max = @parameters return trueendend
本贴来自国际rpgmaker官方论坛作者:Wecoc处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/custom-self-switches-self-variables.138450/
页:
[1]