Good way to add dynamic events to the map?

Right now I'm working with dynamic event spawning on the map.



I could always pre-create some static events and then just move them around as needed, but if I want the event to be spawned in any map, I'd rather just keep the event in one "set up" map and then just generate a new event using that as a template.


Ok, so creating a new event involves picking an ID. I could probably safely assume the length of the event list + 1, unless I'm really generating a lot of events and I hit some limit.


Once an ID is picked, I just need to create a Game_Event object and then assign it to the array of events.


Now, how would I have the spriteset know to draw it?


The spriteset is held by the current scene


1. I could have a method in Spriteset_Map constantly poll the map for any new events that need to be added.


2. I could have tell SceneManager to tell the current scene to call a certain method in the current spriteset.


3. I could explicitly access the current scene through the SceneManager and tell the spriteset to do something (which is like #2 except I'm being more intrusive)


For me, #2 makes the most sense. The SceneManager is in charge of managing the scene, so if I need the scene to do something, I can go tell the scene manager to do it.



I don't like #1 because polling requires constant resource use, and "event spawning" should ideally just fire a trigger that sends a new task to the spriteset, and it's up to the spriteset to determine whether there are any tasks to do.


However, this means that the spriteset is still going to be polling its own list of tasks to do, which is no different from just having it poll the map for any changes in the first place (maybe a bit better in design)


Having the map create a new event is easy, but how should I tell the spriteset to generate new sprites?
In my ABS I went with your #2 method. I would call a function that looked like:


QuasiABS.Manager.addLoot = function(loot) {
var scene = SceneManager._scene;
if (scene.constructor !== Scene_Map) return;
loot._eventId = $gameMap._events.length;
this.loot.push(loot);
$gameMap._events.push(loot);
var sprite = new Sprite_Character(loot);
scene._spriteset._tilemap.addChild(sprite);
};


the loot was already a Game_Character object. I was also unsure with what to use for the Id, but I just went with the event list size.
For #2 I was thinking something like this


SceneManager.addSomeEvent = function(ev) {
this._scene.addSomeEvent(ev);
};

Scene_Base.prototype.addSomeSprite = function(ev) {
// stub. Other scenes can use if needed
};

Scene_Map.prototype.addSomeSprite = function(ev) {
// do stuff with ev
};


And all requests would go through SceneManager.


So if someone had like their own map scene, they could handle new stuff just by adding an implementation to the method I provide.
Ah yes, I was originally writing it in a similar way as that. But then I was like, this is too many functions, I'll just keep as in a single function. Though I was expecting anyone to be using any of my functions.
One concern I have with using the length of the array plus 1 as event ID is that if lots of events were being spawned, you might run into a scenario where you have a huge array.


Not sure if that would actually be a problem in general, especially for things like loot where you probably aren't going to have millions of loot being dropped on the same map, without having changed maps and refreshing that array.


I'm currently standardizing event deletion to just null out the element, which allows me to re-use that index again.
I've been through this before, when building the OrangeCustomEvents plugin.


Back on Ace, I had modified your script to use my own event Id list starting at 1000. When I made the same thing on MV, some people reported having some lag.


I didn't want to simply use Length + 1, because I had a lot of problems with that on Ace: If you create an event via script, save your game and then modify the map to add a new event, that new event would pick the save ID I picked for my event and it wouldn't appear at all when the player reloads the game. Using event ID's starting at 1000 solved the problem for Ace, but in MV I had to do something else:


Game_Map.prototype.getIndexForNewEvent = function() {
var index = 1;
while (index < this._events.length && !!this._events[index]) {
index++;
}

return index;
};




I iterate over all existing events to get the first ID that is not used by any event. To avoid the problem I mentioned earlier, everytime the game is loaded or the player enters a new map, I pick a fresh ID for all of my events:


var oldGameMap_setupEvents = Game_Map.prototype.setupEvents;
Game_Map.prototype.setupEvents = function() {
oldGameMap_setupEvents.call(this);

var customEvents = $gameSystem.getCustomEvents(this._mapId);
for (var eventId in customEvents) {
if (customEvents[eventId] === undefined) continue;
var newEventId = this.getIndexForNewEvent();

customEvents[eventId].eventId = newEventId;
this._events[newEventId] = new Game_Custom_Event(this._mapId, newEventId, customEvents[eventId]);
}
};




This solved both the lag and the problem of events not appearing when loading old save files.

Original thread: https://forums.rpgmakerweb.com/threads/good-way-to-add-dynamic-events-to-the-map.55960/