Skip to content

Commit c0cc629

Browse files
committed
Updated SyncDelay for SyncedVars to be per var
1 parent dd87f98 commit c0cc629

File tree

3 files changed

+99
-91
lines changed

3 files changed

+99
-91
lines changed

MLAPI/Attributes/SyncedVar.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ public class SyncedVar : Attribute
1111
/// <summary>
1212
/// The method name to invoke when the SyncVar get's updated.
1313
/// </summary>
14-
public string hookMethodName;
14+
public string hookMethodName = string.Empty;
1515
/// <summary>
1616
/// If true, the syncedVar will only be synced to the owner.
1717
/// </summary>
18-
public bool target;
18+
public bool target = false;
19+
/// <summary>
20+
/// The delay for syncing this variable.
21+
/// </summary>
22+
public float syncDelay = 0.1f;
23+
internal float lastSyncTime = 0f;
1924
}
2025
}

MLAPI/Data/SyncedVarField.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Collections.Generic;
2-
using System.Reflection;
1+
using System.Reflection;
2+
using MLAPI.Attributes;
33

44
namespace MLAPI.Data
55
{
@@ -11,5 +11,6 @@ internal class SyncedVarField
1111
internal MethodInfo HookMethod;
1212
internal bool Dirty;
1313
internal bool Target;
14+
internal SyncedVar Attribute;
1415
}
1516
}

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ namespace MLAPI.MonoBehaviours.Core
1515
/// </summary>
1616
public abstract class NetworkedBehaviour : MonoBehaviour
1717
{
18-
/// <summary>
19-
/// The minimum delay in seconds between SyncedVar sends
20-
/// </summary>
21-
public float SyncVarSyncDelay = 0.1f;
2218
/// <summary>
2319
/// Gets if the object is the the personal clients player object
2420
/// </summary>
@@ -471,7 +467,8 @@ internal void SyncVarInit()
471467
FieldInfo = sortedFields[i],
472468
FieldType = fieldType,
473469
FieldValue = sortedFields[i].GetValue(this),
474-
HookMethod = hookMethod
470+
HookMethod = hookMethod,
471+
Attribute = attribute
475472
});
476473
}
477474
else
@@ -528,34 +525,65 @@ internal void FlushToClient(uint clientId)
528525
}
529526
}
530527

531-
private float lastSyncTime = 0f;
532528
internal void SyncVarUpdate()
533529
{
534530
if (!syncVarInit)
535531
SyncVarInit();
536-
if (SyncVarSyncDelay > 0 && NetworkingManager.singleton.NetworkTime - lastSyncTime >= SyncVarSyncDelay && SetDirtyness())
532+
533+
if (!SetDirtyness())
534+
return;
535+
536+
byte nonTargetDirtyCount = 0;
537+
byte totalDirtyCount = 0;
538+
byte dirtyTargets = 0;
539+
for (byte i = 0; i < syncedVarFields.Count; i++)
537540
{
538-
byte nonTargetDirtyCount = 0;
539-
byte totalDirtyCount = 0;
540-
byte dirtyTargets = 0;
541-
for (byte i = 0; i < syncedVarFields.Count; i++)
542-
{
543-
if (syncedVarFields[i].Dirty)
544-
totalDirtyCount++;
545-
if (syncedVarFields[i].Target && syncedVarFields[i].Dirty)
546-
dirtyTargets++;
547-
if (syncedVarFields[i].Dirty && !syncedVarFields[i].Target)
548-
nonTargetDirtyCount++;
549-
}
541+
if (syncedVarFields[i].Dirty)
542+
totalDirtyCount++;
543+
if (syncedVarFields[i].Target && syncedVarFields[i].Dirty)
544+
dirtyTargets++;
545+
if (syncedVarFields[i].Dirty && !syncedVarFields[i].Target)
546+
nonTargetDirtyCount++;
547+
}
550548

551-
if (totalDirtyCount == 0)
552-
return; //All up to date!
549+
if (totalDirtyCount == 0)
550+
return; //All up to date!
553551

554-
// If we don't have targets. We can send one big message,
555-
// thus only serializing it once. Otherwise, we have to create two messages. One for the non targets and one for the target
556-
if (dirtyTargets == 0)
552+
// If we don't have targets. We can send one big message,
553+
// thus only serializing it once. Otherwise, we have to create two messages. One for the non targets and one for the target
554+
if (dirtyTargets == 0)
555+
{
556+
//It's sync time!
557+
using (BitWriter writer = BitWriter.Get())
557558
{
558-
//It's sync time!
559+
//Write all indexes
560+
writer.WriteByte(totalDirtyCount);
561+
writer.WriteUInt(networkId); //NetId
562+
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
563+
for (byte i = 0; i < syncedVarFields.Count; i++)
564+
{
565+
//Writes all the indexes of the dirty syncvars.
566+
if (syncedVarFields[i].Dirty == true)
567+
{
568+
writer.WriteByte(i); //FieldIndex
569+
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
570+
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
571+
syncedVarFields[i].Dirty = false;
572+
}
573+
}
574+
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId);
575+
if (stillDirtyIds != null)
576+
{
577+
for (int i = 0; i < stillDirtyIds.Count; i++)
578+
OutOfSyncClients.Add(stillDirtyIds[i]);
579+
}
580+
}
581+
}
582+
else
583+
{
584+
if (!(isHost && ownerClientId == NetworkingManager.singleton.NetworkConfig.NetworkTransport.HostDummyId))
585+
{
586+
//It's sync time. This is the target receivers packet.
559587
using (BitWriter writer = BitWriter.Get())
560588
{
561589
//Write all indexes
@@ -569,80 +597,48 @@ internal void SyncVarUpdate()
569597
{
570598
writer.WriteByte(i); //FieldIndex
571599
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
572-
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
573-
syncedVarFields[i].Dirty = false;
574-
}
575-
}
576-
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId);
577-
if (stillDirtyIds != null)
578-
{
579-
for (int i = 0; i < stillDirtyIds.Count; i++)
580-
OutOfSyncClients.Add(stillDirtyIds[i]);
581-
}
582-
}
583-
}
584-
else
585-
{
586-
if (!(isHost && ownerClientId == NetworkingManager.singleton.NetworkConfig.NetworkTransport.HostDummyId))
587-
{
588-
//It's sync time. This is the target receivers packet.
589-
using (BitWriter writer = BitWriter.Get())
590-
{
591-
//Write all indexes
592-
writer.WriteByte(totalDirtyCount);
593-
writer.WriteUInt(networkId); //NetId
594-
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
595-
for (byte i = 0; i < syncedVarFields.Count; i++)
596-
{
597-
//Writes all the indexes of the dirty syncvars.
598-
if (syncedVarFields[i].Dirty == true)
600+
if (nonTargetDirtyCount == 0)
599601
{
600-
writer.WriteByte(i); //FieldIndex
601-
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
602-
if (nonTargetDirtyCount == 0)
603-
{
604-
//Only targeted SyncedVars were changed. Thus we need to set them as non dirty here since it wont be done by the next loop.
605-
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
606-
syncedVarFields[i].Dirty = false;
607-
}
602+
//Only targeted SyncedVars were changed. Thus we need to set them as non dirty here since it wont be done by the next loop.
603+
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
604+
syncedVarFields[i].Dirty = false;
608605
}
609606
}
610-
bool observing = !InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId); //Send only to target
611-
if (!observing)
612-
OutOfSyncClients.Add(ownerClientId);
613607
}
608+
bool observing = !InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId); //Send only to target
609+
if (!observing)
610+
OutOfSyncClients.Add(ownerClientId);
614611
}
612+
}
615613

