You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
I have had a bit more time to explore unity-jsb, and have stumbled upon a place where I can initialize the runtime and receive Awake and OnEnable calls to my script. It seems though that no other MonoBehaviour lifecycle methods are being called.
Two scripts were created to handle creating and destroying the ScriptRuntime. I found that I needed to change the script execution order within Unity to have the runtime created before the majority of the other scripts were ran, and in like have the runtime destroyed following the other scripts.
// CreateRuntime.csusingQuickJS;usingQuickJS.Binding;usingQuickJS.IO;usingQuickJS.Utils;usingUnityEngine;publicclassCreateRuntime:MonoBehaviour{privateScriptRuntimeruntime;privatevoidAwake(){RuntimeLoggerlogger=newRuntimeLogger();runtime=ScriptEngine.CreateRuntime();IFileSystemfileSystem=newDefaultFileSystem(logger);varasyncManager=newDefaultAsyncManager();varpathResolver=newPathResolver();pathResolver.AddSearchPath("node_modules");pathResolver.AddSearchPath("Scripts/out");runtime.AddModuleResolvers();runtime.Initialize(newScriptRuntimeArgs{fileSystem=fileSystem,pathResolver=pathResolver,asyncManager=asyncManager,logger=logger,byteBufferAllocator=newByteBufferPooledAllocator(),binder=DefaultBinder.GetBinder(false),});}privatevoidUpdate(){if(runtime!=null){runtime.Update((int)Time.deltaTime);}else{Debug.LogError("Lost reference to the runtime!");}}}// DestroyRuntime.csusingSystem;usingQuickJS;usingUnityEngine;publicclassDestroyRuntime:MonoBehaviour{privatevoidOnDestroy(){ScriptEngine.Shutdown();GC.Collect();GC.WaitForPendingFinalizers();}}
Everything seems to work without issue at this point. At least in the sense of no errors being thrown, and the console not reporting anything amiss. When I attach a JSBehaviour, with its source file pointing to the below script, to an empty object though, the runtime seems to fail at calling the Update, OnDisable, or the OnDestroy methods that are defined. It does not report any issues to the Unity console, in fact it does not do anything despite the console.log calls. As reported earlier, the Awake and OnEnable do correctly report their messages to the console.
// Testing.tsimport{MonoBehaviour}from"UnityEngine";import{ScriptString,ScriptType}from"./plover/editor/editor_decorators";
@ScriptType()exportclassTestingextendsMonoBehaviour{
@ScriptString()protectedmessage="Hello World";privatetick=0Awake(){this.reportMessage();}OnEnable(){console.log("Enabling the test object.");}OnDisable(){console.log("Disabling the test object.");}OnDestroy(){console.log("Destroying the test object.");}Update(){this.tick++;if((this.tick%100)==0){console.log("Updating the test object.");}}reportMessage(){console.log(`The message from the test object is: ${this.message}.`);}}
Have I done something incorrect in creating the runtime? I have tried comparing to the example projects from this repository, and I seem to have hit on all of the key points in creating and updating the runtime. Is there something amiss that I am overlooking?
Thank you!
The text was updated successfully, but these errors were encountered:
The Update call was moved into another MonoBehaviour JSBehaviourFull (I'm finding a better way to friendly switch between JSBehaviour and JSBehaviourFull (You can not change one into another in Unity Editor at present). Anyway, I'll check it in my environment.
Changing over to the JSBehaviourFull component does seem to resolve the issue with the Update method being called, so that is wonderful. Thank you!
It does seem in moving to the new component that I am having an error thrown when ending the "Play session" within the editor. It is showing a script runtime not ready error once wrapped up.
From the message provided, it does seem to be related to the editor and does not appear that it would affect the end product. Simply wanted to forward that message your way.
the error script runtime not ready is caused by uncertain execution order, it's difficult to resolve, because the script engine initialization process could/should be asynchronous (e.g waiting for the async file system ready).
So, it's better and safer to wrap them in a prefab, and instantiate the scripted prefab in a script.
I have had a bit more time to explore unity-jsb, and have stumbled upon a place where I can initialize the runtime and receive
Awake
andOnEnable
calls to my script. It seems though that no otherMonoBehaviour
lifecycle methods are being called.Two scripts were created to handle creating and destroying the
ScriptRuntime
. I found that I needed to change the script execution order within Unity to have the runtime created before the majority of the other scripts were ran, and in like have the runtime destroyed following the other scripts.Everything seems to work without issue at this point. At least in the sense of no errors being thrown, and the console not reporting anything amiss. When I attach a
JSBehaviour
, with its source file pointing to the below script, to an empty object though, the runtime seems to fail at calling theUpdate
,OnDisable
, or theOnDestroy
methods that are defined. It does not report any issues to the Unity console, in fact it does not do anything despite theconsole.log
calls. As reported earlier, theAwake
andOnEnable
do correctly report their messages to the console.Have I done something incorrect in creating the runtime? I have tried comparing to the example projects from this repository, and I seem to have hit on all of the key points in creating and updating the runtime. Is there something amiss that I am overlooking?
Thank you!
The text was updated successfully, but these errors were encountered: