Skip to content

Network SyncVars

TBN_MapleWheels edited this page Feb 3, 2023 · 3 revisions

Networked Variables

Network variables allow you to create light-weight versions of CVars without any of the file IO and display functionality. Network variables were created with the intention of allowing vanilla Item/ItemComponent extensions/mods. However, they will work without it regardless.

Example:

public class MyMod : IAssemblyPlugin
{
    Item someItem;
    // Assume we assigned something to it.
    
    // This will be synchronized with the server having control.
    // The item ID of someItem will be used to make a unique ID.
    INetVar<float> itemNetVar = Utils.Networking.CreateNetVar<float>(
        someItem, 
        nameof(Item),           // nameof `Barotrauma.Item`
        nameof(itemNetVar), 
        10f, 
        NetworkSync.ServerAuthority);
    
}

Networking Interfaces & Custom Implementations

All Networking functionality in ModdingTK is built on the INetConfigBase interface. You can implement this interface in your own type/class to get the full benefits of networking functions.

Here's an example of a custom Powered (ItemComponent) type:

public class SimpleGenerator : Powered, INetConfigBase
{
    public string ModName => nameof(SimpleGenerator);
    public string Name => $"{this.Item.ID}_{this.Item.components.IndexOf(this)}";
    
    public float ACPhase { get; private set; }  
    public float MaxPowerOut { get ; private set; }
    
    public void Update()
    {
        ACPhase += 10f;
        MaxPowerOut += 1f;
        if (ACPhase > 360f)
        {
            ACPhase -= 360f;
        }
        if (MaxPowerOut > 100f)
        {
            MaxPowerOut -= 100f;        
        }        
        this.TriggerNetEvent(); // causes data sync event start 
    }
    
    // called by NetworkingManager
    public bool WriteNetworkValue(INetWriteMessage msg)
    {
        msg.WriteSingle(ACPhase);
        msg.WriteSingle(MaxPowerOut);
        return true;
    }
    
    // called by NetworkingManager
    public bool ReadNetworkValue(INetReadMessage msg)
    {
        // Read back in the same order it was written above.
        ACPhase = msg.ReadSingle(); 
        MaxPowerOut = msg.ReadSingle();
        return true;
    }
    
    // ---- Copy Pasta Code ---- //
    // General Networking code.
    private System.Action<INetConfigBase> _onNetworkEvent;
    public void SubscribeToNetEvents(System.Action<INetConfigBase> evtHandle) => this._onNetworkEvent += evtHandle;
    public void UnsubscribeFromNetEvents(System.Action<INetConfigBase> evtHandle) => this._onNetworkEvent -= evtHandle;
    public Type NetSyncVarTypeDef => typeof(string);
    public Guid NetId { get; private set; }
    public NetworkSync NetSync { get; private set; }
    
    public void InitializeNetworking(Guid netId, NetworkSync networkSync)
    {
        this.NetSync = networkSync;
        this.NetId = netId;
    }
    
#if CLIENT    
    // --- Client sync control code.    
    public bool IsNetworked => this.NetSync != NetworkSync.NoSync && GameMain.IsMultiplayer;
    public bool NetAuthorityValidate()
    {
        if (!IsNetworked)
            return true;
        if (NetSync is NetworkSync.ServerAuthority && GameMain.Client.HasPermission(ClientPermissions.ManageSettings))
            return true;
        
        return this.NetSync switch
        {
            NetworkSync.NoSync => true,
            NetworkSync.ClientPermissiveDesync => true,
            NetworkSync.TwoWaySync => true,
            _ => false
        };
    }
    
    // Checks to see if you should send a network event, the message is blocked by the server using a similar check.
    public void TriggerNetEvent()
    {
        if (NetSync is NetworkSync.TwoWaySync
            || (NetSync is NetworkSync.ServerAuthority && GameMain.Client.HasPermission(ClientPermissions.ManageSettings)))
        {
            this._onNetworkEvent?.Invoke(this);
        } 
    }
#elif SERVER
    // --- Server sync control code.
    public bool IsNetworked => this.NetSync != NetworkSync.NoSync;
    public bool NetAuthorityValidate() => true;
    
    // Checks to see if you should send a network event, the message is blocked by the server using a similar check.
    public void TriggerNetEvent()
    {
        if (this.NetSync is NetworkSync.TwoWaySync or NetworkSync.ServerAuthority or NetworkSync.ClientPermissiveDesync)
        {
            this._onNetworkEvent?.Invoke(this);
        } 
    }
#endif

}

Clone this wiki locally