colyseus / colyseus-unity-sdk

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

Cannot overwrite message callback #197

Open sticmac opened 2 years ago

sticmac commented 2 years ago

When using ColyseusRoom.OnMessage twice on the same message ID, I get the following exception:

ArgumentException: An item with the same key has already been added. Key: myMessage

An easy fix would be to modify the OnMessage method to take in consideration if a previous message with the given id already exists. However, I wanted to know if this is a bug or a real design choice before submitting a PR.

endel commented 2 years ago

Thanks for reporting, @sticmac,

We've added support for multiple callbacks on the other clients for upcoming version 0.15 but it turns out the Unity SDK is still missing this feature (on https://github.com/colyseus/colyseus-unity-sdk/pull/177)

Ideally, the Unity SDK should be able to attach and detach multiple callbacks, for example:

var detachCallback1 = room.OnMessage("xx", () => { /* first */ });
var detachCallback2 = room.OnMessage("xx", () => { /* second */ });

// detaching the "OnMessage" callback:
detachCallback1();
detachCallback2();
sticmac commented 2 years ago

I made a pull request (#199) containing a solution like the example you provide :)