boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
10.02k stars 708 forks source link

Document requirement that G must be a JSON-serializable object #741

Closed rwhogg closed 4 years ago

rwhogg commented 4 years ago

Would it be helpful to document the requirement that G must always be a JSON-serializable object? I found https://github.com/nicolodavis/boardgame.io/issues/533#issuecomment-569276056 after realizing that my game behaved differently depending on whether or not it was using a server; with purely client-side usage it handles instance methods correctly but with server-enabled multiplayer they are not defined.

In retrospect, the reason for this restriction was obvious, but it may not be immediately apparent when you're just starting out.

delucis commented 4 years ago

Sure! Would you like to open a PR? Maybe we could a note to the State section here?

vdfdev commented 4 years ago

Can we add some sort of automatic check? Even if it is meant to be run as part of the test suite? Basically encodify and decodify the state and see if it is still the same? This would prevent several bugs

delucis commented 4 years ago

@flamecoals We could add some tests for this. Are there any areas where you’ve seen this produce bugs?

I’m not sure we should add a step of stringify, parse, deep compare to the actual engine, although I guess at some point some kind of tooling behind a development flag could run these checks if enough people are running into this issue.

rwhogg commented 4 years ago

Thanks, submitted #743.

I thought about the automated checking idea at the framework level when I was creating this issue too. I'm personally fine without it; I can just write my own tests for this sort of thing. I don't know how many people besides me have experienced this sort of issue though.

vdfdev commented 4 years ago

Yes, We had a bunch of bugs in FBG caused by developers using classes to represent the state that are not serializable. The tricky thing is that sometimes while testing locally it works fine, but when playing an online game it will fail somewhat silently. So I like your idea of adding this check at least if the debug mode is on or something... I might send a PR later

delucis commented 4 years ago

I’ve debugged instances of people using classes in G at least twice in the Gitter chat and actually when I first started using the framework I also had the instinct to use classes for decks etc. so it does seem to be a relatively common problem. Thanks for adding this to the docs :+1: