Attempt to improve time

This commit is contained in:
2026-01-01 21:49:12 +01:00
parent 69a0a752be
commit 26625f6b6f
12 changed files with 2302 additions and 1913 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -88,7 +88,7 @@ namespace Marro.PacManUdon
StartAttractMode();
}
public void FixedUpdate()
public override void FixedUpdate()
{
TimeSequenceUpdate(Time.deltaTime);
}
@@ -422,7 +422,7 @@ namespace Marro.PacManUdon
ghostManager.SetOwner(Networking.LocalPlayer);
}
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType, uint eventTime)
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
{
data[offset++] = new byte[] { Int32ToByte((int)gameState) };
data[offset++] = BitConverter.GetBytes(currentlyInTimeSequence);
@@ -430,7 +430,7 @@ namespace Marro.PacManUdon
data[offset++] = BitConverter.GetBytes(timeSequenceProgress);
}
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType, uint eventTime)
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
{
SetGameState((PacManGameState)data[offset++]);
@@ -443,11 +443,6 @@ namespace Marro.PacManUdon
return true;
}
public override void SyncedToNewTime(uint oldTime, uint newTime)
{
}
public int ExtraLives
{
set

File diff suppressed because it is too large Load Diff

View File

@@ -150,7 +150,7 @@ namespace Marro.PacManUdon
// Debug.Log($"{gameObject} reset with state: {state}, target: {target}, offGrid: {offGrid}");
}
void FixedUpdate()
public override void FixedUpdate()
{
if (ghostType == PacManGhostType.Blinky)
{
@@ -779,11 +779,6 @@ namespace Marro.PacManUdon
UpdateAnimator();
}
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType, uint eventTime)
{
base.AppendSyncedData(data, ref offset, eventType, eventTime);
}
void OnTriggerEnter(Collider other)
{
if (Networking.IsOwner(gameObject) && other.gameObject.GetComponent<PacManGhostCollider>())

View File

@@ -32,7 +32,7 @@ namespace Marro.PacManUdon
protected abstract void UpdateAnimator();
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType, uint eventTime)
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
{
var position = GetPosition();
data[offset++] = BitConverter.GetBytes(position.x);
@@ -43,7 +43,7 @@ namespace Marro.PacManUdon
data[offset++] = BitConverter.GetBytes(direction.y);
}
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType, uint eventTime)
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
{
SetPosition(new Vector2(BitConverter.ToSingle(data, offset), BitConverter.ToSingle(data, offset + 2)));
SetDirection(new Vector2(BitConverter.ToSingle(data, offset + 4), BitConverter.ToSingle(data, offset + 6)));
@@ -51,10 +51,5 @@ namespace Marro.PacManUdon
return true;
}
public override void SyncedToNewTime(uint oldTime, uint newTime)
{
}
}
}

View File

@@ -43,7 +43,7 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 10
Data: 11
- Name:
Entry: 7
Data:
@@ -331,25 +331,19 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: nextEventTime
Data: nextEventTimeTicks
- Name: $v
Entry: 7
Data: 19|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: nextEventTime
Data: nextEventTimeTicks
- Name: <UserType>k__BackingField
Entry: 7
Data: 20|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.UInt32, mscorlib
- Name:
Entry: 8
Data:
Entry: 9
Data: 17
- Name: <SystemType>k__BackingField
Entry: 9
Data: 20
Data: 17
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -364,7 +358,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 21|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 20|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -388,13 +382,13 @@ MonoBehaviour:
Data: retriesWithoutSuccess
- Name: $v
Entry: 7
Data: 22|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 21|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: retriesWithoutSuccess
- Name: <UserType>k__BackingField
Entry: 7
Data: 23|System.RuntimeType, mscorlib
Data: 22|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Int32, mscorlib
@@ -403,7 +397,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 23
Data: 22
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -418,7 +412,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 24|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 23|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -442,13 +436,13 @@ MonoBehaviour:
Data: buffer
- Name: $v
Entry: 7
Data: 25|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 24|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: buffer
- Name: <UserType>k__BackingField
Entry: 7
Data: 26|System.RuntimeType, mscorlib
Data: 25|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Byte[], mscorlib
@@ -457,7 +451,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
Data: 25
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -472,7 +466,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 27|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 26|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -496,16 +490,16 @@ MonoBehaviour:
Data: index
- Name: $v
Entry: 7
Data: 28|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 27|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: index
- Name: <UserType>k__BackingField
Entry: 9
Data: 23
Data: 22
- Name: <SystemType>k__BackingField
Entry: 9
Data: 23
Data: 22
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -520,7 +514,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 29|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 28|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -544,16 +538,16 @@ MonoBehaviour:
Data: networkedData
- Name: $v
Entry: 7
Data: 30|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 29|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: networkedData
- Name: <UserType>k__BackingField
Entry: 9
Data: 26
Data: 25
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
Data: 25
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -568,13 +562,13 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 31|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 30|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 32|UdonSharp.UdonSyncedAttribute, UdonSharp.Runtime
Data: 31|UdonSharp.UdonSyncedAttribute, UdonSharp.Runtime
- Name:
Entry: 8
Data:
@@ -590,6 +584,54 @@ MonoBehaviour:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: <CurrentTimeTicks>k__BackingField
- Name: $v
Entry: 7
Data: 32|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: <CurrentTimeTicks>k__BackingField
- Name: <UserType>k__BackingField
Entry: 9
Data: 17
- Name: <SystemType>k__BackingField
Entry: 9
Data: 17
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 33|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:

