In the last part of the pong series, you'll be building a fully synchronized point scoring system. Ready to dive in?
Now, after part II of this tutorial, you’re able to run your pong game online with each player controlling their own pad. However, you still need to take care of the point scoring system so that the results of each player are always synchronized and free of errors.
Each player has a separate variable storing the number of the scored points as well as a dedicated event that is called upon the change of this score value. This event is used by classes controlling e.g. the UI shown to the players, so that they can update their internal state when needed.
You still need to allow external classes to access the underlying values, so let's modify the variables:
The resulting modified implementation should look like this:
From now on, score counting will be fully synchronized! 🎉 After joining the match, players will automatically get their current score values.
Controlling the match by the server
Your points are now synchronized over the network, but the overall control over the game is executed by each player separately. Purely hypothetically, a ball in one player’s simulation could hit the score area before its position is corrected (reconciled) by the server, and the score value could be updated to a game-ending result before the server is able to correct that. This scenario could possibly lead to one player displaying an end game screen while the actual game is still being played on the server and the other client.
In such cases, it’s best to move the entire match state synchronization to the server and inform the players (in the RPC-style) when the game has been finished.
Thanks to this implementation, the order of the events will look as follows:
Now, you can rest assured that the control of the match flow will only be controlled by the server. After the changes described in this part, the whole class should look like this:
That's all! 🎉 Your pong is now fully ready for online, server authoritative gameplay! Great job! 💪
If you have some additional questions, join our community where Elympics developers will help you with any technical issue.