616-
if (nonTargetDirtyCount == 0)
617-
return;
614+
if (nonTargetDirtyCount == 0)
615+
return;
618616

619-
//It's sync time. This is the NON target receivers packet.
620-
using (BitWriter writer = BitWriter.Get())
617+
//It's sync time. This is the NON target receivers packet.
618+
using (BitWriter writer = BitWriter.Get())
619+
{
620+
//Write all indexes
621+
writer.WriteByte(nonTargetDirtyCount);
622+
writer.WriteUInt(networkId); //NetId
623+
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
624+
for (byte i = 0; i < syncedVarFields.Count; i++)
621625
{
622-
//Write all indexes
623-
writer.WriteByte(nonTargetDirtyCount);
624-
writer.WriteUInt(networkId); //NetId
625-
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
626-
for (byte i = 0; i < syncedVarFields.Count; i++)
627-
{
628-
//Writes all the indexes of the dirty syncvars.
629-
if (syncedVarFields[i].Dirty == true && !syncedVarFields[i].Target)
630-
{
631-
writer.WriteByte(i); //FieldIndex
632-
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
633-
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
634-
syncedVarFields[i].Dirty = false;
635-
}
636-
}
637-
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, ownerClientId, networkId, null, null); // Send to everyone except target.
638-
if (stillDirtyIds != null)
626+
//Writes all the indexes of the dirty syncvars.
627+
if (syncedVarFields[i].Dirty == true && !syncedVarFields[i].Target)
639628
{
640-
for (int i = 0; i < stillDirtyIds.Count; i++)
641-
OutOfSyncClients.Add(stillDirtyIds[i]);
629+
writer.WriteByte(i); //FieldIndex
630+
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
631+
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
632+
syncedVarFields[i].Dirty = false;
642633
}
643634
}
635+
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, ownerClientId, networkId, null, null); // Send to everyone except target.
636+
if (stillDirtyIds != null)
637+
{
638+
for (int i = 0; i < stillDirtyIds.Count; i++)
639+
OutOfSyncClients.Add(stillDirtyIds[i]);
640+
}
644641
}
645-
lastSyncTime = NetworkingManager.singleton.NetworkTime;
646642
}
647643
}
648644

@@ -654,13 +650,19 @@ private bool SetDirtyness()
654650
bool dirty = false;
655651
for (int i = 0; i < syncedVarFields.Count; i++)
656652
{
653+
if (NetworkingManager.singleton.NetworkTime - syncedVarFields[i].Attribute.lastSyncTime < syncedVarFields[i].Attribute.syncDelay)
654+
continue;
657655
if (!syncedVarFields[i].FieldInfo.GetValue(this).Equals(syncedVarFields[i].FieldValue))
658656
{
659657
syncedVarFields[i].Dirty = true; //This fields value is out of sync!
658+
syncedVarFields[i].Attribute.lastSyncTime = NetworkingManager.singleton.NetworkTime;
660659
dirty = true;
661660
}
662661
else
662+
{
663+
syncedVarFields[i].Attribute.lastSyncTime = NetworkingManager.singleton.NetworkTime;
663664
syncedVarFields[i].Dirty = false; //Up to date;
665+
}
664666
}
665667
return dirty;
666668
}

0 commit comments

Comments
 (0)