Objectivity Part 2: Synchronization

We already covered the design of objects locally, but how will the synchronization work between clients? Now that we have the tools to create a network, we can undertake this task.

To achieve synchronization, we will have to support three kinds of object-related messages in our client-server protocol:

First, we need a message that tells all connected clients to create the same object and a message that tells all connected clients about a change to an object that exists and is presumed to be in sync.

Finally, a message that fully copies an object and it’s state from one client to another. This is crucial, because we will need it both to re-synchronize objects if they end up out of sync for any reason (such as a lost message) and to allow a client connecting into a game in progress to catch up on the current state of the game.

To create the last message, we need a good way to define the complete change of state an object has undergone from it’s default state. JavaScript allows us to do this easily by enumerating an object’s properties:

for(var property in objState)
	obj[property] = objState[property]

In this snippet, we assume that the changes to an object is stored in ‘objState’ . We update the properties of ‘obj’ by enumerating any properties of ‘objState’. This means that we can use this to handle any properties, as long as they exist, and that we can handle as few or as many as needed.

A new demo demonstrating object synchronization is available in the repo Examples/Network/Network Objects Example.

Be Sociable, Share!

Published by

Albert Öhrling

Studies computer science in Skellefteå, at Lulea University of Technology.

Leave a Reply

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