Introduction:
This plugin provide several functions to spawn or copy events.
spawnEvent(which,tag, x, y,d)
Spawn an event at location x, y
Parameters:
which: the event template, see 'Event template' below
tag: if a dead event contains this tag string in its note, it will be revived instead, and 'which' will be ignored unless the dead event is not found. By default, an event is dead if its _erased property is true. If tag is false, the function will not try to find a dead event and always spawn a new one. If tag is a function, it will be used to search for a dead entity using its own rule. The function must have a parameter which is the event to be tested, if the entity is dead the funcion should return true, otherwise it returns false.
x, y: location of the spawned event. Optional.
d: direction of the spawned event. Optional.
replaceEvent(e1,e2)
Replace event e1 with e2. An event using 'Event template' e2 will be spawned and replace event e1.
Game_Event.setPreserved(preserved)
Set if the event is preserved. A preserved event will not reset if the player leaves current map and returns.
Parameter preserved should be a boolean value.
Game_Event.isPreserved()
Check if the event is preserved.
Game_Event.reload()
Reload current event. If the event is foreign, it will use it's original position in the foreign map. If the event has been replaced, the original event will be loaded instead. Notice this is a 'had reset', so everything including self switches will be reset.
$E(which)
Return an event, parameter 'which' should be in the format of 'Event template', see below.
$spr(which)
Return an event's sprite, parameter 'which' should be in the format of 'Event template', see below. This is not part of the spawn functions, but just in case the sprite is changed for some reason.
Event template:
Any event from the map editor can be used as template to spawn.
When using as a parameter, it can be any of the format below:
Event id: it should be an integer.
Event name: a string indicating the name of the event, if multiple events have the same name, the first one will be used.
Event tag: if name is not found, the function will try to find the string inside the note property. A tag can be any string, but it is recommended to use html like tags.
Event object: You can provide an event object to copy from
Object with an eventId method. Usually it means an Game_Interpreter object, so you can use 'this' as parameter in an event command script.
Event from another map: This is only used by spawnEvent function. An event from a foreign map will be spawned in current map, and can be preserved if you use setPreserved to change it's preserved property. In this case, the parameter should be a string in the form of 'event@map'. 'event' can be either id, name or tag. 'map' can be either map id, filename, or map name. For example, '1@Map001', 'cat@1', '<dog>@village_of_fire'...
var mi = $dataMapInfos.findIndex(function(m){return m&&m.name==mapId;});
if(mi>0){
filename = 'Map%1'.format(mi.padZero(3));
}else{
return null;
}
}
var map = $mapCache[filename];
if(!map){
var map = loadDataJSONSync(filename);
if(!map) return null;
$mapCache[filename] = map;
map.id = Number(filename.substring(3));
}
return map;
}
function readForeignEventInfo(which){
var ns = which.split('@');
var map = loadMapJSONSync(ns[1]);
fmapid = map.id;
var e = map.events[ns[0]]||map.events.find(function(e){return e&&e.name==ns[0];})||map.events.find(function(e){return e&&e.note.contains(ns[0]);})||null;
e.mapId = fmapid;
return e;
}
function copyEvent(which,newonly){
var fmapid,feid;
if(typeof which=='string'&&which.contains('@')){
var e = readForeignEventInfo(which);
var slot;
if(e){
if(e.mapId!=$gameMap.mapId()){
fmapid = e.mapId;
feid = e.id;
var fe = $gameMap._events.find(function(e){return e&&e._foreignMapId==fmapid&&e._foreignEventId==e.id;});
if(fe) slot = fe.originalEventId();
else{
slot = $gameMap._events.length;
e = JSON.parse(JSON.stringify(e));
$dataMap.events[slot] = e;
}
which = e.id = slot;
}else{
which = e.id;
}
}else{
throw "copyEvent: event not found - " + which;
}
}
if(typeof which!='number'){
var we = $E(which);
if(we){
which = we.originalEventId();
}else{
we = $dataMap.events.find(function(e){return e&&e.name==which;})||$dataMap.events.find(function(e){return e&&e.note.contains(which);});
if(!we){
throw 'copyEvent: event not found - ' + which;
}
which = we.id;
}
}
var freeid = $gameMap._events.findIndex(function(ev,i){return i>0&&!ev;});
freeid = freeid>0?freeid:$gameMap._events.length;
var event = new Game_Event($gameMap._mapId, which);