|
1 | | -# roblox-lua-parallel |
| 1 | +# roblox-lua-parallel |
| 2 | + |
| 3 | +A simple parallel execution library for Roblox lua. |
| 4 | + |
| 5 | +This library uses Roblox actors to run tasks in parallel. |
| 6 | + |
| 7 | +Read more about Parallel Luau here: [Parallel Luau](https://create.roblox.com/docs/scripting/multithreading) |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +### Method 1 - Wally |
| 12 | +1. Install [Wally](https://wally.run/install) |
| 13 | +2. Add the dependency to your project's `wally.toml` file |
| 14 | +```toml |
| 15 | +[dependencies] |
| 16 | +Parallel = "dig/Parallel@^1" |
| 17 | +``` |
| 18 | +3. Run `wally install` |
| 19 | + |
| 20 | +### Method 2 - Manual |
| 21 | +1. Download the `Parallel.rbxm` file from the latest release |
| 22 | +2. Drag it into your `ReplicatedStorage` folder |
| 23 | + |
| 24 | +## Basic Example |
| 25 | +This example runs a function that returns "Hello, world!" in a new thread: |
| 26 | +```lua |
| 27 | +local ReplicatedStorage = game:GetService("ReplicatedStorage") |
| 28 | +local Packages = ReplicatedStorage:WaitForChild("Packages") |
| 29 | + |
| 30 | +-- Require parallel module |
| 31 | +local Parallel = require(Packages.Parallel) |
| 32 | + |
| 33 | +-- Prepare a thread with a runnable function |
| 34 | +-- This function will be executed in a new thread |
| 35 | +local preparedThread = Parallel.of(function() |
| 36 | + return "Hello, world!" |
| 37 | +end) |
| 38 | + |
| 39 | +-- Submit the thread and get the result via promise |
| 40 | +local result = preparedThread:submit():expect() |
| 41 | +print(result) -- Prints "Hello, world!" |
| 42 | + |
| 43 | +preparedThread:destroy() |
| 44 | +``` |
| 45 | + |
| 46 | +## Advanced Example |
| 47 | +This example runs spatial query function `GetPartBoundsInBox` in a new thread and returns the results: |
| 48 | +```lua |
| 49 | +local RunService = game:GetService("RunService") |
| 50 | +local ReplicatedStorage = game:GetService("ReplicatedStorage") |
| 51 | +local Packages = ReplicatedStorage:WaitForChild("Packages") |
| 52 | + |
| 53 | +-- Require parallel module |
| 54 | +local Parallel = require(Packages.Parallel) |
| 55 | + |
| 56 | +-- Prepare a thread with a runnable function |
| 57 | +-- This function will be executed in a new thread |
| 58 | +local preparedThread = Parallel.of(function(position: CFrame, size: Vector3) |
| 59 | + return workspace:GetPartBoundsInBox(position, size) |
| 60 | +end) |
| 61 | + :withName("SpatialQueryWorker") |
| 62 | + :withActors(2) |
| 63 | + |
| 64 | +-- Example zone of 200 x 200 x 200 |
| 65 | +local zone = Instance.new("Part") |
| 66 | +zone.Name = "Zone" |
| 67 | +zone.Position = Vector3.new(0, 0, 0) |
| 68 | +zone.Size = Vector3.new(200, 200, 200) |
| 69 | +zone.Anchored = true |
| 70 | +zone.CanCollide = true |
| 71 | +zone.Transparency = 0.8 |
| 72 | +zone.Parent = workspace |
| 73 | + |
| 74 | +while RunService:IsRunning() do |
| 75 | + -- Submit the thread and get the result via promise |
| 76 | + local parts = preparedThread:submit(zone.CFrame, zone.Size):expect() |
| 77 | + print(parts) -- Prints array of instances found inside the provided zone |
| 78 | + |
| 79 | + task.wait(1) |
| 80 | +end |
| 81 | + |
| 82 | +preparedThread:destroy() |
| 83 | +``` |
| 84 | + |
0 commit comments