store an event in memory, spawn new unique copies at runtime?

Is there a best practice or good root object to use for copying an event off the map during loading, then recreating all the data needed to insert a new event during gameplay or even map loading?


My goal is to be able to, during the map data loading done in Game_Map.prototype.data function:


1 - grab some events the user made in the editor, by name  (I got this part, I think).


2 - put these events back into the game just after modding the map data, but before the game runs the map.  I don't understand what the engine does with events and characters during the map scene for this part.


I've tried copying $dataMap.events to an array using JsonEx.makeDeepCopy($dataMap.events);





 





But when I put them into the gameMap object with $gameMap._events[next_id]=JsonEx.makeDeepCopy(dg_events[2]);





 





I get a terrible crash saying: 






Quote







TypeError: undefined is not a function


    at file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_objects.js:6154:22


    at Array.some (native)


    at Game_Map.isAnyEventStarting (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_objects.js:6153:26)


    at Game_Map.isEventRunning (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_objects.js:5421:50)


    at Game_Player.canMove (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_objects.js:7609:18)


    at Scene_Map.isMapTouchOk (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_scenes.js:485:43)


    at Scene_Map.updateDestination (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_scenes.js:476:14)


    at Scene_Map.update (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_scenes.js:397:10)


    at Function.SceneManager.updateScene (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_managers.js:1788:25)


    at Function.SceneManager.updateMain (file:///C:/Users/DarkGriffin/Documents/Rpg%20maker%20mv/CodeProject/js/rpg_managers.js:1754:14)









 





In other words, that does not appear to be the object it's expecting there.





 





Does anyone know the object I should be copying or replacing into to make this work?  I'm having hell using the debugger and javascript code to figure out how this is working under the hood during a map display loop, or what object/function I should be "loading" my copied event data into.  Any event programmers here who know this better then me?
Cloning an event is not that simple, but there are plenty of plugins that do that already. You can look at their code to understand what's being done. Here's mine:
Thanks for the reply.  Looks like I dove into the deep end of things, lol.  


I had no idea the game engine split up the sprite objects in the map data and the event page array.  Though in hind sight, it does make sense since "event pages" can be battle or common events too.


Looks like I have my homework cut out for me.  I'll have to save this for a day I'm not so busy.  Too many interconnected systems to figure out in the little half hour bursts I normally have for MV coding.  I already memorize code for my other game programming in other engines, so the event structures and calls are not really sticking when I try to study it.  I'll grasp it eventually, somehow.

Original thread: https://forums.rpgmakerweb.com/threads/store-an-event-in-memory-spawn-new-unique-copies-at-runtime.63074/