=begin #=============================================================================== Title: Reference Events Author: Hime Date: Dec 20, 2013 -------------------------------------------------------------------------------- ** Change log Dec 20, 2013 - improved map load time by caching maps instead of reloading everytime - Changed note-tag to make the map ID an optional value Oct 12, 2013 - initial release -------------------------------------------------------------------------------- ** Terms of Use * Free to use in non-commercial projects * Contact me for commercial use * No real support. The script is provided as-is * Will do bug fixes, but no compatibility patches * Features may be requested but no guarantees, especially if it is non-trivial * Credits to Hime Works in your project * Preserve this header -------------------------------------------------------------------------------- ** Description This script allows you to create events using a "reference event". The purpose is to allow you to create an event once on a map and re-use it across multiple maps. This is useful when you have multiple copies of the same event, but you want to avoid having to change each event manually if you decide to change a command or graphic. For example, suppose you were implementing on-screen enemies where touching an enemy event will begin a battle. If you want to have 20 of these enemies on the map that all do the same thing, rather than copy-pasting one event 20 times, you would first create the enemy event with all of the logic, and then create a second event that will treat the enemy event as the reference event. Then you would copy the second event again and again, so that if you decide to change the enemy event, you don't have to delete all of the enemy events and redo the copy paste process. -------------------------------------------------------------------------------- ** Installation In the script editor, install this script below Materisls and above Main -------------------------------------------------------------------------------- ** Usage To designate a reference event, create a comment on the first page of an event of the form Where `event_id` is the ID of the reference event `map_id` is the ID of the map of the reference event If you don't specify a map ID, it is assumed to be the current map. When the map is loaded, the event will be replaced by the reference event (except its ID) #=============================================================================== =end $imported = {} if $imported.nil? $imported["TH_ReferenceEvents"] = true #=============================================================================== # ** Configuration #=============================================================================== module TH module Reference_Events Regex = //i end end #=============================================================================== # ** Rest of Script #=============================================================================== module RPG class Event def reference_event parse_reference_event unless @reference_event return @reference_event end def parse_reference_event @reference_event = Data_ReferenceEvent.new self.pages[0].list.each do |cmd| if cmd.code == 108 && cmd.parameters[0] =~ TH::Reference_Events::Regex @reference_event.event_id = $1.to_i @reference_event.map_id = $2 ? $2.to_i : $game_map.map_id break end end end end end class Data_ReferenceEvent attr_accessor :map_id attr_accessor :event_id def initialize @map_id = -1 @event_id = -1 end end class Game_Map alias :th_reference_events_setup_events :setup_events def setup_events setup_reference_events th_reference_events_setup_events end #----------------------------------------------------------------------------- # Replace any events that have a reference event assigned #----------------------------------------------------------------------------- def setup_reference_events cache = Hash.new {|hash, key| hash[key] = load_data("Data/Map%03d.rvdata2" %key) } @map.events.each do |id, event| data = event.reference_event if data.map_id != -1 map = cache[data.map_id] ref_event = Marshal.load(Marshal.dump(map.events[data.event_id])) ref_event.id = event.id ref_event.x = event.x ref_event.y = event.y @map.events[id] = ref_event end end end end