Fish-Net: Networking Evolved
  • Overview
    • What is FishNet?
      • Features
        • Unity Compatibility
        • Performance
          • Benchmark Setup
          • Fish-Networking Vs Mirror
      • Pro, Projects, and Support
      • Business Support
      • Development
        • Changelog
        • Roadmap
      • Branding
      • Legal Restrictions
    • Showcase
      • Upcoming Releases
    • Asset Integrations
      • Fish-Network-Discovery
    • Community Resources
  • Guides
    • Getting Started
      • Installing Fish-Networking
      • Getting Connected
      • Preparing Your Player
      • Moving Your Player Around
      • Spawning and Despawning Items
      • Using SyncVars to Sync Colors
      • Connecting to Remote Devices
      • Beyond the Basics
    • High-Level Overview
      • Fundamentals
      • Networking Models
      • Terminology
        • Server, Client, Host
        • Communicating
        • Miscellaneous
      • Transports
    • Features
      • Server and Client Identification
        • Executing on Server or Client
        • NetworkConnections
      • Networked GameObjects and Scripts
        • NetworkObjects
        • NetworkBehaviour
        • Spawning and Despawning
          • Predicted Spawning
          • Nested NetworkObjects
          • Object Pooling
      • Network State Events
      • Network Communication
        • Remote Procedure Calls
        • SyncTypes
          • Customizing Behavior
          • SyncVar
          • SyncList
          • SyncHashSet
          • SyncDictionary
          • SyncTimer
          • SyncStopwatch
          • Custom SyncType
        • Broadcasts
      • Data Serialization
        • Custom Serializers
          • Interface Serializers
          • Inheritance Serializers
      • Ownership
        • Using Ownership To Read Values
      • Area of Interest (Observer System)
        • Modifying Conditions
        • Custom Conditions
      • Scene Management
        • Scene Events
        • Scene Data
          • SceneLookupData
          • SceneLoadData
          • SceneUnloadData
        • Loading Scenes
          • Automatic Online and Offline Scenes
        • Unloading Scenes
        • Scene Stacking
        • Scene Caching
        • Scene Visibility
        • Persisting NetworkObjects
        • Custom Scene Processors
          • Addressables
      • InstanceFinder
      • Addressables
      • Transports
        • Multipass
      • Prediction
        • What Is Client-Side Prediction
        • Configuring PredictionManager
        • Configuring TimeManager
        • Configuring NetworkObject
        • Offline Rigidbodies
        • Interpolations
        • Creating Code
          • Controlling An Object
          • Non-Controlled Object
          • Understanding ReplicateState
            • Using States In Code
            • Predicting States In Code
          • Advanced Controls
        • Custom Comparers
        • PredictionRigidbody
        • Using NetworkColliders
      • Lag Compensation
        • States
        • Raycast
        • Projectiles
    • Upgrading API
    • Server Hosting
      • Edgegap - Official Partner
        • Getting Started with Edgegap
      • Hathora
        • Getting Started with Hathora
      • Amazon Web Services (AWS)
        • Getting Started with AWS
    • Upgrading To Fish-Networking
    • Troubleshooting
      • Technical Limitations
      • Creating Bug Reports
        • Report Example
      • FAQ
  • FishNet Building Blocks
    • Components
      • Managers
        • NetworkManager
        • TimeManager
        • PredictionManager
        • ServerManager
        • ClientManager
        • SceneManager
        • TransportManager
          • IntermediateLayer
        • StatisticsManager
        • ObserverManager
          • HashGrid
        • RollbackManager (Pro Feature)
      • Prediction
        • Network Collider
          • NetworkCollision
          • NetworkCollision2D
          • NetworkTrigger
          • NetworkTrigger2D
        • OfflineRigidbody
        • PredictedOwner
        • PredictedSpawn
      • Utilities
        • PingDisplay
        • BandwidthDisplay
        • Tick Smoothers
          • NetworkTickSmoother
          • OfflineTickSmoother
          • MonoTickSmoother [Obsolete]
          • DetachableNetworkTickSmoother [Obsolete]
      • PlayerSpawner
      • DefaultScene
      • ServerSpawner
      • Authenticator
      • ColliderRollback
      • NetworkAnimator
      • NetworkBehaviour
      • NetworkTransform
      • NetworkObject
      • NetworkObserver
    • Prefabs
      • NetworkManager
      • NetworkHudCanvas
    • ScriptableObjects
      • ObserverConditions
        • DistanceCondition
        • GridCondition
        • HostOnlyCondition
        • MatchCondition
        • OwnerOnlyCondition
        • SceneCondition
      • SpawnablePrefabs
        • DefaultPrefabObjects
        • SinglePrefabObjects
        • DualPrefabObjects
      • LevelLoggingConfiguration
    • Transports
      • Tugboat
      • Multipass
      • Yak (Pro Feature)
      • Bayou
      • FishyWebRTC
      • FishyUnityTransport
      • FishySteamworks (Steam)
      • FishyEOS (Epic Online Services)
      • FishyFacepunch (Steam)
      • FishyRealtime (Photon)
  • API Documentation
    • API Reference
