AttributesSync Class

Synchronize methods and fields using attributes.

Definition

Namespace: Alteruna
Assembly: Alteruna.Trinity (in Alteruna.Trinity.dll) Version: 1.3.4
C#
public class AttributesSync : CommunicationBridgeUID
Inheritance
Object    Object    Component    Behaviour    MonoBehaviour    CommunicationBridge    CommunicationBridgeUID    AttributesSync
Derived

Example

Here is how you can easily synchronize a field using attributes. There is a small performance overhead on using attributes instead of writing your own synchronizable.
C#
public class MyAttributesSyncClass : AttributesSync
{
       [SynchronizableField]
       public string MyString;
}
Unlike SynchronizableField, SynchronizableMethod have almost no overhead. They can be invoked in several ways. Parameters are easy to add, simply add them after the invocation method. Similar to how they are added in normal invocation.
C#
public class MessageAll : AttributesSync
{
    public void SendRpc()
    {
        // Invoke method by name. alternatively, we can call by index.
        BroadcastRemoteMethod(nameof(Message), "Hello, world!");
    }

    // the SynchronizableMethod attribute marks methods available for remote invocation.
    [SynchronizableMethod]
    private void Message(string msg)
    {
        Debug.Log(msg);
    }
}
Note that only serializable objects can be passed as argument. Here is a simple example for syncing audio play using BroadcastRemoteMethod.
C#
public class PlayAudioSync : AttributesSync
{
    // reference to AudioSource.
    public AudioSource AudioSource;

    // public method that we call in event or from external scripts
    public void Play()
    {
        // Invoke method with index 0 on all clients including sender.
        BroadcastRemoteMethod();
    }

    // We define our synced method here.
    // As we only define one we know this one have index 0.
    [SynchronizableMethod]
    private void PlayRemote()
    {
        AudioSource.Play();
    }
}
Using InvokeRemoteMethod, we can invoke RPC on one or more targets. The following example shows how we can send private messages to specified user(s).
C#
public class Message : AttributesSync
{
    // id is index of user
    public void SendMessageToUser(ushort id, string message)
    {
        // Invoke method by index.
        InvokeRemoteMethod(0, id, message);
    }

    public void SendMessageToUser(ushort[] ids, string message)
    {
        // Invoke method by index.
        InvokeRemoteMethod(0, ids, message);
    }

    // Because this is the first method defined, we know its index is 0. The next one would have index of 1.
    [SynchronizableMethod]
    private void Message(string msg)
    {
        Debug.Log(msg);
    }
}

Methods

BroadcastRemoteMethod(Int32, Object) Calls method with the SynchronizableMethod attribute on evey client including sender with given parameters.
BroadcastRemoteMethod(String, Object) Calls method with the SynchronizableMethod attribute on evey client including sender with given parameters. with given parameters.
Commit Send all changes to all users.
ForceSync Force all fields to be synced as if they where changed.
GetMethodAttributeId Get index of method with the SynchronizableMethod attribute by name.
GetMethodAttributeName Get name of method with the SynchronizableMethod attribute by index.
InvokeRemoteMethod(Int32, UserId, Object) Invoke a method with the SynchronizableMethod attribute on target user with given parameters.
InvokeRemoteMethod(Int32, ListUInt16, Object) Invoke a method with the SynchronizableMethod attribute on target users with given parameters.
InvokeRemoteMethod(Int32, UInt16, Object) Invoke a method with the SynchronizableMethod attribute on target user with given parameters.
InvokeRemoteMethod(String, UserId, Object) Invoke a method with the SynchronizableMethod attribute on target user with given parameters.
InvokeRemoteMethod(String, ListUInt16, Object) Invoke a method with the SynchronizableMethod attribute on target users with given parameters.
InvokeRemoteMethod(String, UInt16, Object) Invoke a method with the SynchronizableMethod attribute on target user with given parameters.
LateUpdate Handle changes fields.
Register
(Overrides CommunicationBridgeUIDRegister)
Serialize Write changes to a ITransportStreamWriter processor.
(Overrides CommunicationBridgeUIDSerialize(ITransportStreamWriter, Byte, Boolean))
UncommittedFields Check if there is any uncommitted changes to any fields.
Unserialize Read changes from a ITransportStreamReader processor.
(Overrides CommunicationBridgeUIDUnserialize(ITransportStreamReader, Byte, UInt32))

Fields

LocalMethodBehavior Chose how local methods behave when sending.
Reliability Chose how to send data. Reliable or Unreliable.

See Also