From 60d98a6330941d53c56a01d99ee2396f2845753d Mon Sep 17 00:00:00 2001 From: mwfarb Date: Mon, 14 Oct 2024 14:05:16 -0400 Subject: [PATCH] fixed ttl component --- Runtime/ArenaClientScene.cs | 20 +++++++++++--------- Runtime/ArenaTtl.cs | 16 ++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Runtime/ArenaClientScene.cs b/Runtime/ArenaClientScene.cs index 9814cdb..6de1d2c 100644 --- a/Runtime/ArenaClientScene.cs +++ b/Runtime/ArenaClientScene.cs @@ -557,15 +557,6 @@ private void CreateUpdateObject(ArenaObjectJson msg, object indata, object menuC aobj = gobj.AddComponent(typeof(ArenaObject)) as ArenaObject; arenaObjs[msg.object_id] = gobj; aobj.Created = true; - if (msg.persist.HasValue) - aobj.persist = (bool)msg.persist; - aobj.messageType = msg.type; - if (msg.ttl != null) - { - if (!gobj.TryGetComponent(out var ttl)) - ttl = gobj.AddComponent(); - ttl.SetTtlDeleteTimer((float)msg.ttl); - } #if UNITY_EDITOR // local create context auto-select if (menuCommand != null) @@ -577,6 +568,17 @@ private void CreateUpdateObject(ArenaObjectJson msg, object indata, object menuC #endif } + // apply storage-level object properties + if (msg.persist.HasValue) + aobj.persist = (bool)msg.persist; + aobj.messageType = msg.type; + if (msg.ttl != null) + { + if (!gobj.TryGetComponent(out var ttl)) + ttl = gobj.AddComponent(); + ttl.SetTtlDeleteTimer((float)msg.ttl); + } + JObject jData = JObject.Parse(JsonConvert.SerializeObject(indata)); //new JsonSerializerSettings //{ diff --git a/Runtime/ArenaTtl.cs b/Runtime/ArenaTtl.cs index 9ffd918..7789e09 100644 --- a/Runtime/ArenaTtl.cs +++ b/Runtime/ArenaTtl.cs @@ -7,7 +7,7 @@ namespace ArenaUnity [RequireComponent(typeof(ArenaObject))] public class ArenaTtl : MonoBehaviour { - DateTime? expiration = null; + long? expiration = null; private void Start() { @@ -15,23 +15,19 @@ private void Start() public void SetTtlDeleteTimer(float seconds) { - DateTime now = DateTime.Now; - int secOnly = (int)Math.Truncate(seconds); - int msOnly = (int)Math.Truncate((seconds - secOnly) * 1000); - TimeSpan time = new(0, 0, 0, secOnly, msOnly); - expiration = now.Add(time); + expiration = DateTimeOffset.Now.ToUnixTimeMilliseconds() + (long)(seconds * 1000); } private void Update() { - Debug.Log($"ttl up: {expiration - DateTime.Now}"); - if (expiration != null && expiration > DateTime.Now) + if (expiration != null && DateTimeOffset.Now.ToUnixTimeMilliseconds() > expiration) { var aobj = GetComponent(); if (aobj != null) + { aobj.externalDelete = true; - - Destroy(gameObject); + Destroy(gameObject); + } } }