Schema `OnChange` delegates not removable
See original GitHub issueColyseus Unity client version: 0.14.7 (latest) Colyseus server version: 0.14.1 (not latest, but issue is client-side)
If you store a reference to a Schema
object other than the root schema, later (after a schema patch?) attempting to remove a delegate from that reference’s OnChange
event will fail silently.
See comments in the example client code below for more specifics on what’s going wrong. It looks like Colyseus is doing something to the OnChange
references that’s breaking the ability to remove delegates.
Example server code:
export default class Phase extends Schema {
@type('int32')
public value: number;
constructor() {
super();
this.value = 1;
}
}
export default class GameState extends Schema {
@type(Phase)
public readonly phase: Phase;
constructor() {
super();
this.phase = new Phase();
}
}
export default class GameRoom extends Room<GameState> {
// ...
onCreate(options: any) {
let gameState = new GameState();
this.setState(gameState);
this.clock.setInterval(() => {
console.log('changing phase');
this.state.phase.value++;
}, 1000);
}
}
Example client code:
class MyClass {
private GameState gameState;
private Phase phase;
// Start by calling this only once.
private void AddDelegate(GameState gameState) {
this.gameState = gameState;
this.phase = gameState.phase;
this.gameState.phase.OnChange += MyCallback1;
this.phase.OnChange += MyCallback2;
}
private void MyCallback1(List<DataChange> change) {
Debug.Log("MyCallback1 called");
// This unregisters the callback correctly.
this.gameState.phase.OnChange -= MyCallback1;
}
private void MyCallback2(List<DataChange> change) {
Debug.Log("MyCallback2 called");
// This doesn't unregister the callback correctly. No error is thrown, and this callback keeps
// getting called.
this.phase.OnChange -= MyCallback2;
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
C# Delegation is not being removed
Here's what to do: Step 1: Change your event handler to take an instance of the picture box and not the index to...
Read more >How to manage different delegate signatures/Types ...
With a Dictionary<Type, List<object>>, i can store by delegate Type, and store the instance as an object. This messes up OOP which i...
Read more >Event delegation
Hide messages with delegation There's a list of messages with removal buttons [x] . Make the buttons work.
Read more >Clean event handler invocation with C# 6
It does this by showing that a tight loop that reads such a field will terminate when the field is changed by another...
Read more >C# static code analysis: Anonymous delegates should not ...
Anonymous delegates should not be used to unsubscribe from Events ... This happens because to remove the entry from the subscription list, a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Interesting, thanks for reporting again @rnd256, I’m gonna have a look at this during this week!
I already found the solution, your comments were helpful, thank you