schema icon indicating copy to clipboard operation
schema copied to clipboard

ArraySchema keys not updated when elements are removed

Open jushii opened this issue 2 years ago • 1 comments

Hi,

I'm working on a turn-based game where the turn order of my entities is managed using ArraySchema<string>. The element at index 0 is the current acting entity. I use turnOrder.shift() to change the turn.

I've noticed that when elements are removed from the ArraySchema, the key of the elements is not updated to match the element's index in the array. I wonder if this is correct behaviour?

Also, when I use clear to clear the ArraySchema and then push a new element to the array, the keys do not start counting from 0 and it seems like the $refId of the ArraySchema just keeps increasing.

Some time ago I had a similar project created using Colyseus 0.14 where I used the same logic for managing the turn order. I don't recall ArraySchema exhibiting this kind of behaviour.

I'm not that familiar with how the Schema internals work, but as an experiment I added this.$refId = 0; inside the clear method and that "fixes" the issue for me, at least for the clear function (it probably breaks something too, haha).

I did notice a few people mention this issue in Colyseus Discord channel, but seems like there was no good fix proposed for it.

jushii avatar Aug 24 '23 17:08 jushii

Hi @jushii, thank you for the detailed report and the workaround. This is not the intended behavior.

I just added your issue report to the framework's roadmap here on "fixing/improving ArraySchema" alongside another scenario recently reported by @meSerge8 on https://github.com/orgs/colyseus/discussions/589

(Another workaround that might work is using array.at() / array.setAt() instead of the array accessor, but also not ideal, this should be properly fixed in the future)

endel avatar Aug 24 '23 17:08 endel

This is hopefully fixed now on Colyseus 0.16 / @colyseus/schema 3.0 🎉 https://github.com/colyseus/colyseus/releases/tag/0.16.0

endel avatar Feb 18 '25 21:02 endel