colyseus-unity-sdk icon indicating copy to clipboard operation
colyseus-unity-sdk copied to clipboard

"Local schema mismatch from server" - when defining multiple rooms/states

Open endel opened this issue 3 years ago • 5 comments

This error is reported often, and one of its causes is when multiple room/state classes are defined in the server with the same schema "context".

Internally, every @type() call is going to register the property in a "global" context:

import { type, Schema } from "@colyseus/schema";

class State1 extends Schema {
  @type(...) my_prop
}

class State2 extends Schema {
  @type(...) my_prop
}

To avoid registering every schema in the global context, you can create a different context like this:

import { Context, Schema } from "@colyseus/schema"; // do not import "type" here

const type = Context.create(); // this is your @type() decorator bound to a context

class State2 extends Schema {
  @type(...) my_prop
}

Actions needed:

  • Update documentation to encourage creating different contexts, especially in Unity because of the crash
  • Throw WARNING on the client-side instead of ERROR when handshake sends more structures than necessary

endel avatar Dec 01 '20 11:12 endel

Hi @endel, I also got this error, but i am using javascript instead of typescript on the server side. Can u give a javascript example? Thanks a lot.

sorrow31812 avatar Dec 16 '20 09:12 sorrow31812

Hi @sorrow31812, for plain JavaScript it is a bit complicated, the general suggestion is to always use TypeScript:

// javascript
const schema = require("@colyseus/schema");

// create a new context
const context = new schema.Context();

class MyState extends schema.Schema {
}
schema.defineTypes(MyState, {
  currentTurn: "string"
}, context); // use the context

Make sure you use the specified context for every structure your state depends on, otherwise the "schema mismatch" is going to happen

endel avatar Dec 16 '20 11:12 endel

I get it, thanks for your help! 👍

sorrow31812 avatar Dec 17 '20 08:12 sorrow31812

Another known appearance of the "schema-mismatch" problem was just found by @drburton when duplicating a field name through inheritance. Just documenting this here, as a fix is coming soon!

How to reproduce

class Entity extends Schema {
    @type("string") id: string;
    @type("string") ownerId: string;
    @type("number") xPos: number = 0;
    @type("number") yPos: number = 0;
    @type("number") zPos: number = 0;
}

class Player extends Entity {
    @type("string") id: string; // duplicate "id" from Entity - "schema-mismatch" is going to happen!
    @type("boolean") connected: boolean;
}

EDIT: since @colyseus/[email protected] the above definition is going to throw an error pointing out where the duplicate definition is located at. (https://github.com/colyseus/schema/commit/a8b86e998c1df3c32a470fdd27d4e2e3c7cffcbd)

endel avatar Apr 06 '21 17:04 endel

hello dear friends i have this error only in android build of unity (apk), in editor when i running game every thing is ok

hdev72 avatar Jun 21 '22 16:06 hdev72