docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class NetworkVariable<T>

    A variable that can be synchronized over the network.

    Inheritance
    object
    NetworkVariableBase
    NetworkVariable<T>
    Implements
    IDisposable
    Inherited Members
    NetworkVariableBase.GetBehaviour()
    NetworkVariableBase.Initialize(NetworkBehaviour)
    NetworkVariableBase.SetUpdateTraits(NetworkVariableUpdateTraits)
    NetworkVariableBase.DefaultReadPerm
    NetworkVariableBase.DefaultWritePerm
    NetworkVariableBase.Name
    NetworkVariableBase.ReadPerm
    NetworkVariableBase.WritePerm
    NetworkVariableBase.SetDirty(bool)
    NetworkVariableBase.MarkNetworkBehaviourDirty()
    NetworkVariableBase.CanClientRead(ulong)
    NetworkVariableBase.CanClientWrite(ulong)
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.Netcode
    Assembly: Unity.Netcode.Runtime.dll
    Syntax
    [Serializable]
    [GenerateSerializationForGenericParameter(0)]
    public class NetworkVariable<T> : NetworkVariableBase, IDisposable
    Type Parameters
    Name Description
    T

    the unmanaged type for NetworkVariable<T>

    Constructors

    NetworkVariable(T, NetworkVariableReadPermission, NetworkVariableWritePermission)

    Constructor for NetworkVariable<T>

    Declaration
    public NetworkVariable(T value = default, NetworkVariableReadPermission readPerm = NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission writePerm = NetworkVariableWritePermission.Server)
    Parameters
    Type Name Description
    T value

    initial value set that is of type T

    NetworkVariableReadPermission readPerm

    the NetworkVariableReadPermission for this NetworkVariable<T>

    NetworkVariableWritePermission writePerm

    the NetworkVariableWritePermission for this NetworkVariable<T>

    Fields

    CheckExceedsDirtinessThreshold

    Declaration
    public NetworkVariable<T>.CheckExceedsDirtinessThresholdDelegate CheckExceedsDirtinessThreshold
    Field Value
    Type Description
    NetworkVariable<T>.CheckExceedsDirtinessThresholdDelegate

    OnValueChanged

    The callback to be invoked when the value gets changed

    Declaration
    public NetworkVariable<T>.OnValueChangedDelegate OnValueChanged
    Field Value
    Type Description
    NetworkVariable<T>.OnValueChangedDelegate

    Properties

    Value

    The value of the NetworkVariable container

    Declaration
    public virtual T Value { get; set; }
    Property Value
    Type Description
    T
    Remarks

    When assigning collections to Value, unless it is a completely new collection this will not detect any deltas with most managed collection classes since assignment of one collection value to another is actually just a reference to the collection itself.
    To detect deltas in a collection, you should invoke CheckDirtyState(bool) after making modifications to the collection.

    Methods

    CheckDirtyState(bool)

    Invoke this method to check if a collection's items are dirty. The default behavior is to exit early if the NetworkVariable<T> is already dirty.

    Declaration
    public bool CheckDirtyState(bool forceCheck = false)
    Parameters
    Type Name Description
    bool forceCheck

    when true, this check will force a full item collection check even if the NetworkVariable is already dirty

    Returns
    Type Description
    bool
    Remarks

    This is to be used as a way to check if a NetworkVariable<T> containing a managed collection has any changees to the collection items.
    If you invoked this when a collection is dirty, it will not trigger the OnValueChanged unless you set to true.

    Dispose()

    Virtual IDisposable implementation

    Declaration
    public override void Dispose()
    Overrides
    NetworkVariableBase.Dispose()

    ExceedsDirtinessThreshold()

    Check whether or not this variable has changed significantly enough to send an update. If not, no update will be sent even if the variable is dirty, unless the time since last update exceeds the UpdateTraits' MaxSecondsBetweenUpdates.

    Declaration
    public override bool ExceedsDirtinessThreshold()
    Returns
    Type Description
    bool
    Overrides
    NetworkVariableBase.ExceedsDirtinessThreshold()

    ~NetworkVariable()

    Declaration
    protected ~NetworkVariable()

    IsDirty()

    Gets Whether or not the container is dirty

    Declaration
    public override bool IsDirty()
    Returns
    Type Description
    bool

    Whether or not the container is dirty

    Overrides
    NetworkVariableBase.IsDirty()

    OnInitialize()

    Called on initialization

    Declaration
    public override void OnInitialize()
    Overrides
    NetworkVariableBase.OnInitialize()

    ReadDelta(FastBufferReader, bool)

    Reads value from the reader and applies it

    Declaration
    public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
    Parameters
    Type Name Description
    FastBufferReader reader

    The stream to read the value from

    bool keepDirtyDelta

    Whether or not the container should keep the dirty delta, or mark the delta as consumed

    Overrides
    NetworkVariableBase.ReadDelta(FastBufferReader, bool)

    ReadField(FastBufferReader)

    Reads the complete state from the reader and applies it

    Declaration
    public override void ReadField(FastBufferReader reader)
    Parameters
    Type Name Description
    FastBufferReader reader

    The stream to read the state from

    Overrides
    NetworkVariableBase.ReadField(FastBufferReader)

    Reset(T)

    Resets the NetworkVariable when the associated NetworkObject is not spawned

    Declaration
    public void Reset(T value = default)
    Parameters
    Type Name Description
    T value

    the value to reset the NetworkVariable to (if none specified it resets to the default)

    ResetDirty()

    Resets the dirty state and marks the variable as synced / clean

    Declaration
    public override void ResetDirty()
    Overrides
    NetworkVariableBase.ResetDirty()

    WriteDelta(FastBufferWriter)

    Writes the variable to the writer

    Declaration
    public override void WriteDelta(FastBufferWriter writer)
    Parameters
    Type Name Description
    FastBufferWriter writer

    The stream to write the value to

    Overrides
    NetworkVariableBase.WriteDelta(FastBufferWriter)

    WriteField(FastBufferWriter)

    Writes the complete state of the variable to the writer

    Declaration
    public override void WriteField(FastBufferWriter writer)
    Parameters
    Type Name Description
    FastBufferWriter writer

    The stream to write the state to

    Overrides
    NetworkVariableBase.WriteField(FastBufferWriter)

    Implements

    IDisposable
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)