How can i use Parallel Luau to make efficient NPCs

so ive been looking into ways to make NPCs as efficient and smooth as possible. they will be used for tycoons. i heard that Parallel Luau can help with things like that but i really dont understand it at all. has anyone used it for their npcs or have any examples?

1 Like

Imagine going to a store and buying things. You pull up to the front and there is only one register open and 32 people in front of you. You have to wait for every single person in front of you to finish before you get your turn at the register.

Imagine now that there are 8 open registers. Now, since each person is completely self contained and won’t affect the purchases of the others, you are free to just put any person on any lane when they are open. This allows you to speed up the rate at which you finish all customers.

This is basically parallel luau. lua by default is a single thread language which basically makes it so that every script has to wait their turn to run on the one thread dedicated to it. Akin to a single checkout lane. Parallel luau allows you to spread out your running functions (think co-routines) onto multiple threads which means instead of going through them sequentially, you get to set up some of these functions to be able to run at the same time. Running code at the same time can lead to problems though. This leads to things like what if you change a variable in one script at the same time as another or so close you can’t predict it. This could lead to things crashing since one script might need a value one way and the other the value the other way. There are ways to intelligently share this information, but parallel luaus approach is to just not share information.

Make it so that each function is unable to or edit data outside of itself. If a function is meant to be ran in parallel, you can pass stuff in, but otherwise you basically loose any external scope so that the computer can be sure not to accidentally break code elsewhere. Any code that would need to share data would need to go back and wait at that first register to make sure that the data isn’t being accessed at the same time as other things that also need it.

So basically any actor gets it’s own checklane to run where you specifically assign it customers. The actor looses any access to data from external contexts when you desynchronize it, but can get put back in the one check lane to rule them all when you resynchronize it allowing you to still share results back.

So if you have a lot of tasks that only need different inputs and don’t need to rely on calculations from other parts, you have a very good candidate for parallel luau. This extends to npcs well because each NPC can be pretty much self contained. This allows you to desynchronize a lot of the stuff since one NPC usually won’t affect another. In cases where they do, you’ll need to intelligently handle putting stuff back on the main thread (synchronize it) to pick up information they need then putting it back on it’s seperate thread (desync). Ideally you want it to be desynced as much as possible as that’s where the performance benefit comes from.

5 Likes