Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-only Schema updates #141

Open
thomaswp opened this issue Oct 2, 2022 · 2 comments
Open

Client-only Schema updates #141

thomaswp opened this issue Oct 2, 2022 · 2 comments

Comments

@thomaswp
Copy link

thomaswp commented Oct 2, 2022

I'm using colyseus.js with a javascript front end. Is there any way to update a Schema on the client side and have those updates propagate to listeners (onChange, onAdd, etc.). I know traditionally updates should come from the server, not client, but I'll explain why below.

For example, is there a way to have this work?

let mySchema = new MySchema(); // extends Schema

mySchema.onChange = changes => {
  // listen client-side for a change, as usual
  console.log(changes);
};

// try to trigger change client-side
// currently does nothing
myScema.foo = 3;

This is somewhat counterintuitive, and while I'm guess it's not a bug per se, I'm hoping there's a way to work around it.

Rationale: My game uses Colyseus for networking, and I've separated my game logic out so that the server can it and the client responds to state update. That works great. However, I'd like to build a client-only single-player version of the game, which doesn't require a server to run. This in theory should be fine. The game logic doesn't depend on anything in node.js, so the client should be able to run it. I've got messages to work just fine, but the problem is that when my game logic updates the state, the client's listeners don't trigger.

I've tried a naive approach, creating a readState (for client updates) and writeState (for game logic mutations, which would normally run on the server). Then I try:

let bytes = this.writeState.encode();
console.log(bytes);
this.readState.decode(bytes);

But this doesn't seem to be recursive, and I'm guessing I've got it wrong. Is there a better workaround. Thanks for any help!

@endel
Copy link
Member

endel commented Oct 3, 2022

Hi @thomaswp, this is a great discussion but I don't know the best way to approach this. I think the answer would be outside of schema.

I think another layer that performs visual changes based on data would be best, for example:

// multiplayer version
player.listen("something", function(value) {
  visualStuff.changePlayerSomething(value);
})

// single-player version
visualStuff.changePlayerSomething(xxx);

The approach to simulate server -> client mutations may work. I'm not sure what you mean by "doesn't seem to be recursive". Maybe you've missed some onAdd callback which is necessary to attach more callbacks to child structures. Happy to help on this approach if you can share any code!

Cheers!

@khoakomlem
Copy link

You should take a look at this:
#36 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants