MV Object Copy Problem

Hello peeps,

I've been trying to develop an orb system that is similar to how defect operates in Slay the Spire if any of you are familiar. If not, this is how it works: There are slots for orbs to go in, each orb does something (usually at turn end but not necessarily) and when you have all slots full and create more, the oldest one gets popped and the newly created one gets added to the front. There are also skills that make use of these orbs to do things.

They way I've done it so far is that I have one visible state(passive state) that contains the array of possible and current orbs. Here's my code for it:

Spoiler: Code
JavaScript:
<Custom Battle Effect>

target.orbsAll = [
{
"type": "plasma",
"duration": 5,
},
{
"type": "staff",
"duration": 10,
},
{
"type": "book",
"duration": 3,
}
];

target.orbs = [];

</Custom Battle Effect>


Then I have a skill to create each type of orb as well as a state that deals with each type of orb. Here are my codes for the skills and states.

Spoiler: Code
Skill 1

JavaScript:
<Instant>

<Custom Execution>

user.addState(43);

user.orbs.unshift(user.orbsAll[0]);

if (user.orbs.length > 3)
user.orbs.pop();


</Custom Execution>



State 1


JavaScript:
<Custom Turn End Effect>
 
 var group =  $gameTroop.aliveMembers();


if (target.orbs[0].type == "plasma") {
   var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(-8);
   targeted.startDamagePopup();
   target.orbs[0].duration = target.orbs[0].duration -1;
}

if (target.orbs[1].type == "plasma") {
    var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(-8);
   targeted.startDamagePopup();
   target.orbs[1].duration = target.orbs[1].duration -1;
}

if (target.orbs[2].type == "plasma") {
     var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(-8);
   targeted.startDamagePopup();
   target.orbs[2].duration = target.orbs[2].duration -1;
}

</Custom Turn End Effect>



Skill 2



Code:
<Instant>




<Custom Execution>




user.addState(44);




user.orbs.unshift(user.orbsAll[1]);




if (user.orbs.length > 3)


user.orbs.pop();






</Custom Execution>



State 2

Code:
<Custom Turn End Effect>
 
 var group =  $gameParty.aliveMembers().filter(member => member.hp < member.mhp);


if (target.orbs[0].type == "staff" && group.length > 0) {
   var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(5);
   targeted.startDamagePopup();
   target.orbs[0].duration = target.orbs[0].duration -1;
}

if (target.orbs[1].type == "staff" && group.length > 0) {
    var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(5);
   targeted.startDamagePopup();
   target.orbs[1].duration = target.orbs[1].duration -1;
}

if (target.orbs[2].type == "staff" && group.length > 0) {
     var targeted = group[Math.floor(Math.random() * group.length)];
   targeted.gainHp(5);
   targeted.startDamagePopup();
   target.orbs[2].duration = target.orbs[2].duration -1;
}

</Custom Turn End Effect>



The effects of the orbs run smoothly, but today I tried to add the duration ticking mechanic and encountered a big problem. When I have multiples of the same type of orb, ticking down one's duration ticks down the duplicates as well. Not only that, ticking down the duration of an orb in the active orbs array ticks down the duration of the originals that are in the original array.

I had thought that pushing objects from the original array into the active orbs array would make copies that could be independently modified but this suggests to me that I'm merely referencing the same object instead of making independent copies.

I've fished around the internet for solutions but haven't found an easy way to circumvent this. How would I be able to do what I want to do here?
JavaScript:
const copy = JsonEx.makeDeepCopy(object);
would be one of the ways to do it.

.unshift() puts new Array items at the beginning and mutates the original array - I don't see anything else that would be making a copy of it unless I missed it somewhere.

Try that or you could also try
JavaScript:
const clone = structuredClone(object);

You could also condense your custom turn end code as well (untested):
JavaScript:
<Custom Turn End Effect>
const group = $gameTroop.aliveMembers();
for (const orb of target.orbs) {
  if (orb.type === 'plasma') {
    const targeted = group[Math.randomInt(group.length)];
    targeted.gainHp(-8);
    targeted.startDamagePopup();
    orb.duration--;
  }
}
</Custom Turn End Effect>
Or if you object is simple (there's no object properties), you can just use shallow copy.

object2 = {...object1}
@utunnels

The objects I use here have properties, so I'm assuming that method won't work.


@bass2yang


Thank you! The .makeDeepCopy function worked. I had -wrongly- thought that pushing an element from one array to another with .unshift would create a copy as it doesn't change the array I'm pulling from but it seems it merely copies the reference, not the whole object.

One thing I'm concerned with now however is that, if I keep making copies this way throughout a battle, what happens to those copies once a battle ends? I'm assuming the copies are deleted because at battle start, I define the array they exist in as empty, but if they are stored in someplace, would that cause issues/potential bloating throughout a game?
As long as the property is not an object that need copy it is OK.
Like your first post.
@utunnels

The objects I use here have properties, so I'm assuming that method won't work.


@bass2yang


Thank you! The .makeDeepCopy function worked. I had -wrongly- thought that pushing an element from one array to another with .unshift would create a copy as it doesn't change the array I'm pulling from but it seems it merely copies the reference, not the whole object.

One thing I'm concerned with now however is that, if I keep making copies this way throughout a battle, what happens to those copies once a battle ends? I'm assuming the copies are deleted because at battle start, I define the array they exist in as empty, but if they are stored in someplace, would that cause issues/potential bloating throughout a game?
As utunnels stated, a simple copy of it would be fine.

You can iterate with the spread syntax {...object} (I think this will work, haven't tested,

(Edit: do [...object] to keep it as an array instead of {})

use .slice(0), or .clone() from the corescripts and it should work as well since your data is technically an array with objects inside.
JavaScript:
// utunnel's suggestion with array spread
const obj1 = [...user.orbs];
// clone
const obj2 = user.orbs.clone();
// or slice
const obj3 = user.orbs.slice(0);
Since you are defining the variable with const/let, it will only be available during the scope of that script call and will be gone once the script call is evaluated/complete.

Original thread: https://forums.rpgmakerweb.com/threads/object-copy-problem.174974/