Powered by GitBook
On this page
  1. Guides
  2. Features
  3. Network Communication
  4. SyncTypes

SyncList

SyncList is an easy way to keep a List collection automatically synchronized over the network.

Using a SyncList is done the same as with a normal List.

Network callbacks on SyncList do have a little more information than SyncVars. Other non-SyncVar SyncTypes also have their own unique callbacks. The example below demonstrates a SyncList callback.

private readonly SyncList<int> _myCollection = new();

private void Awake()
{
    /* Listening to SyncList callbacks are a
    * little different from SyncVars. */
    _myCollection.OnChange += _myCollection_OnChange;
}
private void Update()
{
    //You can modify a synclist as you would any other list.
    _myCollection.Add(10);
    _myCollection.RemoveAt(0);
    //ect.
}

/* Like SyncVars the callback offers an asServer option
 * to indicate if the callback is occurring on the server
 * or the client. As SyncVars do, changes have already been
 * made to the collection before the callback occurs. */
private void _myCollection_OnChange(SyncListOperation op, int index,
    int oldItem, int newItem, bool asServer)
{
    switch (op)
    {
        /* An object was added to the list. Index
        * will be where it was added, which will be the end
        * of the list, while newItem is the value added. */
        case SyncListOperation.Add:
            break;
        /* An object was removed from the list. Index
        * is from where the object was removed. oldItem
        * will contain the removed item. */
        case SyncListOperation.RemoveAt:
            break;
        /* An object was inserted into the list. Index
        * is where the obejct was inserted. newItem
        * contains the item inserted. */
        case SyncListOperation.Insert:
            break;
        /* An object replaced another. Index
        * is where the object was replaced. oldItem
        * is the item that was replaced, while
        * newItem is the item which now has it's place. */
        case SyncListOperation.Set:
            break;
        /* All objects have been cleared. Index, oldValue,
        * and newValue are default. */
        case SyncListOperation.Clear:
            break;
        /* When complete calls all changes have been
        * made to the collection. You may use this
        * to refresh information in relation to
        * the list changes, rather than doing so
        * after every entry change. Like Clear
        * Index, oldItem, and newItem are all default. */
        case SyncListOperation.Complete:
            break;
    }
}

If you are using this SyncType with a container, such as a class, and want to modify values within that container, you must set the value dirty. See the example below.

private class MyClass
{
    public string PlayerName;
    public int Level;
}

private readonly SyncList<MyClass> _players = new SyncList<MyClass>();

//Call dirty on an index after modifying an entries field to force a synchronize. 
[Server] 
private void ModifyPlayer()
{
    _players[0].Level = 10;
    //Dirty the 0 index.
    _players.Dirty(0);
}

Structures cannot have their values modified when they reside within a collection. You must instead create a local variable for the collection index you wish to modify, change values on the local copy, then set the local copy back into the collection

/* . */
[System.Serializable]
private struct MyStruct
{
    public string PlayerName;
    public int Level;
}

private readonly SyncList<MyStruct> _players = new();

[Server] 
private void ModifyPlayer()
{
    MyStruct ms = _players[0];
    ms.Level = 10;
    _players[0] = ms;
}
PreviousSyncVarNextSyncHashSet

Last updated 1 year ago