colyseus-unity-sdk: v0.14 : KeyNotFoundException: The given key was not present in the dictionary.

I receive a key not found at https://github.com/colyseus/colyseus-unity3d/blob/master/Assets/Plugins/Colyseus/Serializer/Schema/ReferenceTracker.cs#L51

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <9577ac7a62ef43179789031239ba8798>:0)
Colyseus.Schema.ReferenceTracker.Remove (System.Int32 refId) (at Assets/Plugins/Colyseus/Serializer/Schema/ReferenceTracker.cs:51)

Fields generated by the last version of c# generator (original version)

Version : colyseus server : 0.14.4 colyseus unity : 0.14.2 schema : 1.0.6

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Hey, @endel I made a fork from colyseus-unity3d and implemented the behavior to trigger the bug.

it’s a card game you start with 52 cards and every time you click to play the cards button it plays 1 to 3 cards randomly if you hit it many times (e.g. once per second) it will trigger the bug before the remaining cards hit zero.

Repo: forked repo (card game)

Thanks!

Thank you, @Unelith this is helpful info!

@mitchLucid Perhaps extra details could be useful then:

  1. This ArraySchema holds instances of an AreaState Schema, whose definition is as follows:
export class AreaSettings extends SettingsGroup {
	@type('int32')
	distanceCalculationMode: number = 0;

	@type('int32')
	gridWidth: number = 10;

	@type('int32')
	gridHeight: number = 10;

	@type('string')
	distanceUnit: string = 'ft';

	@type('float64')
	gridSquareSideLength: number = 5.0;
}
export class AreaState extends Schema {
	@type('string')
	id: string;

	@type('string')
	name: string;

	@type( AreaSettings )
	areaSettings: AreaSettings = new AreaSettings();
}
  1. On the client, having retrieved an AreaState schema instance from either OnAdd or OnRemove listener attached to the ArraySchema, I do pass it around quite a lot, for instance:
room.State.areas.OnAdd += emitter.HandleAddArea;
public void HandleAddArea(AreaState _area, int _idx) {
    onAddArea.Invoke(_area, _idx);

    _area.OnChange += (_changes) => {
        HandleAreaChanges(_area, _idx, _changes);
    };
}
emitter.onAddArea.AddListener(delegate (AreaState _area, int _idx) {
    CreateAreaListEntryObject(_area);
});
_refs.deleteAreaButton.onClick.AddListener(delegate {
    HandleRemoveAreaButton(_area);
});

Which in turn calls this:

public async void RemoveArea(AreaState _area) {
    await colyseusClientController.CallService("deleteArea", new {
        areaId = _area.id
    });
}

@Unelith I will try to look into this soon but if it was fixed in the update it was done incidentally, so I wouldn’t begin migrating your project on that hope quite yet. I will try to set up a good repro case locally and test with the updated SDK to confirm one way or the other

@lee-orr can you share a snippet of the code deals with those operations and a snippet of your object with map schemas? If we can it will be easier for @endel to fix the bug on MapSchema

I’m getting the same issue, and then next time I try to update the same object I end up with:

Exception: refId not found: 6
Colyseus.Schema.Schema.Decode (System.Byte[] bytes, Colyseus.Schema.Iterator it, Colyseus.Schema.ReferenceTracker refs) (at Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs:226)
Colyseus.SchemaSerializer`1[T].Patch (System.Byte[] data, System.Int32 offset) (at Assets/Plugins/Colyseus/Serializer/SchemaSerializer.cs:31)
Colyseus.Room`1[T].Patch (System.Byte[] delta, System.Int32 offset) (at Assets/Plugins/Colyseus/Room.cs:351)
Colyseus.Room`1+<ParseMessage>d__37[T].MoveNext () (at Assets/Plugins/Colyseus/Room.cs:310)

I have a large object (containing a bunch of MapSchema’s) that I replace when this happens. Generally the first replacement will cause this but seem to work, while the second one doesn’t. I also tried instead of placing this object in a Map itself, deleting the old one and adding the new one in a new key - but it still causes the exact same issue. For me this is breaking - this object contains the building blocks for user-created maps, and without being able to update them & replace them I am completely stuck.