@@ -15,10 +15,6 @@ namespace MLAPI.MonoBehaviours.Core
15
15
/// </summary>
16
16
public abstract class NetworkedBehaviour : MonoBehaviour
17
17
{
18
- /// <summary>
19
- /// The minimum delay in seconds between SyncedVar sends
20
- /// </summary>
21
- public float SyncVarSyncDelay = 0.1f ;
22
18
/// <summary>
23
19
/// Gets if the object is the the personal clients player object
24
20
/// </summary>
@@ -471,7 +467,8 @@ internal void SyncVarInit()
471
467
FieldInfo = sortedFields [ i ] ,
472
468
FieldType = fieldType ,
473
469
FieldValue = sortedFields [ i ] . GetValue ( this ) ,
474
- HookMethod = hookMethod
470
+ HookMethod = hookMethod ,
471
+ Attribute = attribute
475
472
} ) ;
476
473
}
477
474
else
@@ -528,34 +525,65 @@ internal void FlushToClient(uint clientId)
528
525
}
529
526
}
530
527
531
- private float lastSyncTime = 0f ;
532
528
internal void SyncVarUpdate ( )
533
529
{
534
530
if ( ! syncVarInit )
535
531
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 ++ )
537
540
{
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
+ }
550
548
551
- if ( totalDirtyCount == 0 )
552
- return ; //All up to date!
549
+ if ( totalDirtyCount == 0 )
550
+ return ; //All up to date!
553
551
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 ( ) )
557
558
{
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.
559
587
using ( BitWriter writer = BitWriter . Get ( ) )
560
588
{
561
589
//Write all indexes
@@ -569,80 +597,48 @@ internal void SyncVarUpdate()
569
597
{
570
598
writer . WriteByte ( i ) ; //FieldIndex
571
599
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 )
599
601
{
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 ;
608
605
}
609
606
}
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 ) ;
613
607
}
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 ) ;
614
611
}
612
+ }
615
613
616
- if ( nonTargetDirtyCount == 0 )
617
- return ;
614
+ if ( nonTargetDirtyCount == 0 )
615
+ return ;
618
616
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 ++ )
621
625
{
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 )
639
628
{
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 ;
642
633
}
643
634
}
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
+ }
644
641
}
645
- lastSyncTime = NetworkingManager . singleton . NetworkTime ;
646
642
}
647
643
}
648
644
@@ -654,13 +650,19 @@ private bool SetDirtyness()
654
650
bool dirty = false ;
655
651
for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
656
652
{
653
+ if ( NetworkingManager . singleton . NetworkTime - syncedVarFields [ i ] . Attribute . lastSyncTime < syncedVarFields [ i ] . Attribute . syncDelay )
654
+ continue ;
657
655
if ( ! syncedVarFields [ i ] . FieldInfo . GetValue ( this ) . Equals ( syncedVarFields [ i ] . FieldValue ) )
658
656
{
659
657
syncedVarFields [ i ] . Dirty = true ; //This fields value is out of sync!
658
+ syncedVarFields [ i ] . Attribute . lastSyncTime = NetworkingManager . singleton . NetworkTime ;
660
659
dirty = true ;
661
660
}
662
661
else
662
+ {
663
+ syncedVarFields [ i ] . Attribute . lastSyncTime = NetworkingManager . singleton . NetworkTime ;
663
664
syncedVarFields [ i ] . Dirty = false ; //Up to date;
665
+ }
664
666
}
665
667
return dirty ;
666
668
}
0 commit comments