How can I not load all this map generation at once?
I have a map generation system, but I want it to optimize so that maybe it doesn't load it all at once. For instance a render distance. Or maybe a flood algorithm that takes the players position, rounds it to the nearest chunk and expands. Im really not sure of the best way to do this, so Im open to suggestions. the way im doing it lags if the player falls or moves fast, because it's based on distance from each chunk.
Code In update:
for (int x = 0; x < generatedChunks.GetLength (0); x ++)
{
for (int y = 0; y < generatedChunks.GetLength (1); y ++)
{
if (generatedChunks [x, y])
{
if (Vector3.Distance (player.transform.position, chunks [x, y].transform.position) <= renderDistance)
{
chunks [x, y].SetActive (true);
}
else
{
chunks [x, y].SetActive (false);
}
}
else
{
int tileX = chunkSize * chunkCountX;
int tileY = chunkSize * chunkCountY;
Vector3 chunkPos = new Vector3 (x * chunkSize - tileX / 2 + chunkSize / 2, y * chunkSize - tileY + chunkSize / 2, 0);
if (Vector3.Distance (player.transform.position, chunkPos) <= renderDistance)
{
GenerateChunk (x, y, tileX, tileY, chunkPos, chunkContainer, true);
}
}
}
}
$$anonymous$$aybe use yield to pause the code every now and then, so it'll only load a certain amount per frame ins$$anonymous$$d of everything in 1 frame.
Hi @$$anonymous$$hozo,
I see a few problems with this. If you could clarify how to overcome these problems Id be happy to accept an answer.
If you make a yield, at some times you may see an unrendered part of the world. However if you dont you see everything but it's very unoptimized.
I hope you can get back to me on this, thanks!
PS should I even bother with keeping the unrender part? should everything just stay when It loads? would it make a big difference?
Hi @awplays49,
$$anonymous$$ake it so it yields after it's looped a certain amount of time, for example
if(y == $$anonymous$$athf.Round(y/30)*30)) {
yield 0;
}
This will yield the code every 30 chunks it's loaded. Change the number 30 to any number that suits the project. If there's still unrendered parts of the map then increase the number 30 to a bigger number.
I dont see a difference in the generation :/
IEnumerator UpdateWorld () {
for (int x = 0; x < generatedChunks.GetLength (0); x ++)
{
if (x == $$anonymous$$athf.Round (x / 30) * 30)
{
yield return 0;
}
for (int y = 0; y < generatedChunks.GetLength (1); y ++)
{
if (y == $$anonymous$$athf.Round (y / 30) * 30)
{
yield return 0;
}
if (!generatedChunks [x, y])
{
int tileX = chunkSize * chunkCountX;
int tileY = chunkSize * chunkCountY;
Vector3 chunkPos = new Vector3 (x * chunkSize - tileX / 2 + chunkSize / 2, y * chunkSize - tileY + chunkSize / 2, 0);
if (Vector3.Distance (player.transform.position, chunkPos) <= renderDistance)
{
GenerateChunk (x, y, tileX, tileY, chunkPos, chunkContainer, true);
}
}
}
}
isUpdatingWorld = false;
}
Your answer
Follow this Question
Related Questions
URP post processing = fps drop (low and mid-end mobile devices) 1 Answer
How to optimize a PC game? 1 Answer
Comparing distance of vector 3's on a dynamic growing list 0 Answers
Optimization: how to do a checking less often than within an Update 2 Answers
Debris particles slowing down simulation 0 Answers