View File

@@ -1,4 +1,6 @@
using System;
using Cysharp.Threading.Tasks.Triggers;
using JetBrains.Annotations;
using System;
using System.Drawing;
using System.Text;
using UdonSharp;
@@ -46,8 +48,7 @@ namespace Marro.PacManUdon
private bool isSynced;
private long startTimeTicks = DateTime.UtcNow.Ticks; // Initialize to prevent errors
private uint nextEventTime;
private long nextEventTimeTicks;
private int retriesWithoutSuccess;
@@ -64,6 +65,8 @@ namespace Marro.PacManUdon
[UdonSynced] private byte[] networkedData = new byte[0];
public long CurrentTimeTicks { get; private set; }
public void Initialize()
{
if (!BitConverter.IsLittleEndian)
@@ -90,6 +93,8 @@ namespace Marro.PacManUdon
{
ProgressReplayTime();
}
UpdateTime(DateTime.UtcNow.Ticks);
}
public void SendEvent(NetworkEventType eventType)
@@ -100,13 +105,13 @@ namespace Marro.PacManUdon
return;
}
var eventTime = CurrentTime;
var eventTime = GetTimestamp(CurrentTimeTicks);
InitializeEvent(eventType, eventTime, BufferMaxSizeBytes, out byte[][] data, out var index);
foreach (var obj in syncedObjects)
{
obj.AppendSyncedData(data, ref index, eventType, eventTime);
obj.AppendSyncedData(data, ref index, eventType);
}
// Get event size, skipping over the event size which is not yet included
@@ -210,11 +215,11 @@ namespace Marro.PacManUdon
this.index = size;
// Sync up to the time in the full sync
var time = BitConverter.ToUInt32(networkedData, index + HeaderTimestampIndex);
SyncToTime(time);
var timestamp = BitConverter.ToUInt32(networkedData, index + HeaderTimestampIndex);
SyncToTimestamp(timestamp);
// Immediately apply the full sync
nextEventTime = time;
nextEventTimeTicks = GetTimeTicks(timestamp);
isSynced = true;
}
@@ -233,8 +238,7 @@ namespace Marro.PacManUdon
private void ProgressReplayTime()
{
var currentTime = CurrentTime;
while (index != 0 && nextEventTime <= currentTime)
while (index != 0 && nextEventTimeTicks <= CurrentTimeTicks)
{
ProcessIncomingEvent();
UpdateNextEventTime();
@@ -248,10 +252,10 @@ namespace Marro.PacManUdon
return;
}
var nextEventTime = BitConverter.ToUInt32(buffer, HeaderTimestampIndex);
if (nextEventTime >= this.nextEventTime)
var nextEventTimeTicks = GetTimeTicks(BitConverter.ToUInt32(buffer, HeaderTimestampIndex));
if (nextEventTimeTicks >= this.nextEventTimeTicks)
{
this.nextEventTime = nextEventTime;
this.nextEventTimeTicks = nextEventTimeTicks;
}
else
{
@@ -263,31 +267,31 @@ namespace Marro.PacManUdon
private void ProcessIncomingEvent()
{
var eventTime = BitConverter.ToUInt32(buffer, HeaderTimestampIndex);
var eventTimeTicks = GetTimeTicks(BitConverter.ToUInt32(buffer, HeaderTimestampIndex));
var eventType = (NetworkEventType)buffer[HeaderEventTypeIndex];
var index = (int)HeaderLength; // Skip header
if (false) // TODO: check event type
UpdateTime(eventTimeTicks);
foreach (var obj in syncedObjects)
{
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Invalid event type for incoming data! Buffer will be cleared.");
HandleError();
return;
obj.FixedUpdate();
}
foreach (var obj in syncedObjects)
{
var success = obj.SetSyncedData(buffer, ref index, eventType, eventTime);
var success = obj.SetSyncedData(buffer, ref index, eventType);
if (!success)
{
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Malformed data reported by {obj.name} during event type {eventType}! Resetting state.");
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Malformed data reported by {obj.name} during event type {eventType}!");
HandleError();
return;
}
if (index > this.index)
{
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Buffer overflow during SetSyncedData for {obj.name} in event type {eventType}! Resetting state.");
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Buffer overflow during {nameof(SyncedObject.SetSyncedData)} for {obj.name} in event type {eventType}!");
HandleError();
return;
}
@@ -308,10 +312,11 @@ namespace Marro.PacManUdon
retriesWithoutSuccess = 0; // We had success!
}
private void SyncToTime(uint newTime)
private void SyncToTimestamp(uint newTime)
{
var timeToSyncTo = newTime - Delay;
startTimeTicks = DateTime.UtcNow.Ticks - timeToSyncTo * TimeSpan.TicksPerMillisecond;
Debug.Log($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Synced to time {newTime}, startTimeTicks is now {startTimeTicks}");
}
@@ -483,12 +488,26 @@ namespace Marro.PacManUdon
}
}
public float GetDtInSeconds(uint currentTime, uint lastUpdate)
private void UpdateTime(long timeTicks)
{
return (CurrentTime - lastUpdate) / 1000f;
CurrentTimeTicks = timeTicks;
foreach (var obj in syncedObjects)
{
obj.Dt = (timeTicks - obj.LastUpdateTicks) / (float)TimeSpan.TicksPerSecond;
obj.LastUpdateTicks = timeTicks;
}
}
public uint CurrentTime => (uint)((DateTime.UtcNow.Ticks - startTimeTicks) / TimeSpan.TicksPerMillisecond);
public uint GetTimestamp(long timeTicks)
{
return (uint)((timeTicks - startTimeTicks) / TimeSpan.TicksPerMillisecond);
}
public long GetTimeTicks(uint timeStamp)
{
return timeStamp * TimeSpan.TicksPerMillisecond + startTimeTicks;
}
public bool IsOwner => isOwner;

File diff suppressed because it is too large Load Diff

View File

@@ -75,7 +75,7 @@
animator.SetTrigger("Reset");
}
void FixedUpdate()
public override void FixedUpdate()
{
// gameStateManager.statusDisplay.SetDebugText(1, this.targetDirection.ToString());

View File

@@ -6,8 +6,10 @@ namespace Marro.PacManUdon
{
public abstract class SyncedObject : UdonSharpBehaviour
{
public abstract void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType, uint eventTime);
public abstract bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType, uint eventTime);
public abstract void SyncedToNewTime(uint oldTime, uint newTime);
public long LastUpdateTicks { get; set; }
public float Dt { get; set; }
public abstract void FixedUpdate();
public abstract void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType);
public abstract bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType);
}
}

