colyseus / colyseus-unity-sdk

⚔ Colyseus Multiplayer SDK for Unity
https://docs.colyseus.io/getting-started/unity-sdk/
MIT License
371 stars 102 forks source link

OnAdd + OnRemove not reliably called #87

Closed mobyjames closed 5 years ago

mobyjames commented 5 years ago

Please see latest in this repro for example: https://github.com/mobyjames/colyseus-unity3d

Steps:

  1. Start server
  2. Start Unity project
  3. Connect
  4. Create Room
  5. Send Message

This triggers the server to add a couple numbers to the array

Expected: OnAdd is called- it is not

A few seconds later, the server will remove these items

Expected: OnRemove is called for each- it's only called for one of them

mobyjames commented 5 years ago

Oddly, if I convert the field to ArraySchema<string> the onAdd method fires, but the issue with onRemove remains. It only fires for the first removed item.

endel commented 5 years ago

The OnRemove() issue was a silly mistake during the remove loop: https://github.com/colyseus/colyseus-unity3d/commit/f8ec63558470fdf9a7a90532daa881561b557d82

Apparently the OnAdd() wasn't triggering because of the check for new keys in the Dictionary (https://github.com/colyseus/colyseus-unity3d/commit/c7f9cbcdedc0aef93e8c36d3026aeca8862a7276). The currentValue[newIndex] returns default(int) instead of null. So I've changed this check to use .ContainsKey(newIndex) instead.

I'm afraid MapSchema also has the OnAdd() problem with numbers, I haven't tested with it yet!