Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork36
Worker
ialex32x edited this pageFeb 18, 2025 ·3 revisions
Worker is currently supported as an experimental feature with limitations:
- Can not use script classes (GodotJSScript) in workers
transferable objectsare not supportedonerrorandonreadyevents are not implemented yet
Warning
The class nameWorker may be changed in future versions for compatibility in different JS engines.
And,Worker scripts may need to have a specific file name suffix or reside in a designated directory.
// tests/master.ts// ...import{Object,Node}from"godot";import{JSWorker}from"godot.worker";letworker=newJSWorker("tests/worker");worker.onmessage=function(m:any){console.log("master: get message",m);}worker.ontransfer=function(obj:Object){console.assert(objinstanceofNode);this.add_child(obj);worker.terminate();}worker.postMessage("hello");
// tests/worker.tsimport{PackedScene,ResourceLoader}from"godot";import{JSWorkerParent}from"godot.worker";if(typeofJSWorkerParent!=="undefined"){JSWorkerParent.onmessage=function(m:any){console.log("worker: get message",m);JSWorkerParent.postMessage("worker result");// [EXPERIMENTAL] instantiate a PackedScene in worker thread and transfer it back to master threadletasset=<PackedScene>ResourceLoader.load("res://background.tscn");letobj=asset.instantiate();console.log("[worker] transfering object:",obj);JSWorkerParent.transfer(obj);// worker can terminate itself by calling `close()`// close();}}
Scripting
Utilities
Experimental Features
Advanced
Misc