This is a third and hoping last part of cycle about using flatbuffers in Unity. Here we take a look at usage example.
There is an example of PingPong game. It is not about fascinating gameplay it is just demonstration of flatbuffers usage. First of all, there are three .fbs-files (BallCoordinates, MoveDirection and PlayerAction) that we need to compile into c# and python (.cs and .py respectively). Do not forget that you can choose a flatc-compiler (although there is default one for each of platforms: Windows, MacOS, Linux).
After compiling you have to move python files into server-part folder. (Scripts->Server->FbsCompilled). It is time to prepare server:
That's it. I have added FbsSerializer for message serializing/deserializing.
At StartGame Unity's client send a coordinates of ball (relative to computer-player's board) and its move direction. On server-side handler tries to calculate where the board should be and sends an action command back to client.
Thank you for your time!
There is an example of PingPong game. It is not about fascinating gameplay it is just demonstration of flatbuffers usage. First of all, there are three .fbs-files (BallCoordinates, MoveDirection and PlayerAction) that we need to compile into c# and python (.cs and .py respectively). Do not forget that you can choose a flatc-compiler (although there is default one for each of platforms: Windows, MacOS, Linux).
After compiling you have to move python files into server-part folder. (Scripts->Server->FbsCompilled). It is time to prepare server:
- Install python and pip
- Optionally you can create python environment:
python -m venv /path/to/new/virtual/environment
- Install requirements from file "requirements.txt" (with the help of pip):
pip install -r requirements.txt
- Run flask server with the help of script run_server.bat. There should be output:
* Serving Flask app "server.py" * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Pay attention to string "Running on http://127.0.0.1:5000/". It is default port and you server will serve only on localhost. In example this string is hardcoded (NetworkPlayer::host).
That's it. I have added FbsSerializer for message serializing/deserializing.
At StartGame Unity's client send a coordinates of ball (relative to computer-player's board) and its move direction. On server-side handler tries to calculate where the board should be and sends an action command back to client.
Thank you for your time!
Comments
Post a Comment