View File

@@ -43,7 +43,115 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 7
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: <LastUpdateTicks>k__BackingField
- Name: $v
Entry: 7
Data: 2|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: <LastUpdateTicks>k__BackingField
- Name: <UserType>k__BackingField
Entry: 7
Data: 3|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Int64, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 3
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 4|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: <Dt>k__BackingField
- Name: $v
Entry: 7
Data: 5|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: <Dt>k__BackingField
- Name: <UserType>k__BackingField
Entry: 7
Data: 6|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Single, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 6
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 7|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
@@ -52,13 +160,13 @@ MonoBehaviour:
Data: networkManager
- Name: $v
Entry: 7
Data: 2|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 8|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: networkManager
- Name: <UserType>k__BackingField
Entry: 7
Data: 3|System.RuntimeType, mscorlib
Data: 9|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: Marro.PacManUdon.NetworkManager, Assembly-CSharp
@@ -67,7 +175,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 7
Data: 4|System.RuntimeType, mscorlib
Data: 10|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: VRC.Udon.UdonBehaviour, VRC.Udon
@@ -88,13 +196,13 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 5|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 11|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 6|UnityEngine.SerializeField, UnityEngine.CoreModule
Data: 12|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -118,13 +226,13 @@ MonoBehaviour:
Data: start
- Name: $v
Entry: 7
Data: 7|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 13|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: start
- Name: <UserType>k__BackingField
Entry: 7
Data: 8|System.RuntimeType, mscorlib
Data: 14|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: UnityEngine.Transform, UnityEngine.CoreModule
@@ -133,7 +241,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 8
Data: 14
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -148,13 +256,13 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 9|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 15|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 10|UnityEngine.SerializeField, UnityEngine.CoreModule
Data: 16|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -178,16 +286,16 @@ MonoBehaviour:
Data: end
- Name: $v
Entry: 7
Data: 11|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 17|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: end
- Name: <UserType>k__BackingField
Entry: 9
Data: 8
Data: 14
- Name: <SystemType>k__BackingField
Entry: 9
Data: 8
Data: 14
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -202,13 +310,13 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 12|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 18|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 13|UnityEngine.SerializeField, UnityEngine.CoreModule
Data: 19|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -232,22 +340,16 @@ MonoBehaviour:
Data: amountUp
- Name: $v
Entry: 7
Data: 14|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 20|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: amountUp
- Name: <UserType>k__BackingField
Entry: 7
Data: 15|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Single, mscorlib
- Name:
Entry: 8
Data:
Entry: 9
Data: 6
- Name: <SystemType>k__BackingField
Entry: 9
Data: 15
Data: 6
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -262,7 +364,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 16|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 21|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -286,13 +388,13 @@ MonoBehaviour:
Data: loopOffset
- Name: $v
Entry: 7
Data: 17|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 22|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: loopOffset
- Name: <UserType>k__BackingField
Entry: 7
Data: 18|System.RuntimeType, mscorlib
Data: 23|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Int32, mscorlib
@@ -301,7 +403,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 18
Data: 23
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -316,61 +418,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 19|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: lastUpdate
- Name: $v
Entry: 7
Data: 20|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: lastUpdate
- Name: <UserType>k__BackingField
Entry: 7
Data: 21|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.UInt32, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 21
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 22|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 24|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0

