Best way to implement Parallel Lua to handle multiple "team" objects

Foreword: I’m new to Parallel Luau, so please be patient.

In my use case, I plan on running serverside step logic for multiple teams in parallel, with the results per step being collected by a main manager to then be implemented.

Originally, I simply looped through each team object in my main manager and called :step() on each one. However as each team may have hundreds of NPCs and other computationally heavy tasks, this solution is too slow. I think that using Parallel Luau is the right choice here, as each team is self contained. I’m just not sure how to implement it.

From what I’ve read on documentation, you can’t really have a central script calling on desynchronized modules due to actors being in separate VMs. So, in order to maintain scalability, would I need to clone individual scripts + actors to represent a “team” object? I assume that I would need to use bindable events to communicate to each one and for each one to communicate to the main manager (as I’ve heard that shared tables are rather slow). Here’s what I’m thinking of:

Any advice would be appreciated.

Have you tried using task.spawn() like so:

task.spawn(function()
    -- Code here
end)

?

Does task.spawn() create a parallel process? I don’t see it mentioned anywhere in the Parallel Luau docs.

I do know it allows you to work with multiple things at once, but I don’t know if its a parallel process (unless my brain is dying of sleep, I don’t think it is a parallel process).

EDIT: You should try it though

Parallel Lua is intended to be used on things have require a lot of time, or processing power to actually give you any benefits at all, for this purpose, you should use it for the calulations part of the NPC’s, which would be the main source of lag for any system like this.

No, to maintain scailability, you need to centralize your code rather than split it upon many scripts, so you would need to store their respective data in a single script, and then run them parallel. and instead of seperate loops, have it concentrated on a few for specific functions.

In a few rare cases you should split your code into different scripts (Such as ModuleScripts for specific functions), but in order to maintain organization, and efficiency, it would be best to run them from a one to a few scripts at a time.

No, in fact task functions practically the exact same to coroutine, which is another way to split up threads from the main thread, parallel lua utilizes the extra cores on your CPU to run code faster, and more efficiently from the main thread, task and coroutine still run syncronized to the main thread.


Note: I dont always use parallel in my work, so I’m not the one who knows everything about how it works, I only ever use it for when im doing something that requires the performance, otherwise parallel lua is not needed in your game.

1 Like

No, to maintain scailability, you need to centralize your code rather than split it upon many scripts, so you would need to store their respective data in a single script, and then run them parallel. and instead of seperate loops, have it concentrated on a few for specific functions.

Don’t you still need a actor + script instance combination to actually use Parallel Luau? And if you have more than one script per actor then they will run in sequence on that thread. How would you create multiple parallel threads from the same script?

Not really what im saying, I’m just saying dont make too many scripts.
Its better to have one script to control the NPC’s functions rather than multiple scripts that are basically doing the same thing.

If its the same actor, then yes, otherwise it wont, it will be its own thread, or if its without an actor, the main thread.

(Missread this)
You dont, for that you need another actor.
More Actors doesnt always mean good, especially for probably simple functions that dont require it.

1 Like

Not really what im saying, I’m just saying dont make too many scripts.
Its better to have one script to control the NPC’s functions rather than multiple scripts that are basically doing the same thing.

My current structure is OOP based, I have a main manager which stores N team objects and calls methods on each one of them every step. In my specific use case all NPCs can be segregated into a specific team object. All team objects are completely independent, so I thought it would be a good idea to process all team objects in parallel. Is that an improper usage of Parallel Luau?

Also, I still don’t understand what you mean by “store their respective data in a single script and then run them parallel.” Could you elaborate?

It isnt if your script requires it, but its impractical, and inefficient to have them as complete seperate scripts, which is what im saying.

It means to keep things like cooldowns, events, damage, etc as one script and make calculations using said script across all NPC’s.
I’m not really sure how im being confusing here.

I think @TimeVector is trying to ask for a way to do it in one script (am I right or no?), which, I suggested task.spawn.

Sorry if I was being unclear, the main point of my post is help on how to parallel-ise (is that a real word?) my current system because in the worst case scenario it’s too slow. I am essentially running a game loop, say, 8 times because I have 8 different “teams”.

Perhaps the name “team” is too vague, I should call them something different - let’s say instead that they are called Areas.

Back to the example, I have 8 different areas that I step on each frame. Each world is currently represented by an Area object and it is completely isolated from another, the Main Manager is the go between for each one. Every frame, the Main Manager reads in player inputs and events and applies them to the Areas. The structure of each NPC isn’t important, I already have a robust NPC system. Each NPC is stored inside its Area object, and will never interact with an object that’s not in its Area.

The issue arises when I have multiple Areas. I can step one Area in the worst case scenario under the time limit. The issue arises when I have, say, 8 Areas running in sequence, it takes too long.

So from what I picked up, you want something that uses thread-ish systems?

I’m not sure what thread-ish systems exactly means, but I assume you mean that being able to run multiple objects concurrently (Which is what parallel luau can do, I assume). If so, yes.

I’m going to experiment with approaches and let you guys know which one worked best for me.

This thread answers my questions pretty well. Thank you, to everyone who pitched in.

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