core icon indicating copy to clipboard operation
core copied to clipboard

deep objects in effect state

Open ratacat opened this issue 6 years ago • 2 comments

It seems like some unexpected behaviors can happen when using deep objects in an effect state. I have an array of nested objects in an effect definition. Once the effect is activated, it starts editing / rearranging though nested objects.

If multiple copies of the effect are created later, the state of the deep objects that are edited is copied over to the new effects. So it seems to me that the weird part is actually that the definition of the effect is being rewritten.

I ended up working around it for the moment by creating a deep copy of the original nested objected before working on them within the effect.

this.state.messageList = JSON.parse(JSON.stringify(this.state.messageListOriginal));

But thought I'd post about it to hear any thoughts from everyone.

ratacat avatar Aug 28 '19 19:08 ratacat

Yeah the workaround for this is to put that exact line of code into the effect's effectAdded listener. A possible core fix for this would be to add this.state = JSON.parse(JSON.stringify(this.state)); to EffectList#add so that as soon as an effect is added to an entity it gets its own copy of state

shawncplus avatar Aug 28 '19 21:08 shawncplus

I addressed this in #121.

azigler avatar Jun 07 '20 03:06 azigler