Skip to content

Commit 19d048e

Browse files
committed
feat: Added send rate to SyncedVar
1 parent 8e11b4b commit 19d048e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

MLAPI/NetworkedVar/SyncedVarAttribute.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ public class SyncedVarAttribute : Attribute
1212
/// The channel to send changes on.
1313
/// </summary>
1414
public string Channel = "MLAPI_DEFAULT_MESSAGE";
15+
16+
/// <summary>
17+
/// The maximum times per second this var will be synced.
18+
/// A value of 0 will cause the variable to sync as soon as possible after being changed.
19+
/// A value of less than 0 will cause the variable to sync only at once at spawn and not update again.
20+
/// </summary>
21+
public float SendTickrate = 0;
1522
}
1623
}

MLAPI/NetworkedVar/SyncedVarContainer.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@ internal class SyncedVarContainer
1111
internal object fieldInstance;
1212
internal object value;
1313
internal bool isDirty;
14+
internal float lastSyncedTime;
1415

1516
internal bool IsDirty()
1617
{
17-
object newValue = field.GetValue(fieldInstance);
18-
object oldValue = value;
19-
20-
if (newValue != oldValue || isDirty)
18+
if (attribute.SendTickrate >= 0 && (attribute.SendTickrate == 0 || NetworkingManager.Singleton.NetworkTime - lastSyncedTime >= (1f / attribute.SendTickrate)))
2119
{
22-
isDirty = true;
20+
lastSyncedTime = NetworkingManager.Singleton.NetworkTime;
21+
22+
object newValue = field.GetValue(fieldInstance);
23+
object oldValue = value;
2324

24-
value = newValue;
25+
if (newValue != oldValue || isDirty)
26+
{
27+
isDirty = true;
2528

26-
return true;
29+
value = newValue;
30+
31+
return true;
32+
}
2733
}
2834

2935
return false;

0 commit comments

Comments
 (0)