#==============================================================================
# ** Game_SelfVariables
#------------------------------------------------------------------------------
# This class handles self variables. It's a wrapper for the built-in class
# "Hash." Refer to "$game_self_variables" for the instance of this class.
#==============================================================================
class Game_SelfVariables
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
@data = {}
end
#--------------------------------------------------------------------------
# * Get Self Variable
# key : key
#--------------------------------------------------------------------------
def [](key)
return @data[key] == nil ? 0 : @data[key]
end
#--------------------------------------------------------------------------
# * Set Self Variable
# key : key
# value : the variable's value
#--------------------------------------------------------------------------
def []=(key, value)
@data[key] = [[value, -99999999].max, 99999999].min
end
end
#==============================================================================
# ** Self Variable Interpreter
#------------------------------------------------------------------------------
# This class handles the translation of $game_self_variables into an array
# so that any individual event can treat its own self_variables as such. It's
# pretends to be an Array, so far as the interaction requires.
#==============================================================================
class SelfVarInterpreter
attr_accessor :key
#---------------------------------------------------------------------------
# * Object Initialization
# key = [$game_map.map_id, @event_id]
#---------------------------------------------------------------------------
def initialize
@key = [0, 0]
end
#---------------------------------------------------------------------------
# * Get Self Variable
# variable_id : variable's ID
#---------------------------------------------------------------------------
def [](variable_id)
key = [@key[0], @key[1], variable_id]
return $game_self_variables[key]
end
#---------------------------------------------------------------------------
# * Set Self Variable
# variable_id : variable's ID
# value : the variable's value
#---------------------------------------------------------------------------
def []=(variable_id, value)
key = [@key[0], @key[1], variable_id]
$game_self_variables[key] = value
$game_map.need_refresh = true
end
end
#==============================================================================
# ** Interpreter
#==============================================================================
class Interpreter
#---------------------------------------------------------------------------
# * Initialize
#---------------------------------------------------------------------------
alias ds_self_variables_initialize initialize
def initialize(depth = 0, main = false)
@self_variables = SelfVarInterpreter.new
ds_self_variables_initialize(depth, main)
end
#---------------------------------------------------------------------------
# * Setup
#---------------------------------------------------------------------------
alias ds_self_variables_setup setup
def setup(list, event_id)
ds_self_variables_setup(list, event_id)
@self_variables.key = [@map_id, @event_id]
end
end
#==============================================================================
# ** Game_Event
#------------------------------------------------------------------------------
# Change the Refresh method to seek out comments containing "use self_variable"
# and, if it finds one, use the self_variables instead of game_variables to
# determine whether a page is active.
#==============================================================================
class Game_Event
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
# Initialize local variable: new_page
new_page = nil
# If not temporarily erased
unless @erased
# 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 $game_switches[c.switch1_id] == false
next
end
end
# Switch 2 condition confirmation
if c.switch2_valid
if $game_switches[c.switch2_id] == false
next
end
end
# Variable condition confirmation
if c.variable_valid
#--------------------------------------------------------------------
# Changes begin here.
#--------------------------------------------------------------------
for i in 0...page.list.size
if page.list[i].code == 108
if page.list[i].parameters[0].include?("use self variable")
use_self_variable = true
end
end
end
if use_self_variable == true
key = [@map_id, @id, c.variable_id]
if $game_self_variables[key] < c.variable_value
next
end
else
if $game_variables[c.variable_id] < c.variable_value
next
end
end
#--------------------------------------------------------------------
# Changes end here.
#--------------------------------------------------------------------
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
end
# If event page is the same as last time
if new_page == @page
# End method
return
end
# Set @page as current event page
@page = new_page
# Clear starting flag
clear_starting
# If no page fulfills conditions
if @page == nil
# Set each instance variable
@tile_id = 0
@character_name = ""
@character_hue = 0
@move_type = 0
@through = true
@trigger = nil
@list = nil
@interpreter = nil
# End method
return
end
# Set each instance variable
@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 is [parallel process]
if @trigger == 4
# Create parallel process interpreter
@interpreter = Interpreter.new
end
# Auto event start determinant
check_event_trigger_auto
end
end
#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
# Aliases are made to properly initialize self variables.
#==============================================================================
class Scene_Title
alias ds_self_variables_command_new_game command_new_game
def command_new_game
$game_self_variables = Game_SelfVariables.new
ds_self_variables_command_new_game
end
alias ds_self_variables_battle_test battle_test
def battle_test
$game_self_variables = Game_SelfVariables.new
ds_self_variables_battle_test
end
end
#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
# Adding $game_self_variables.
#==============================================================================
class Scene_Save
alias ds_self_variables_write_save_data write_save_data
def write_save_data(file)
ds_self_variables_write_save_data(file)
Marshal.dump($game_self_variables, file)
end
end
#==============================================================================
# ** Scene_Load
#------------------------------------------------------------------------------
# Adding $game_self_variables.
#==============================================================================
class Scene_Load
alias ds_self_variables_read_save_data read_save_data
def read_save_data(file)
ds_self_variables_read_save_data(file)
$game_self_variables = Marshal.load(file)
end
end
출처 : http://forum.chaos-project.com/index.php?action=search2
어떤 분께서 만드신 셀프 변수라는 기능을 가진 스크립트를 이용하려고 하고 있습니다
그래서 이벤트 스크립트에서
$game_self_variables[[map_id, event_id, variable_id]]
또는 @self_variables[variable_id] 를 입력하면
자꾸 NoMethodError [] for nil:nilclass 오류가 나네요...
아무리 찾아봐도 원인이 뭔지 모르겠습니다;;
스크립트 상으로 봤을 때는 아무런 이상이 없는데요..
혹시 저의 사용방법이 잘못된건지...
답변부탁드립니다.
필요하신 정보가 있다면 답변해드리겠습니다.