MV Proper Cloning of JS Objects

This is where my familiarity with other languages causes me trouble: making copies of objects in JS. I'm used to being able to make pretty clean, independent, copies of something, and JS handles a lot of things by reference and it causes me no end of stress...

I'm using the Sanshiro MapGenerator (https://forums.rpgmakerweb.com/index.php?threads/map-generator.51808/) and arleq1n's Tactics system (https://forums.rpgmakerweb.com/index.php?threads/tactics-system-1-2-1-tactical-battle-system.117600/) for significant elements in my game. The following script borrows some concepts for the Sanshiro MapGenerator to copy template events for placing party members (and enemies) onto the map in various locales so we can run the battle.
My issue right now comes from JS and how it handles objects and references, IE that JS doesn't copy things all the time, it REFERENCES them, so that doing something to a "copy" affects the referenced object. In this particular case, we're setting up our party members. Everything works perfectly when we setup a single enemy and a single party member; the events are copied, details updated, events placed, battle goes on without a hitch.
Now, if we add a second party member, we have an issue.
We lay down our first party member, things are fine.
When we lay down our second party member, updates to portions of the event are applied to the first instance as well. The TBS uses the notetag/meta value <Party:X> or <Enemy:X> to designate what the unit is. When we update the party event so the meta tag is "2" for the second party member, the first event is updated as well, giving me two instances of party member 2.
Any thoughts on how to make an INDEPENDENT copy of the event, so things aren't passed around the copies by reference? Unless its not a JS thing but an unforeseen interaction in RPGM?

Note: "dataMapEventsXy" is a function to ID an event at the coordinates and return that template for the new Game_Event.

Spoiler: Code
JavaScript:
// Setup the party...
setParty = function() {
    var refXY = {x:0,y:2};

    for(i=1;i<=$gameParty._actors.length;i++) {
      var event = new Game_Event($gameMap.mapId(), dataMapEventsXy(refXY.x, refXY.y)[0].id);

      let dataEvent = $dataMap.events[event._eventId];
      let actor     = $gameActors.actor($gameParty._actors[i-1]);

      event.meta = dataEvent.meta;
      event.name = dataEvent.name + i;
      event.setImage(actor._characterName,actor._characterIndex);

      event.meta.Party = i;
      console.log("Event party member ID is..." + event.meta.Party);

      $gameMap._events.push(event);
      event._eventId = $gameMap._events.indexOf(event);

      var newLocationX = (Math.floor($dataMap.width/2)-Math.floor($gameParty._actors.length/2)) + (i-1);
      var newLocationY = $dataMap.height-3;

      event.setPosition(newLocationX, newLocationY);
      console.log("Set Player " + i + "...");

    }

    //Move the player...
    $gamePlayer.locate(newLocationX, newLocationY);
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);

}
The issue seems to be that you're using the same refXY in the Game_Event constructor, so your new events always have the same ID as any that came before them.
I am, because that's my template event.
That particular structure comes from the MapGenerator code, which references the event at the locale, and is then able to copy it. We copy that event, and then we need to assign the unique elements to it.
In the MapGenerator code at least, the events are replicated around the map and are their own thing. Own positions, states, etc.
The code initially grabs that copy, but then (is supposed to) updates the _eventId based on its position in the gameMap events array. That appears to update fine in the objects.
The events ARE cloned even; I wind up with the two entities on my screen, at unique X/Y positions. The meta data and images, however, are shared. That's partly what's confounding me; there are two events on the screen, at unique X/Y positions, but the meta data and sprite data are the same...

Edit to include some attachments: from the console, these are the results of the clone, with unique _eventIds. _dataEventIds is introduced by the Sanshiro MapGenerator to refer back to the source event when needed (which, nothing seems to). They have unique names it seems, the event IDs are reassigned, there's the unique positions...
So I AM realizing this may not be a JS clone issue. That part appears OK. But I have two parts (the meta data and the spite setting) that are not OK. Which feels weird; the names are separate, the meta data is not, both are assigned directly. The positions are separate, the sprites are not, both are assigned via function.
I am, because that's my template event.
That particular structure comes from the MapGenerator code, which references the event at the locale, and is then able to copy it. We copy that event, and then we need to assign the unique elements to it.
In the MapGenerator code at least, the events are replicated around the map and are their own thing. Own positions, states, etc.
The code initially grabs that copy, but then (is supposed to) updates the _eventId based on its position in the gameMap events array. That appears to update fine in the objects.
The events ARE cloned even; I wind up with the two entities on my screen, at unique X/Y positions. The meta data and images, however, are shared. That's partly what's confounding me; there are two events on the screen, at unique X/Y positions, but the meta data and sprite data are the same...
You're setting its meta values before updating the event ID though, so it's going to use the ones from the template event every time.

If you want them to be a unique object, you'll need to set meta to {} and then loop through the meta properties instead, setting that property in the new meta object to be the same value.

If you assign an object to another object, you're only going to create a reference to it.
If you want them to be a unique object, you'll need to set meta to {} and then loop through the meta properties instead, setting that property in the new meta object to be the same value.

If you assign an object to another object, you're only going to create a reference to it.
Alright, so, one part solved 🙂 It is a form of the JS copy/reference issue.
Using JSON.parse(JSON.stringify()) on the source meta data copied the data, not creating a reference, so now each of the events has a unique meta data set that can be manipulated!
The sprite issue was a by-product of the party issue, the TBS writing the actor details in based on the Party tag.
Now, I have other issues to tackle with this, but THAT ONE is done for. Thanks!

Edit: Doing this copies over all of the dataEvent.meta information as new information, not a reference. No need to iterate over the whole of the object.
JavaScript:
      event.meta    = JSON.parse(JSON.stringify(dataEvent.meta));
I would suggest MV/Z's core JsonEx.makeDeepCopy(baseObject) method instead. That should preserve constructor/prototype references where appropriate.

Regardless, note that this approach will not copy any non-stringifiable values.
I would suggest MV/Z's core JsonEx.makeDeepCopy(baseObject) method instead. That should preserve constructor/prototype references where appropriate.

Regardless, note that this approach will not copy any non-stringifiable values.
For my needs, it seems I just needed to apply this to the source meta data to make sure I had a workable copy.
Now fixing this seems to not have fixed another issue; seems the TBS doesn't acknowledge the second event as a party member. I had assumed having the two events with the same meta data was causing the issue, but it appears not. On to wrangle with that one, but it seems not-unusual for that TBS system...

Edit: AND tracked that one down, too. Unsure if anyone but me will ultimately care, but the issue was rooted in references back to the base event; the TBS used a script to pull parameters in such a way it went back to the source event, causing my two battlers to have the same ID, and the TBS wouldn't add the extra battlers because they all shared the same id. Updated those functions to pull the meta directly and voila~ My extra battlers are dynamically placed!
This is where my familiarity with other languages causes me trouble: making copies of objects in JS. I'm used to being able to make pretty clean, independent, copies of something, and JS handles a lot of things by reference and it causes me no end of stress...
Just to toss it out there, this isn't really some quirk that is unique to JS. Most OOP languages use reference semantics for dynamic objects.

Original thread: https://forums.rpgmakerweb.com/threads/proper-cloning-of-js-objects.165020/