RanvierMUD / core

Core engine code for Ranvier
https://ranviermud.com
MIT License
45 stars 40 forks source link

deep objects in effect state #101

Open ratacat opened 5 years ago

ratacat commented 5 years ago

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.

shawncplus commented 5 years ago

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

azigler commented 4 years ago

I addressed this in #121.