View File

@@ -17,91 +17,74 @@ public class TestBall : SyncedObject
private float amountUp = 0;
private int loopOffset = 0;
private uint lastUpdate;
private void Start()
{
networkManager.Initialize();
lastUpdate = networkManager.CurrentTime;
}
public void FixedUpdate()
public override void FixedUpdate()
{
var currentTime = networkManager.CurrentTime;
float dt = networkManager.GetDtInSeconds(currentTime, lastUpdate);
DeltaUp(-DownPerSecond * dt);
DeltaUp(-DownPerSecond * Dt);
float progress = GetProgress(currentTime);
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;
lastUpdate = currentTime;
float progress = GetProgress();
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;;
}
private void DeltaUp(float delta)
{
amountUp = Mathf.Clamp(amountUp + delta, 0, 1);
//Debug.Log($"delta: {delta}, amountUp: {amountUp}");
}
private void SetProgress(float progress, uint currentTime)
private void SetProgress(float progress)
{
//loopOffset = (int)(progress * LoopTimeMs % (LoopTimeMs + currentTime));
loopOffset = (int)(currentTime - progress * LoopTimeMs);
//loopOffset = (int)currentTime % LoopTimeMs + (int)(progress * LoopTimeMs);
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
loopOffset = (int)(currentTimestamp - progress * LoopTimeMs);
}
private float GetProgress(uint currentTime)
private float GetProgress()
{
return ((int)currentTime - loopOffset) % LoopTimeMs / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
//return loopOffset - ((int)currentTime % LoopTimeMs) / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
return ((int)currentTimestamp - loopOffset) % LoopTimeMs / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
}
public void UpButtonPressed()
{
DeltaUp(UpPerPress);
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress()} to {amountUp}.");
networkManager.SendEvent((NetworkEventType)1);
}
public void SyncButtonPressed()
{
networkManager.SendEvent((NetworkEventType)0);
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress(networkManager.CurrentTime)} and amountUp {amountUp}.");
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress()} and amountUp {amountUp}.");
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType, uint eventTime)
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
{
var currentTime = networkManager.CurrentTime;
if (eventType == 0)
{
data[index++] = BitConverter.GetBytes(amountUp);
data[index++] = BitConverter.GetBytes(GetProgress(currentTime));
data[index++] = BitConverter.GetBytes(GetProgress());
}
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType, uint eventTime)
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
var currentTime = networkManager.CurrentTime;
if (eventType == 0)
{
amountUp = BitConverter.ToSingle(data, index);
SetProgress(BitConverter.ToSingle(data, index + 4), currentTime);
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress(currentTime)} and amountUp {amountUp}.");
SetProgress(BitConverter.ToSingle(data, index + 4));
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}.");
index += 8;
}
else
{
DeltaUp(UpPerPress);
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress()} to {amountUp}.");
}
return true;
}
public override void SyncedToNewTime(uint oldTime, uint newTime)
{
lastUpdate += newTime - oldTime;
}
}