Continuously send unreliable data with HLAPI

It’s me again :slight_smile:

For my project I need an ongoing unreliable data stream 20 times a second.
Since I’m using the HLAPI, I’d like to stay on this level as far as possible.

My idea was to write custom OnSerialize and OnDeserialze functions for a NetworkBehaviour which is collecting data from various other Behaviours and just de/serializing it all the time.
However it didn’t seem to be triggered, so I added a SetDirtyBit hack to the update function.

Now data is being transferred regularly, but the way I do this feels like dirty abuse.

//reduced version of implementation
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System;

[NetworkSettings(channel=1,sendInterval=0.05f)]
public class NetStreamer : NetworkBehaviour
{
    private bool initialized; //inhibit data transfer
  
    void Update()
    {
        if (isServer)
        {
            SetDirtyBit(1); //force serialization
        }
    }

    public override bool OnSerialize(NetworkWriter writer, bool initialState)
    {
        if (initialized)
        {
            //server side
            //serialize all kind of data here
        }
        return true;
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        if (initialized)
        {
            //client side
            //deserialize all kind of data here
        }
    }
}

Again, my usual question:
How is this supposed to be done in a proper way?

Thanks for any hint.

Hi !

The other way is to use a network message and implement your own OnSerialize, OnDeserialize but you will get the same result. So it seems good to set your object to dirty when you want to send a new update.

The question is: why force to send data if they didn’t changes. you have to take care of your bandwidth :wink:

It is motion data which changes very often.
I can live with data loss, as it is being sent regularly.

On client side I’m doing some interpolation. Reliable data can be delayed a lot which is not good for fluid interpolation.

Understood ! I did that with a NetworkMessage and a method to check if my player change enough and a sending rate.
When the player is alive is mostly send data every time, after a moment of inactivity I disconnect the player (if player didn’t move or didn’t an action there is nothing send).