SeedV / SeedLang

An embeddable and visualizable scripting engine for .Net and Unity.
https://seedv.github.io/SeedLang/
Apache License 2.0
9 stars 1 forks source link

SeedLangVM: support running as a Unity coroutine in the main thread #220

Open wixette opened 2 years ago

wixette commented 2 years ago

Unity WebGL player doesn't support multi-threaded mode.

The current implementation of SeedLangExamples projects runs SeedLang VM in a separate thread that queues main thread actions with an ActionQueue.

For running SeedLangExamples in web pages with Unity WebGL player, we need to run SeedLang VM in a Unity co-routine that stays in the main thread.

The simplest example:

public IEnumerator SeedLangCoroutine(...) {
   setup seedlang engine...
   while (!stopping) {
       engine.RunStep(...);
       yield return null;
   }
}

It's also required to sync up between the animation coroutine and the SeedLang coroutine. For example, in the current SortingBot project, the SeedLang engine runs the code, queues a "Swapping" animation to the main thread, and waits for the end of the animation queue, then continues the execution of the code (otherwise, the animation sequence will be out-of-sync of the program execution). When migrating the current multi-thread mode to the coroutine mode, the same sync-up behavior must be supported somehow.

For syncing up between the animation coroutine and the SeedLang VM coroutine, the ideal solution is to support C#-to-SeedPython and SeedPython-to-C# bidirectional coroutines. But this solution is a bit complex since it requires SeedPython to support the same underlying mechanism of C#'s coroutine. We can use simple callback closures or C#'s async/premise to do this first.

wixette commented 2 years ago

See the discussion of https://github.com/SeedV/SeedLangExamples/pull/16 to see a few options and their pros and cons to support coroutine.