Trouble passing function to parallel scripts

With the new image editing API, I saw the opportunity to make a “pseudo-shader” module where devs could create/play shader-like images based on some “draw” function provided by them that returns pixel data.

The first implementation was real-time, and it was very laggy… not because of the image API itself, but instead because of the draw function being called and doing work many, many times per frame depending on the resolution (and of course without the benefits of running on the GPU vs CPU). So, I set up a system where instead of being real-time, frames would be pre-rendered and stored for playback. It worked great!

video

Though… you can still see the game chugs while rendering, even at 64x64 resolution. Today while looking for ways to speed up this process, I came across the “Parallel Luau” page in the creator docs. I tried implementing it and got everything set up, but there was one problem: I can’t find a way for the parallel scripts to obtain and use the draw function from the module script.

SharedTable is out of the question since functions are forbidden values; BindableFunction isn’t usable in parallel; an intermediate module to hold functions doesn’t seem to update for the parallel scripts; and loadstring isn’t available for LocalScript.

Do I have any other options or workarounds? Or should I just revert to the previous version of the module without any threading?

Also, I’m not too experienced with Luau and asynchronous code; I do apologize if there’s any oversight here.

1 Like

The only way to make that work is by making the developers put their drawing function in a module script and then passing that around, the worker script will require that and call the function the module script returns instead. You can’t pass functions but you can pass Instances around so that’s the only way to do it.

This is because:

  1. Functions can’t be serialized
  2. Functions from a VM can’t be accessed by another VM. scripts inside of different actors are running in different VMs so even if they somehow got access to the function, attempting to call them will return an error.
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.