ArraySchema with Schema Objects?
-
Hello All,
I am getting an error trying to implement
ArraySchema
. Any help would be appreciated :)I have this
Player
Schema:import { Schema, type } from '@colyseus/schema'; export class Player extends Schema { @type('string') username: string; @type('string') sessionId: string; }
And this
MyRoomState
Schema which has anArraySchema<Player>
:import { Schema, ArraySchema, type } from '@colyseus/schema'; import { Player } from './Player'; export class MyRoomState extends Schema { @type([Player]) players = new ArraySchema<Player>(); }
And this is the
MyRoom
code that tries to push to the array of players:import { Room, Client } from "colyseus"; import { MyRoomState } from "./schema/MyRoomState"; import { Player } from './schema/player'; export class MyRoom extends Room { ... onJoin(client: Client, options: any) { let player: Player = new Player(); player.username = options.username; player.sessionId = client.sessionId; this.state.players.push(player); this.broadcast('user-joined', `${player.username} joined room: ${this.roomId} with session: ${player.sessionId}`); } ... }
But I am getting this error on the server:
Error: a 'Player' was expected, but 'Player' was provided in ArraySchema#0 at new EncodeSchemaError (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\lib\Schema.js:48:42) at assertInstanceType (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\src\Schema.ts:84:15) at MyRoomState.Schema.encode (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\src\Schema.ts:598:21) at MyRoomState.Schema.encodeAll (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\src\Schema.ts:650:21) at SchemaSerializer.getFullState (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\serializer\SchemaSerializer.ts:25:41) at MyRoom.sendFullState (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\Room.ts:468:77) at MyRoom._onMessage (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\Room.ts:593:14) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\ws\lib\websocket.js:825:20) at Receiver.emit (events.js:315:20) [ERROR] 19:52:16 Error: a 'Player' was expected, but 'Player' was provided in ArraySchema#0 Error: a 'Player' was expected, but 'Player' was provided in ArraySchema#0 at new EncodeSchemaError (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\lib\Schema.js:48:42) at assertInstanceType (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\src\Schema.ts:84:15) at MyRoomState.Schema.encode (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\@colyseus\schema\src\Schema.ts:598:21) at SchemaSerializer.applyPatches (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\serializer\SchemaSerializer.ts:47:34) at MyRoom.broadcastPatch (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\Room.ts:433:41) at Timeout._onTimeout (C:\Users\dawic\Desktop\FullStack\mean\colyseus-server\node_modules\colyseus\src\Room.ts:156:52) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)
-
Hi @dawickizer, do you possibly have two versions of the schema module loaded somehow?
-
This post is deleted!
-
@endel figured it out. I was just being dumb lol.
import { Player } from './Player';
inMyRoomState.ts
and
import { Player } from './schema/player';
inMyRoom.ts
Accidentally capitalized one of the imports
-
I see! Glad you found out. There is a TypeScript configuration to prevent this from happening called
forceConsistentCasingInFileNames
:// tsconfig.json { "compilerOptions": { // (...) "forceConsistentCasingInFileNames": true, } } ``