Calling Methods from the main thread using Loom

Jun 24, 2017
Calling Methods from the main thread using Loom
  • Description:

    Usage of some UnityEngine methods causes the rust server to throw an "Oops! Crashed" window.
    This is not a Rust, nor a Fougerite bug, but a UnityEngine version bug. It happens when specific UnityEngine methods are called outside from the main thread, such as new threads or Timers.

    Currently known methods that cause It:
    • UnityEngine.Object.FindObjectsOfType (Any Unity Find methods)
    Currently known Fougerite/Rust methods that are calling them:
    • Player.Disconnect() - Loom is automatically called at this method, you don't need to handle this.
    • World.Entities
    • World.SupplyCrates
    • World.LootableObjects
    • World.StructureComponents()
    • World.DeployableObjects()
    • World.BasicDoors()

    How to avoid the crash popup:

    Use our newly implemented class called Loom. It can help you call code from the main thread.
    Crash popups can happen when a specific code uses UnityEngine.Object.FindObj... under a thread.

    C#
    Code (C#):
    public void Call()
    {
        if (Util.GetUtil().CurrentWorkingThreadID != Util.GetUtil().MainThreadID)
        {
            Loom.QueueOnMainThread(() => {
                Call();
            });
            return;
        }
        Console.WriteLine("Hello");
    }
    Python:
    Code (Python):
    def On_PluginInit(self):
        ConnectionData = Plugin.CreateDict()
        Plugin.CreateParallelTimer("Connect", 10000, ConnectionData).Start()

    def ConnectCallback(self, ATimedEvent):
        ATimedEvent.Kill()
        Plugin.Log("Testing", "MainThreadID: " + str(Util.MainThreadID) + " CurrentWorkingThreadID: "
                    + str(Util.CurrentWorkingThreadID))

        # Using Loom to call at the main thread:
        Loom.QueueOnMainThread(lambda:
            Plugin.Log("Testing", "MainThreadID: " + str(Util.MainThreadID) + " CurrentWorkingThreadID: "
                        + str(Util.CurrentWorkingThreadID))
        )

    JavaScript:

    Lua:


    Simple Threading for Python, JavaScript, and Lua (C# has Threading class):


    Python:
    Code (Python):

    def HardStuffThatUsesCPU(self, Number):
        for i in xrange(0, Number):
            math.sqrt(i)

    def On_PluginInit(self):
        Loom.ExecuteInBiggerStackThread(lambda:
            self.HardStuffThatUsesCPU(1000000)
        )