[XP] Self-Variables

Started by DrakoShade, January 12, 2010, 09:09:23 am

Previous topic - Next topic

DrakoShade

Self-Variables
Authors: DrakoShade
Version: 1.0
Type: Interpreter Expansion and Event Control Modification
Key Term: Game Utility



Introduction

This script adds Self-Variables (like Self-Switches, but more than two options) to your events.  Treat them like what they are--  a hybrid between Global Variables and Self-Switches.  Each event will only want to look at and modify its own, so you can make a single event that uses @self_variable[1], copy-and-paste it 50 times on 10 different maps, and each one of them will be looking at its very own @self_variable[1], for whatever internal conditional branches or page conditions you told the original to use.

It's easy to use from within events, requiring a script call no more difficult than accessing a local variable in a script: @self_variables[X].  Even from an event that doesn't own the variable, it's possible with $game_self_variables[[map_id, event_id, variable_id]].


Features


  • Self-Variables for every event in the game.
  • Can use a Self-Variable in place of a Global Variable as a page condition (optional.)
  • Events access their own self-variables with very simple script commands.
  • An event's self-variables can be accessed from outside without much difficulty.



Screenshots

It's very difficult to screenshot a back-end improvement.


Demo

No demo.  The script should speak for itself if you have need of it.


Script

Spoiler: ShowHide

#==============================================================================
# ** 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



Instructions

Paste above Main.

Calling an event's self-variables from outside said event requires a call to $game_self_variables[[map_id, event_id, variable_id]].  From within the event (in a Call Script box), it only needs a simple @self_variables[variable_id].


Compatibility

Untested with any version of SDK.
Incompatible with anything else that alters the refresh method of Game_Event.


Credits and Thanks


  • Me
  • Myself
  • I
  • Vodka.  Specifically, Stoli.



Author's Notes

Have fun.

Aqua

Ooh... nice :o
I've been wondering about this for a bit.

You should add credits within the script though...
Wouldn't want your credits to go lost D:

Holyrapid

This can be useful. DonĀ“t need to use like a thousand varibles just because events need different variables. Nice work :)

cdaz

This is look awesome but I little confuse how actually use it.
I mean how to Control this Self-Variable and how to check it value.

I want to use it to make some simple HP system.
by use Self Variable start with 0 and increase every time player push action button on it.
When Self-Variable = 4 then erase event.

If there are some demo I think may be easier to understand how it work.

I made this event

Quote@Script @self_variables[1]=0
@Condition Brunch : Script @self_variables[1]=0
@Text: Hello
@Brunch End


But noting Happened.

ForeverZer0

The condition should use 2 equal signs.
@self_variables[1]==0
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.