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:

  1. Install python and pip 
    • Optionally you can create python environment:
    • python -m venv /path/to/new/virtual/environment
  2. Install requirements from file “requirements.txt” (with the help of pip):
    • pip install -r requirements.txt
  3. 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).

After starting server you will be able to play with it in Play mode. Just click start. You can see that every message from server is logged in Debug console.

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!


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *