[VX] Self-Variables

Started by DrakoShade, January 12, 2010, 09:12:57 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


#==============================================================================
# ** Game_Interpreter
#==============================================================================

class Game_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
#==============================================================================

class Game_Event

  #--------------------------------------------------------------------------
  # * Determine if Event Page Conditions are Met
  #--------------------------------------------------------------------------
  def conditions_met?(page)
    c = page.condition
    if c.switch1_valid      # Swtich 1
      return false if $game_switches[c.switch1_id] == false
    end
    if c.switch2_valid      # Swtich 2
      return false if $game_switches[c.switch2_id] == false
    end
    if c.variable_valid     # Variable
      #----------------------------------------------------------------------
      # 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]
        return false if $game_self_variables[key] < c.variable_value
      else
        return false if $game_variables[c.variable_id] < c.variable_value
      end
      #----------------------------------------------------------------------
      # Changes end here.
      #----------------------------------------------------------------------
    end
    if c.self_switch_valid  # Self switch
      key = [@map_id, @event.id, c.self_switch_ch]
      return false if $game_self_switches[key] != true
    end
    if c.item_valid         # Item
      item = $data_items[c.item_id]
      return false if $game_party.item_number(item) == 0
    end
    if c.actor_valid        # Actor
      actor = $game_actors[c.actor_id]
      return false unless $game_party.members.include?(actor)
    end
    return true   # Conditions met
  end
end


#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
# Appropriate aliasing made here to create the $game_self_variables hash.
#==============================================================================
class Scene_Title
  alias ds_self_variables_create_game_objects create_game_objects
  def create_game_objects
    $game_self_variables = Game_SelfVariables.new
    ds_self_variables_create_game_objects
  end
end

#==============================================================================
# ** Scene_File
#------------------------------------------------------------------------------
# Self variables added to save_data and load_data.
#==============================================================================
class Scene_File
  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

  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 in the Materials section.

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

Incompatible with anything else that alters the conditions_met? method of Game_Event.


Credits and Thanks


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



Author's Notes

Have fun.

Holyrapid

So, did it on both scriptable RM's, eh? Good job mate. I may wink this to a friend of mine who´s working on a RMVX game.

Neuntausend

o/

the script looks exactly like what I would need, but how do I actually use the self-variable as a page condition. it  says it's optional - well, i want to use that option, but how do i pull it off?

thanks in advance for any smart answer.

\o