[Unsupported] PK8's Self Variables VX
#1
PK8's Self Variables VX
Version: 1

Introduction
This script is pretty similar to that of the built in feature: Self Switches. For those not familiar with Self Switches, Self Switches pretty much pertain to a specific event. Example: Treasure Chests.

Self Variables allows users to set certain variables pertaining to a specific event. Examples include: An event's feelings towards you.

Features
  • Set self variables of an event via call script.
  • Get self variables of an event via call script.

Screenshots
How can I screenshot this?

Demo
No demo.

Script
Code:
=begin
?????????????????????????????????????????????????????????????????????????????????
? PK8's Self Variables                                                         ?
? Version 1.0                                                                  ?
? by PK8                                                                       ?
? 9/16/09                                                                      ?
? http://rmvxp.com                                                             ?
?????????????????????????????????????????????????????????????????????????????????
? ? Table of Contents                                                          ?
? ?? Author's Notes                - Line 18?21                                ?
? ?? Introduction & Description    - Line 23?29                                ?
? ?? Features                      - Line 31?33                                ?
? ?? How to Use                    - Line 35?59                                ?
? ?? This aliases the following... - Line 61?64                                ?
? ?? Thanks                        - Line 66?71                                ?
? ?? Changelog                     - Line 73,74                                ?
????????????????????????????????????????????????????????????????????????????????
? ? Author's Notes                                                             ?
? Lowell/Adalwulf mentioned creating a "Personal Variables" system which filled?
? my head in with ideas about variables and switches. One of these ideas being ?
? self variables.                                                              ?
?????????????????????????????????????????????????????????????????????????????????
? ? Introduction & Description                                                 ?
? This script is pretty similar to that of the built in feature: Self Switches.?
? For those not familiar with Self Switches, Self Switches pretty much pertain ?
? to a specific event. Example: Treasure Chests.                               ?
?                                                                              ?
? Self Variables allows users to set certain variables pertaining to a specific?
? event. Examples include: An event's feelings towards you.                    ?
?????????????????????????????????????????????????????????????????????????????????
? ? Features                                                                   ?
? ? Set self variables of an event via call script. (How to use is below)      ?
? ? Get self variables of an event via call script. (How to use is below)      ?
????????????????????????????????????????????????????????????????????????????????
? ? How to Use                                                                 ?
?                                                                              ?
? ? Setting up a self variable:                                                ?
?   To set a self variable for an event, you'll need to call this script on a  ?
?   map event:                                                                 ?
?     self_variable(id, value, oper)                                           ?
?       id:     Self Variable Identification. Example: 'feeling'               ?
?       value:  Give it a value.                                               ?
?       oper:   0: Set, 1: Add, 2: Sub, 3: Mul, 4: Div, 5: Mod                 ?
?                                                                              ?
? ? Getting a self variable:                                                   ?
?   To get a self variable for an event, you can call this script.             ?
?     self_variable(id)                                                        ?
?       id:     Self Variable Identification.                                  ?
?                                                                              ?
? ? Using self variables in evented if conditions.                             ?
?   You're probably wondering how, right? Alright. To do this, go to the       ?
?   conditional branch event command, click on the fourth tab, select Script   ?
?   and type either of these in the input form:                                ?
?     self_variable(id) == value     <- Equal to.                              ?
?     self_variable(id) >= value     <- Greater than or Equal to.              ?
?     self_variable(id) <= value     <- Less than or Equal to.                 ?
?     self_variable(id) > value      <- Greater than.                          ?
?     self_variable(id) < value      <- Less than.                             ?
?     self_variable(id) != value     <- Not Equal to.                          ?
????????????????????????????????????????????????????????????????????????????????
? ? This aliases the following...                                              ?
? command_new_game           - Scene_Title                                     ?
? write_save_data            - Scene_Save < Scene_File                         ?
? read_save_data             - Scene_Load < Scene_File                         ?
????????????????????????????????????????????????????????????????????????????????
? ? Thanks                                                                     ?
? Lowell: He mentioned creating a personal variables system for his project    ?
?         (I think), which made me catch "the scripting bug".                  ?
? Decibel: Wanted some way to get self variables into messages but I couldn't  ?
?          do it. Someone, help!                                               ?
? Kain Nobel: He helped me with making this an RMXP script.                    ?
????????????????????????????????????????????????????????????????????????????????
? ? Changelog                                                                  ?
? Version 1.0 - 9/16/09: Initial Release                                       ?
?????????????????????????????????????????????????????????????????????????????????
=end

#==============================================================================
# ** Game_SelfVariables
#------------------------------------------------------------------------------
#  This class handles self variables. It's a wrapper for the built-in class
# "Hash." The instance of this class is referenced by $game_self_variables.
#==============================================================================

class Game_SelfVariables
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    @data = {}
  end
  #--------------------------------------------------------------------------
  # * Get Self Switch
  #     key : key
  #--------------------------------------------------------------------------
  def [](key)
    if @data[key] == nil
      return 0
    else
      return @data[key]
    end
  end
  #--------------------------------------------------------------------------
  # * Set Self Switch
  #     key   : key
  #     value : key's value
  #--------------------------------------------------------------------------
  def []=(key, value)
    @data[key] = value
  end
