-
What do you want to achieve?
I would like to use parallel luau with my terrain generator to lighten the lag and increase the rate at which chunks are loaded in. -
What is the issue?
I tried using parallel luau, though I think I’m doing it wrong. I saw an example where someone created a bunch of actors with bindable functions under them and bound a function to each and ran them. I tried to recreate this and surprisingly I had no lag on my old computer but the terrain is now generating incredibly slowly even though it’s not lagging the main game. It’s going at less than 1/30 the speed of running the terrain generator synchronized. -
What solutions have you tried so far?
This is the main part of the current code:
for cx = 1, scale do
-- Pause terrain generator every 1/yield times to reduce lag.
local actor = Instance.new("Actor", terrainActor)
local bindable = Instance.new("BindableFunction", actor)
local function binded()
task.desynchronize()
for cz = 1, scale do
-- Get Height
local height = htab[cx][cz]
local height1 = height+1
local mcx = mats[cx]
local ocx = occs[cx]
for cy = -yScale, yScale do
-- Generate material
local mat = worldGen.getMaterial(cy, height)
local tY = cy+yScale+1
mcx[tY][cz] = mat
-- Generate occupancy
local val = worldGen.getOccupancy(cy, height, tY, x, z, cx, cz)
ocx[tY][cz] = val
end
end
task.synchronize()
end
bindable.OnInvoke = binded
bindable:Invoke()
end
terrainActor:ClearAllChildren()
wait()
local region = genRegion(x, z)
-- Create terrain
terrainService:WriteVoxels(region, 4, mats, occs)
These are the relevant functions of the main script:
-- Get material for height.
worldGen.getMaterial = function(cy, height)
cy = cy + yScale
height = height + yScale
if cy < height then
if cy < height*0.2 then
return basalt
elseif cy < height*0.4 then
return salt
elseif cy < height*0.6 then
return slate
elseif cy < height*0.95 then
return stone
else
if cy > mountainZone then
return stone
elseif cy > fieldZone then
return grass
else
return sand
end
end
else
if cy > yScale then
return air
else
return water
end
end
end
worldGen.getOccupancy = function(cy, height, tY, x, z, cx, cz)
local val = 0
if cy < math.floor(height) then
val = 1
elseif cy < height then
val = height - cy
task.synchronize()
-- v This creates a random object (or nothing) on the surface
genDecoration(tY, x, z, cx, cy, cz)
task.desynchronize()
elseif cy <= 0 then
val = 1
end
return val
end
Sorry for the lack of comments on most of the code.