- Home /
How to stop game freezing when doing large calculation? multi threading?
So i have a map generator that take a few seconds to generate and display. However, during these seconds the game completely freezes. Is there a way that i can do the calculation on a different thread and have the game not freeze when generating? Any help is greatly appreciated! :)
Seems normal to me that map generation can take a while, and usually this is only done during the start of the game level and a loading screen is displayed in most games.
Answer by RendrWyre · Jan 10, 2020 at 02:47 AM
To multi-thread you can use Unity's Job system.
(https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJob.html)
https://docs.unity3d.com/Manual/JobSystemSchedulingJobs.html
and if you're using jobs look into the burst compiler.
https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html
I have tried doing a simple test with unity Jobs and it still freezes while calculating. am i doing something wrong?
public void Iterate(){
NativeArray<int> RESULT = new NativeArray<int>(1, Allocator.TempJob);
IterateJob iterateJob = new IterateJob {
RESULT = RESULT
//iterations = _iterations
};
JobHandle iterateJobHandle = iterateJob.Schedule();
iterateJobHandle.Complete();
Debug.Log(RESULT[0]);
RESULT.Dispose();
}
public struct IterateJob : IJob
{
public NativeArray<int> RESULT;
public void Execute()
{
RESULT[0] = 12412;
for (int i = 0; i < 50000000; i++)
{
RESULT[0] = (int)$$anonymous$$athf.Sqrt(RESULT[0]);
}
}
}
You call JobHandle.Complete inside your starting code which runs on the main thread. Read the manual. The Complete method will ensure the job has completed now. So if it hasn't been started yet it would be executed from inside Complete synchonosly on this thread and would block it.
If the job has already been started on a seperate thread you get a similar end result since the Complete method will wait / block until the job has finished since that's the point of the Complete method.
Running something on a seperate thread means it's executed asynchonously. So you won't get your results immediately. Whenever you wait inside your starting code for the completeion, it would be no difference than not using a seperate thread at all since you block the main thread until the job is done.
You seem to have used the example given here one-to-one, however you haven't read the comment above the Complete line which tells you that you usually shouldn't do this. Well the Unity documentation is known for its bad examples ^^.
Loading task are usually started in Start. To keep the game running before the job is completed you would check each frame if the job has completed and then do finalizing steps on the main thread. You could use the Update method to check for JobHandle.IsCompleted, however a coroutine would make more sense for one time loading tasks
IEnumerator Load()
{
// prepare your job
// [ ... ]
// schedule your job
JobHandle iterateJobHandle = iterateJob.Schedule();
// check each frame until finished
while (!iterateJobHandle.IsCompleted)
yield return null;
// job has finished, use your data
// [ ... ]
}
Follow this Question
Related Questions
Multithreading freezes editor 1 Answer
How would I create a prefab that moves forward? 2 Answers
Ball physics for game - normal physics fine? 1 Answer
Vector3 randomize spawnpoints 1 Answer
Setting GameObject in a script. 0 Answers