Reduce process priority

Distant terrain generation is a very expensive but low-priority process, but on the server it can interfere with physics and in play mode it causes freezes.
Is there a way to get a process yield to everything else?

As of now I don’t think it’s possible although I may be wrong.

The lag might be from part instantiation. If it’s in the distance can’t you just store the terrain until it becomes closer? Or slowly instantiate while it’s in the distance?

Depends on the terrain if it’s a part type of terrain yes but if it’s the actual terrain that may be a little harder

Slowly instantiating doesn’t work.
In theory, I could instantiate terrain with zero percievable lag; there’s plenty of free CPU time unoccupied by physics and graphics calculations.
But in practice, if I instantiate terrain with lots of waiting, I just break one half-second freeze into several seconds of choppy performance.

This is purely a matter of controlling how Lua multithreads.

Lua doesn’t multithread, it employs cooperative multitasking: only one Lua coroutine runs at once, and it yields when you ask it to, and at no other time. Consequently there’s no such concept as priority.

The best you can do is determine how long the terrain generator has been running for since the last wait() and, once this has exceeded some amount of time that’s appropriate for the generator to use per frame, call wait() to yield to the other tasks that need to be done that frame. You could also write code to reduce this amount of time if it detects that frames are taking too long.

1 Like

I’ve done exactly that before for something which updates a lot of GUI frames, and it didn’t work.
But then I realised just now that I had tied the event to RenderStepped which meant it was waiting for a render call then jumping in before the render call which maximised lag, so I changed it to Heartbeat and now it’s completely smooth.

I imagine that having terrain generation be tied to Heartbeat would mean that it’d jump in before physics and other stuff, but it’d be literally a millisecond per frame or something so it wouldn’t cause everything to suddenly freeze.