Calling Methods from the main thread using Loom

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,099
4,863
113
At your house.
github.com
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#
C#:
public void Call()
{
if (Util.GetUtil().CurrentWorkingThreadID != Util.GetUtil().MainThreadID)
{
Loom.QueueOnMainThread(() => {
Call();
});
return;
}
Console.WriteLine("Hello");
}
Python:
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:
Python:

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

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

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,099
4,863
113
At your house.
github.com
I added a Python example.

I was thinking on automatically calling the timer methods from the main thread after they elapsed though.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,099
4,863
113
At your house.
github.com
Updated the page with more information again. Those methods need to be under the Loom class inorder to avoid crash popups. (If the code is in Timer or Thread)
 
  • Like
Reactions: Jakkee