Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArraySchema keys not updated when elements are removed #152

Open
jushii opened this issue Aug 24, 2023 · 1 comment
Open

ArraySchema keys not updated when elements are removed #152

jushii opened this issue Aug 24, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@jushii
Copy link

jushii commented Aug 24, 2023

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.

@endel endel added the bug Something isn't working label Aug 24, 2023
@endel
Copy link
Member

endel commented Aug 24, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants