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.