Problem
As a Roblox developer, it is currently too hard to use Parallel LUA with non-thread safe aspects of the API. I’m finding that I have to pepper my code with task.synchronize()
and task.desynchronize()
statements or create wrapper functions which includes the aforementioned statements to work around this issue. As a result, it takes time to merge a thread into the main thread group and then pull it back out whereas not needing to do this will add to game performance and the ability to handle more players.
I realize that Roblox is working towards making their API more thread safe, but in the meantime, I propose the following solutions:
-
If I can guarantee that nothing else is going to access a thread unsafe property/method of an instance (usually a part, model, or constraint), there should be a flag to disable the parallel mode warnings/errors that are associated with it. A script could check for this flag to determine if it needs to synchronize or not.
-
The second solution, and probably the better one in my opinion, is the ability to set which actor can access the properties/methods of the instance be it a part, model, or whatever. If something other than threads in that actor access it, then it throws a warning/error. A script can check to see if it has permission to access the instance by checking if it’s actor is set against the instance. If the setting is nil, then only the main thread group can access it and synchronization is required. This could be preset in Studio.
Obviously, the proposed solutions will not work for services which are not thread safe such as DataStoreService
and PathfindingService
services, but they should work for other things.
Use Cases
There are several use cases for this that I have that would benefit greatly from this feature:
-
NPCs - An NPC which is under its own actor is in an isolated environment so only the scripts that are specific to the NPC can access the resources provided by the NPC such as animations, sounds, parts, models, constraints, etc… In my implementation, the bulk of the NPC logic is stored in module scripts that are included in the main NPC controller script. The module scripts are written in such a way as they can only access the NPC’s resources, so I can guarantee that nothing else will be able to access the NPC’s resources from outside the actor.
-
Map Components - Once again, individual components such as elevators/lifts, jump pads, teleporters, automatic doors, etc… are under their own actors. As such, each one has it’s own individual resources that it uses in isolation. Like NPCs, these resources include parts (including base parts, mesh parts, union operations, and intersect operations), constraints, sounds, and animations.
-
Traps - Traps which are trigged by players are also within their own actors. As stated above, the same resource isolation applies.
-
Weapons - Weapons are also within their own actors. Since they create and modify parts, constraints, models, and effects, and they are used quite often, they would benefit greatly from such an ability. The amount of processing involved in weapons is also formidable since there’s a lot of code that the server must chew through to not only produce the effects, but ammunition management, security checks, target hit evaluation, etc… Effects include things like fire, smoke, lighting, beams, etc…
I am unsure of the internal implementation of the engine in regards to Parallel LUA or if there is a technical reason as to why this cannot be done.
Conclusion
If Roblox is able to address this issue, it would improve my Roblox development experience by allowing me to not have to worry about synchronizing and desynchronizing when accessing thread unsafe aspects of the API when I can guarantee that nothing else will be accessing it outside of the specific actor in question.