You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Subject: Seeking Advice: Code Structure Best Practices for Colyseus Project
Hi Colyseus Community,
I hope this message finds you well. I'm currently working on a Colyseus project and I'm seeking some advice on structuring my code according to best practices. Specifically, I'm looking to split my code into smaller, more manageable components.
Here's a brief overview of what I've done so far:
I've attached the code for my main room file (room.ts) and the GameManager class (gameManager.ts). In the GameManager, I'm attempting to utilize this.broadcast from the main room file within the GameManager.
However, I encountered an error stating "TypeError: this.broadcastMessageType is not a function," which has left me puzzled about the correct approach to split my code effectively.
I've reviewed the Colyseus documentation and found some general recommendations and best practices that suggest keeping room classes small, delegating game-specific functionality to other composable structures, and avoiding heavy game logic inside schema structures.
Here's what I'm trying to achieve:
Keep the room classes as small as possible.
Ensure that synchronizeable data structures are minimal.
Avoid implementing heavy game logic inside schema structures.
Delegate game logic to other structures or external functions.
Could you please provide guidance on how to structure my code according to these best practices? Any insights, examples, or recommendations would be greatly appreciated.
// main room
import { Room, Delayed } from '@colyseus/core';
import { xState } from '../schema';
import { GameManager } from './gameManager';
export class Main extends Room<xState> {
private gameManager: GameManager | null = null;
public delayedInterval!: Delayed;
constructor() {
super();
this.autoDispose = false;
}
onCreate(options: any) {
if (options.secret !== 'secret') {
throw new Error('unauthorized');
}
this.setMetadata(options);
this.maxClients = options.maxClients;
this.setState(new xState());
this.state.something = options.something;
let self = this;
this.gameManager = new GameManager(this.clock, this.presence, this.broadcast, this.state);
self.clock.start();
this.onMessage('action', (client, data) => {
switch (data.action) {
case 'join':
this.gameManager._startGame(client.sessionId);
break;
case 'quit':
this.gameManager._xResolver(client.sessionId);
break;
}
});
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Subject: Seeking Advice: Code Structure Best Practices for Colyseus Project
Hi Colyseus Community,
I hope this message finds you well. I'm currently working on a Colyseus project and I'm seeking some advice on structuring my code according to best practices. Specifically, I'm looking to split my code into smaller, more manageable components.
Here's a brief overview of what I've done so far:
I've attached the code for my main room file (room.ts) and the GameManager class (gameManager.ts). In the GameManager, I'm attempting to utilize this.broadcast from the main room file within the GameManager.
However, I encountered an error stating
"TypeError: this.broadcastMessageType is not a function,"
which has left me puzzled about the correct approach to split my code effectively.I've reviewed the Colyseus documentation and found some general recommendations and best practices that suggest keeping room classes small, delegating game-specific functionality to other composable structures, and avoiding heavy game logic inside schema structures.
Here's what I'm trying to achieve:
Could you please provide guidance on how to structure my code according to these best practices? Any insights, examples, or recommendations would be greatly appreciated.
// main room
// gameManager
Thank you in advance for your assistance. I'm eager to learn and improve my codebase for better readability and maintainability.
Beta Was this translation helpful? Give feedback.
All reactions