Skip to content

AutoSync

Garrett Luskey edited this page Sep 22, 2024 · 5 revisions

The Autosync framework through immense pain magic (aka Reflection.Emit) will sync data automatically between the server and all clients.

Currently the Autosync framework only provides server controlled functionality.

  • If a auto synced value is changed on the server it will update for all clients
  • If a auto synced value is changed on the client it will not set the value and log an error

Prerequisites

  • Lifetime sync is setup for your declaring class and the type of the field (if it is a class/struct, native types work by default)

Terms

  • Declaring class - The class that contains the field
  • Sync - State is updated to match the controlling entity (the server in our case)

Example Usage

class FiefSync : IAutoSync
{
    public FiefSync(IAutoSyncBuilder autoSyncBuilder)
    {
        // Sync a field in declaring class
        autoSyncBuilder.AddField(AccessTools.Field(typeof(Fief), nameof(Fief.GarrisonPartyComponent)));

        // Sync a field called by an external class/external function call (aka not in this class)
        autoSyncBuilder.AddFieldChangeMethod(AccessTools.Method(typeof(TBD), nameof(Example.ExampleExternalMethod)));

        // Sync a property
        autoSyncBuilder.AddProperty(AccessTools.Property(typeof(Fief), nameof(Fief.FoodStocks)));
    }
}

Testing

Testing properties

Property Test Example

Testing fields

WARNING: Setting the field directly in the test will not trigger a field changed event. To do so get the field set intercept from the environment from the example below

Field Test Example

Clone this wiki locally