AttributesSync Class

Synchronize methods and fields using attributes.

Definition

Namespace: Alteruna.Multiplayer
Assembly: Alteruna (in Alteruna.dll) Version: 2.0.1+a1176e08a0b4a6bfd8fefeddde6163a16d29e5ab
C#
public abstract class AttributesSync : CommunicationBridgeUID
Inheritance
Object    Object    Component    Behaviour    MonoBehaviour    CommunicationBridge    CommunicationBridgeUID    AttributesSync
Derived

Remarks

AttributesSync can be used to easily create a script that synchronizes with other clients. See examples below for help on how to get started. For finer controls over a synchronization, see Synchronizable.

Example

An example of 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#
// By inheriting from AttributesSync, we get access to attributes and methods for controlling synchronization
// of methods and fields between clients in a room.
public class MyAttributesSyncClass : AttributesSync
{
       // By using the SynchronizableField attribute, the field will be synchronized between other clients
       // in the same room automatically.
       [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
{
    private int _messageMethodID;

    private void Start()
    {
        // Get the method's ID once.
        _messageMethodID = GetMethodAttributeId(nameof(Message));
    }

    public void SendMessage()
    {
        // Invoke method by id.
        BroadcastRemoteMethod(_messageMethodID, "Hello, world!");

        // Alternatively, you can invoke using the SynchronizableMethod's name. This method of calling the
        // SynchronizableMethod is less performant as it has to look through all SynchronizableMethod's rather
        // than calling it directly using the ID.
        // BroadcastRemoteMethod(nameof(Message), "Hello, world!");
    }

    // The SynchronizableMethod attribute marks the method as available for invocation from
    // AttributesSync behavior.
    [SynchronizableMethod]
    private void Message(string msg)
    {
        // The message will now be printed to all the clients' log in the room.
        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;

    // The public method that we call in an event or from external scripts.
    public void Play()
    {
        // When no argument is provided, ID defaults to 0.
        // Calls the method on all clients, including the sending client.
        BroadcastRemoteMethod();
    }

    // We define our synced method here.
    // As we only define one in this script, we know this one have index 0.
    [SynchronizableMethod]
    private void PlayRemote()
    {
        AudioSource.Play();
    }
}
Using InvokeRemoteMethod, we can invoke a synced method on one or more target clients. The following example shows how we can send private messages to specified user(s).
C#
public class Message : AttributesSync
{
    public void SendMessageToUser(ushort userID, string message)
    {
        // Calls the function on a different client.
        // As there is only one SynchronizableMethod in this script, the method ID will always be 0.
        // So we will just use id 0 here instead of using the nameof method.
        InvokeRemoteMethod(0, userID, message);
    }

    public void SendMessageToUsers(ushort[] userIDs, string message)
    {
        // Calls method by index.
        InvokeRemoteMethod(0, userIDs, 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)
    {
        // This message will not be printed on the client(s) that has matching id(s).
        Debug.Log(msg);
    }
}

Constructors

Methods

BroadcastRemoteMethod(Int32, Object) Calls method with the SynchronizableMethod attribute on every client including sender with given parameters.
BroadcastRemoteMethod(String, Object) Calls method with the SynchronizableMethod attribute on every 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 were 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 changed fields.
Register
(Overrides CommunicationBridgeUIDRegister)
Serialize Write changes to a ITransportStreamWriter processor.
(Overrides CommunicationBridgeUIDSerialize(ITransportStreamWriter, SerializeInfo))
UncommittedFields Check if there is any uncommitted changes to any fields.
Unserialize Read changes from a ITransportStreamReader processor.
(Overrides CommunicationBridgeUIDUnserialize(ITransportStreamReader, UnserializeInfo))

Fields

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

See Also