Skip to content

Getting started

JanFokke edited this page Aug 12, 2020 · 4 revisions

Step 1 Installation

Via Git

Add this repository as a submodule:

git submodule add https://github.com/janfokke/MonoSync.git

git submodule update --init

add a reference to MonoSync.csproj

Via Nuget

Install-Package MonoSync

Step 2 (Optional but recommended)

Install Fody/PropertyChanged

MonoSync tracks changed, using the INotifyPropertyChanged.PropertyChanged event. And Fody/PropertyChanged automatically injects code which raises the PropertyChanged event, into property setters of classes which implement INotifyPropertyChanged.

Step 3

Add Attributes to your Entities/GameWorld

[Synchronizable]
[AddINotifyPropertyChangedInterface]
internal class TestGameWorld
{
    [Synchronize]
    public ObservableDictionary<string, TestPlayer> Players { get; set; } =
        new ObservableDictionary<string, TestPlayer>();

    [Synchronize] 
    public int RandomIntProperty { get; set; }
}

[Synchronizable]
[AddINotifyPropertyChangedInterface]
internal class TestPlayer
{
    [Synchronize]
    public int Level { get; set; }

    [Synchronize]
    public int Health { get; set; }
}

Step 4

Create a SyncSourceRoot Server-side

var sourceGameWorld = new TestGameWorld();
var sourceSynchronizerRoot = new SourceSynchronizerRoot(sourceGameWorld );

Step 5

Initial Synchronization to new Clients

using (var writeSession = sourceSynchronizerRoot.BeginWrite())
{
    byte[] fullSynchronization = writeSession.WriteFull();

    foreach(Client client in _newClients)
    {
        client.SendWorldData(fullSynchronization);
    }

    // Sync existing clients here (See step 10)
    // ...
    // ...
}

Step 6

Receive initial world data Client-Side and create a new SyncTargetRoot

var targetSynchronizerRoot = new TargetSynchronizerRoot<TestGameWorld>(fullSynchronization);

Step 7

Write changes Server-Side

using (var writeSession = sourceSynchronizerRoot.BeginWrite())
{
    // Sync new clients here (See step 5)
    // ...
    // ...

    SynchronizationPacket changesWithoutTick = writeSession.WriteChanges();
    foreach (var client in clients)
    {
         byte[] changesSynchronization = changesWithoutTick.SetTick(client.Tick);
         client.SendWorldData(changesSynchronization);
    }
}

Step 8

Read changes client-side

targetSynchronizerRoot.Read(changesSynchronization);

Example project: https://github.com/janfokke/MonoSync/tree/master/samples/Tweening

Clone this wiki locally