Help - Search - Members - Calendar
Full Version: Memorize Event Positions
RPG RPG Revolution Forums > Scripting > Script Tutorials > RGSS2
BigEd781
Memorize Event Positions

By Biged781


Intro
For those who do not like that the positions of their events reset when the player exits and enters a map, this script will automatically memorize their positions for you. Just drop it into the materials section and your events will always appear where they left off. You can enable and disable this behavior by turning a switch ON or OFF.

Bug Fixes / Enhancements
-> Added a way to disable the effects of this script for certain events.
-> Fixed a bug that would have occurred with encrypted game files.
-> Ended reliance on the file system, greatly reduces chances of bugs (thanks wora wink.gif)

Configuration / Use
Just plop it into the "Materials" sections, should be no problems. To turn the script on and off, find this part of the script:

CODE
#-----------------------------------------------------------------------
  # * Set this to the switch number that you will
  #   use to turn this script ON or OFF.
  #   ---------  
  #   Set to switch id "1" by default.
  #
  MOVE_EVENTS_SWITCH_ID = 1


You can modify this constant,

CODE
MOVE_EVENTS_SWITCH_ID


to equal the id of the switch that you would like to use. If this switch is ON, the events on each map will memorize their positions at all times. If you turn this switch OFF, they will return to their default positions. Note that only the maps that are loaded after the switch is turned ON or OFF will be affected, i.e., the current map positions will be saved, but the events will not be moved.

--------------------------------
To disable the effects of this script at the event level, add a comment to the event with only the text "no mem", without the quotes.




Script
CODE
#-----------------------------------------------------------------------
#                       Memorize Event Positions
#                               BigEd781
#-----------------------------------------------------------------------
class Game_System
  
  alias :eds_old_pre_mem_intialize :initialize
  def initialize
    eds_old_pre_mem_intialize
    @event_pos_data = {}    
  end
  
  def save_event_position_data(map_id, data)
    @event_pos_data[map_id] = data
  end
  
  def get_event_position_data(map_id)
    return @event_pos_data[map_id].nil? ? [] : @event_pos_data[map_id]
  end
  
end

class Game_Event < Game_Character

  def comment?(comment)
    unless @list.nil?
      @list.each { |line|
        next if line.code != 108                              
        return true if line.parameters[0].upcase == comment.upcase }      
    end    
    return false
  end  
  
end

class Game_Map
  
  alias :eds_old_pre_mem_setup :setup
  def setup(map_id)    
    save_positions if $game_switches[1]
    eds_old_pre_mem_setup(map_id)    
    restore_positions  if $game_switches[1]
  end
  
  def save_positions
    return if @events.nil? || @events == { }        
    data = []
    @events.values.each { |event| data += [[ event.id, event.x, event.y, event.direction ]] }      
    $game_system.save_event_position_data(@map_id, data)    
  end
  
  def restore_positions    
    return if @events.nil? || @events == { }        
    $game_system.get_event_position_data(@map_id).each { |data|  
    event = @events[data[0]]  
      unless event.nil? || event.comment?('no mem')
        x, y, direction = data[1], data[2], data[3]
        event.moveto(x, y) unless event.pos?(x, y)
        event.set_direction(direction)
      end
    }    
  end
  
end


Compatibilty / Issues
I don't not know of any issues currently, but this system writes out the event's locations to a file when maps are left, and reads that file back in to position the events when a map is loaded. Also, there are many edge conditions to yet be found so there will probably be some bugs. I will fix them as soon as they are posted. Compatibility with other scripts should be 100%.
Warder
Hey, this is a pretty cool script. There have been times when I've wished I had something that would do this. Thanks for making it. smile.gif
woratana
Nice~^^ I wrote this in XP half a year ago, and it's my first script! >_>/ Btw, does this script memorize @direction (It seems like it doesn't) ? And have you tested it on encrypted project? The other issues I see are that the process of getting/saving data could make a short pause for big maps, and data in text file can be edited to cheat game easily.
BigEd781
Good eye wora, I'll add saving direction. Encrypted projects will probably...fail. I could just save the text files in the working directory I suppose. If someone really wants to cheat by editing the files, I don't know that I really care. I tested with a max of 100 events, but I have a pretty nice machine. I would hope that reading in a simple text file would not take a long time, but I could try it out on my old laptop to be sure.

EDIT: Added directional saving. more to come...
woratana
My idea is to save it in $game_system or other class that will be stored in save file, because it won't affect other save files~^^
BigEd781
That's a good idea. For now, it will work with encrypted games (it just saves the files in the working directory). I will do it in Game_System though, that is a much safer solution. be back soon...

EDIT: Oh yeah, you can disable the effects of this script on events now by adding a comment to it.

EDIT2: No more reliance on .txt files, using Game_System to store data now. Greatly reduces chances of bugs cropping up (thanks Wora wink.gif)
woratana
Your welcome >_>//
lahandi

This script is very usefull for boulder pushing event.
Thanks mate!

irbis
okey so where the hell is
"#-----------------------------------------------------------------------
# * Set this to the switch number that you will
# use to turn this script ON or OFF.
# ---------
# Set to switch id "1" by default.
#
MOVE_EVENTS_SWITCH_ID = 1"

inside the script? maybe i gone blind but i cant find it
Kread-EX
Yeah, it's not in. Just add it at the very top of the script and replace all occurences of $game_switches[1] by $game_switches[MOVE_EVENTS_SWITCH_ID].
irbis
ahhh thank you Kread!
Philip
This is kudos for an absolutly simple but amazing script!! The block pushing puzzles will never be the same LOL!!! Cool cool cool... downloading.

Please. You know the rules. Don't necropost. ~Kread
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.