From 517e2609ba9b5c508644156f653939398c00c4e0 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sun, 29 May 2022 14:37:27 -0700 Subject: [PATCH] TryGetWorldObject --- QSB/WorldSync/QSBWorldSync.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/QSB/WorldSync/QSBWorldSync.cs b/QSB/WorldSync/QSBWorldSync.cs index e2704601..9c50dcb6 100644 --- a/QSB/WorldSync/QSBWorldSync.cs +++ b/QSB/WorldSync/QSBWorldSync.cs @@ -269,6 +269,26 @@ public static class QSBWorldSync return (TWorldObject)worldObject; } + public static bool TryGetWorldObject(this MonoBehaviour unityObject, out TWorldObject worldObject) + where TWorldObject : IWorldObject + { + if (!unityObject) + { + DebugLog.ToConsole($"Error - Trying to run GetWorldFromUnity with a null unity object! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:NULL, Stacktrace:\r\n{Environment.StackTrace}", MessageType.Error); + worldObject = default; + return false; + } + + if (!UnityObjectsToWorldObjects.TryGetValue(unityObject, out var iWorldObject)) + { + worldObject = default; + return false; + } + + worldObject = (TWorldObject)iWorldObject; + return true; + } + /// /// not deterministic across platforms. /// iterates thru all objects and throws error if there isn't exactly 1.