end

#==============================================================================
# ** Interpreter
#------------------------------------------------------------------------------
#  This interpreter runs event commands. This class is used within the
#  Game_System class and the Game_Event class.
#==============================================================================

class Interpreter
  def self_variable(id, value = nil, oper = nil)
    if @event_id > 0
      key = [@map_id, @event_id, id]
      if value != nil
        case oper
        when nil, 0, 'equal', 'set', '='                     # Setting
          $game_self_variables[key] = value
        when 1, 'add', '+'                                   # Adding
          $game_self_variables[key] += value
        when 2, 'sub', 'subtract', '-'                       # Subtracting
          $game_self_variables[key] -= value
        when 3, 'mul', 'multiply', 'x', '*'                  # Multiplying
          $game_self_variables[key] *= value
        when 4, 'div', 'divide', '/'                         # Dividing
          $game_self_variables[key] /= value if value != 0
        when 5, 'mod', 'modular', '%'                        # Modulating
          $game_self_variables[key] %= value if value != 0
        else
          return $game_self_variables[key]
        end
      end
      if value != nil
        $game_map.need_refresh = true
        return true
      end
    end
  end
end

#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs the title screen processing.
#==============================================================================

class Scene_Title
  alias pk8_self_variables_command_new_game :command_new_game
  #--------------------------------------------------------------------------
  # * Create Game Objects
  #--------------------------------------------------------------------------
  def command_new_game
    pk8_self_variables_command_new_game
    $game_self_variables = Game_SelfVariables.new
  end
end

#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
#  This class performs save screen processing.
#==============================================================================

class Scene_Save < Scene_File
  alias pk8_self_variables_write_save_data  :write_save_data
  #--------------------------------------------------------------------------
  # * Write Save Data
  #     file : write file object (opened)
  #--------------------------------------------------------------------------
  def write_save_data(file)
    pk8_self_variables_write_save_data(file)
    Marshal.dump($game_self_variables, file)
  end
end

#==============================================================================
# ** Scene_Load
#------------------------------------------------------------------------------
#  This class performs load screen processing.
#==============================================================================

class Scene_Load < Scene_File
  alias pk8_self_variables_read_save_data   :read_save_data
  #--------------------------------------------------------------------------
  # * Read Save Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    pk8_self_variables_read_save_data(file)
    $game_self_variables = Marshal.load(file)
  end
end

Instructions
How to use is in the instructions.

FAQ
No questions asked so far...

Compatibility
It aliases the following methods from the following classes:
  • create_game_objects - Scene_Title < Scene_Base
  • write_save_data - Scene_File < Scene_Base
  • read_save_data - Scene_File < Scene_Base

Thanks
Lowell: He mentioned creating a personal variables system for his project (I think), which made me catch "the scripting bug".
Decibel: Wanted some way to get self variables into messages but I couldn't do it. Someone, help!

Author's Notes
Lowell/Adalwulf mentioned creating a "Personal Variables" system which filled my head in with ideas about variables and switches. One of these ideas being self variables.

Terms and Conditions
Possibly an RMVXP exclusive.
Reply }
#2
Awesome job, very clean coding and looks easy as hell to use! I'm definately gonna add your XP version to my lib because I could get alot of good use out of this too!

One thing I'm gonna pick on though is it appears that the system is only used to handle Numeric data, while $game_variables can handle other data types like Strings and Arrays and the such (even though they weren't designed to either anyway). Still, I believe some of the math methods should still work on them (+ and * at least, never tried using other math operators directly on String/Array/etc classes)

And also, self switches require map_id, event_id, key (ie, $game_self_switches[[1, 5, "A"]]). I would make the arguments interchangable, where if the 1st and 2nd argument are Integers it'll look up Map ID and Event ID, otherwise it would just look up Event ID. Alternatively, you could just use Key ("A") and not specify Map/Event ID and it would do it for whatever event is running atm.

self_variables(1, 5, "A") # Map ID, Event ID, Var Key (for any event on any map)
self_variables(5, "A") # Event ID, Var Key (used for the current map)
self_variables("A") # Var Key (used directly from event call script)
[Image: Button-BOTB.png]
[Image: Save-Point.gif][Image: Button-You-Tube2.png][Image: Button-Sound-Cloud2.png][Image: Button-Audio-Mack2.png]
[Image: LS-Banner.gif]
NEW ALBUM OUT NOW!

Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Group Variables v1.1 PK8 0 166 11-12-2009, 06:30 AM
Last Post: PK8
   Common Event Self Variables VX PK8 0 924 09-17-2009, 04:55 AM
Last Post: PK8
   Actor & Party's Self Variables VX PK8 0 697 09-17-2009, 01:43 AM
Last Post: PK8
   [Unsupported] Actor & Party's Self Switches VX PK8 0 5,117 09-16-2009, 10:57 PM
Last Post: PK8
   [Unsupported] Game Strings (Streamline dialogue!) PK8 3 9,398 09-15-2009, 11:28 PM
Last Post: Wyatt
   [Unsupported] Title Skip 2 VX PK8 0 4,909 12-07-2008, 12:36 PM
Last Post: PK8



Users browsing this thread: 2 Guest(s)